0
次のコードでは、3つの表示リストボタンの1つをクリックすると、まずコンポジットにラベルが表示され、文字列リストの各行にラベルが作成されます。廃棄と再作成後にSWTラベルが表示されない
同じボタンを2回クリックすると、すべてのテキストが分散してしまうという問題があります。
他のボタンとテキスト表示のいずれかに切り替えると、
コードはコンポジット内のすべてのラベルを破棄して新しいラベルをコンポジットに追加するたびに同じことを行いますが、まったく同じ内容のラベルをコンポジットに追加すると何らかの理由で画面に表示されなくなりました。
content.layout();
を呼び出す
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class BasicMapLabelTestDialog extends Dialog
{
List<Label> displaylabels = new ArrayList<>();
Composite content, list;
public BasicMapLabelTestDialog(Shell parentShell)
{
super(parentShell);
}
@Override
protected void configureShell(Shell shell)
{
super.configureShell(shell);
shell.setSize(new Point(700, 500));
shell.setText("FML"); //$NON-NLS-1$
}
@Override
public Control createDialogArea(final Composite comp)
{
content = (Composite) super.createDialogArea(comp);
content.setLayout(new GridLayout(1, false));
content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final List<String> rows1 = new ArrayList<>(3);
rows1.add("Hello");
rows1.add("Baz");
rows1.add("Please");
rows1.add("Help");
final List<String> rows2 = new ArrayList<>(3);
rows2.add("You're");
rows2.add("My");
rows2.add("Only");
rows2.add("Hope");
final List<String> rows3 = new ArrayList<>(3);
rows3.add("Or");
rows3.add("Maybe");
rows3.add("greg?");
Button set1 = new Button(content, SWT.PUSH);
set1.setText("Display List 1");
set1.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
updateList(rows1);
}
});
Button set2 = new Button(content, SWT.PUSH);
set2.setText("Display List 2");
set2.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
updateList(rows2);
}
});
Button set3 = new Button(content, SWT.PUSH);
set3.setText("Display List 3");
set3.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
updateList(rows3);
}
});
Button test = new Button(content, SWT.PUSH);
test.setText("Print Label Content");
test.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
for (Label l : displaylabels) {
System.out.println(l.getText());
}
}
});
list = new Composite(content, SWT.NONE);
list.setLayout(new GridLayout(1, true));
new Label(content, SWT.HORIZONTAL | SWT.SEPARATOR);
return content;
}
private void updateList(List<String> rows) {
if (this.displaylabels == null) {
this.displaylabels = new ArrayList<>();
}
for (Label l : displaylabels) {
l.dispose();
}
this.displaylabels.clear();
for (String item : rows) {
addListLabel(item);
}
content.layout();
content.redraw();
}
private void addListLabel(String whoText) {
Label a = new Label(list, SWT.NONE);
a.setText(whoText);
this.displaylabels.add(a);
}
public static void main(String[] args)
{
Display d = new Display();
Shell s = new Shell();
BasicMapLabelTestDialog fml = new BasicMapLabelTestDialog(s);
fml.open();
}
}
おかげで、
グレンX
私はあなたが最初にリストに載っていないと悪いと感じています。ありがとうございました。その行動はとても難解でした。私はそれを決して考えなかったでしょう。 – Link19