SWT composite - redraw problem

匿名 (未验证) 提交于 2019-12-03 03:05:02

问题:

I have a composite element, that initially has a Label. Now I call dispose on the it (the label) and create another label in the same container (composite elm), but I don't see the new text. It brings me to question how do I enable redraw on the composite, so that the new label (or any other component I might create) will render in place of the old one. Here is the code I have (separated into a unit test for redraw a composite)

private Label createLabel( Composite parent) {     Label label = new Label(parent, SWT.NONE);     label.setAlignment(SWT.CENTER);     label.setLayoutData( new GridData( SWT.CENTER, SWT.CENTER, true, true) );     return label; }  private void changeText() {     assert testCell != null : "Please initialize test cell";     testCell.getChildren()[0].dispose();     Label l = createLabel(testCell);     l.setText("New TexT");     testCell.redraw(); }  private void draw() {     Display display = new Display();     shell = new Shell(display);     shell.setLayout(new GridLayout(2,false));      testCell = new Composite(shell,SWT.BORDER);     testCell.setLayout(new GridLayout());     Label l = createLabel(testCell);     l.setText("Old Text");     Composite btnCell = new Composite(shell,SWT.NONE);     btnCell.setLayout(new GridLayout());     Button b = new Button(btnCell, SWT.PUSH);     b.setText("Change");     b.addListener(SWT.MouseDown, new Listener() {         public void handleEvent(Event e) {             changeText();         }     }); 

As you can see, I am calling redraw on the composite after I add a new element. Also, I have verified that after the call to dispose, testCell.getChildren().length returns 0, as expected, and when I create a new label, I get the same expression to return 1, verifying that the new element is indeed getting added to its parent composite container Am I missing something here ?

回答1:

In the changeText() function, the

testCell.redraw();

line should be replaced by

testCell.layout();

Or, if you want to resize it correctly you should use

shell.layout();.



回答2:

I would say add a selectionListener on the label.

.addSelectionListener(new SelectionAdapter() {     @Override     public void widgetSelected(final SelectionEvent e) {         //Change text by Label.setText();     } } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!