Why don't newly created items in the Java SWT Selection listener display or trigger a paint event?

I have a problem with SWT (standard widget toolkit) and redrawing. Basically I want something to happen when I press a button, for example. new items will appear. For simplicity, let it be the text here.

However, when I click the button, nothing happens, the element does not appear, PaintEvent does not start. When I resize the window, all the elements suddenly appear (because it causes paintEvent / redraw).

Docs and blogs have reported that a drawing event is fired whenever something needs to be redrawn. I thought adding a new item to the application would fall into this category.

So the question is, am I doing something wrong here? Should I call layout () / redraw () manually to trigger a redraw? This seems tedious to me, and as I understand it, SWT handles this for me in most cases.

Oh, I am running Linux x64 with SWT 4.2.2. But we have it on almost any platform (as well as on the old SWT 3.7).

And here is my little sample code:

import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; import org.eclipse.swt.events.*; import org.eclipse.swt.layout.*; public class MainExample { public static void createText(Shell shell) { Text text = new Text(shell, SWT.SINGLE); text.setText("here is some fancy text"); } public static void main (String [] args) { Display display = new Display (); Shell shell = new Shell (display); Button button = new Button(shell, SWT.PUSH); button.setText("I am a button"); button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { System.out.println("Button Clicked"); // Why do these texts not show up and not trigger a Paint Event? createText(e.display.getActiveShell()); } }); createText(shell); shell.setLayout (new RowLayout()); shell.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { // just triggered in the beginning and with resizes System.out.println("Hooray we got a Paint event"); } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } } 

Please keep in mind that the point is not only to make this example work. This is a simple version of the problem for a much larger project :-)

Any help is appreciated!

+4
source share
1 answer

If you are using a layout, you must call Composite.layout () (or layout (true)) after adding a new child to the composite. The layout does not know (it does not fit the widget) until you call layout ().

By the way, if you change the size of the shell (composite), the layout automatically invalidates its internal caches and checks all the children, puts them. why the newly created text appears after resizing the shell.

+1
source

Source: https://habr.com/ru/post/1489899/


All Articles