Question

I'm trying to implement a simple SWT dialog, which has a text field and if there is something wrong in the entered text of the text field, I want to show this error in a label at the bottom of the text field.

So in the beginning you see only the text field, which is filling the whole window. If I show the label, I create it and want to show it at the bottom of the text field.

My problem is, that you only see this label after resizing the window (all the same whether I make it smaller or bigger). So which method is called in the shell, when I resize the window?

I tried:

shell.layout();
shell.update();
shell.pack(); 
shell.redraw();
textField.update();
textField.redraw();

But nothing changed.

So the only possibility that worked was to take:

Point oldSize = shell.getSize();
oldSize.x += 1;
shell.setSize(oldSize);
oldSize.x -= 1;
shell.setSize(oldSize);

But this is not an acceptable solution, because the user notices the small changing of the size of the shell. My question is similar to this: Java SWT added Label not visible (also the application looks very similar), but in this solution with pack and setting the old size, the user notices the resizing, and this is very unsatisfying for me.

So do you know, which method I could call to update the window, like when I resize? But in this way the size of the window stays the same?

Thank you!

Edit: If I create the label in the beginning ans set it as invisible, you notice that there is something: Example: this picture shows the window part without creating the label and setting it to invisible Window part without creating label and setting it to invisible

This picture shows the window part with creating the label and setting it to invisible: Window part with creating label and setting it to invisible

I would prefer version 1.

Was it helpful?

Solution 2

The best thing to do in such cases is to create the label in the beginning and just keep it hidden (Label#setVisible(false)), and enable the visibility when the error message needs to be shown.

Update1: Additionally you can also add layout data on the label and set it to exclude from layout. This doesn't take up empty space.

errorLabel = new Label(parent, SWT.NONE);
errorLabel.setVisible(false);
GridData gd = new GridData();
gd.exclude = true;
errorLabel.setLayoutData(gd);

And when we want to show the label, we can do the following:

errorLabel.setVisible(true);
((GridData) errorLabel.getLayoutData()).exclude = false;
errorLabel.getParent().layout();

OTHER TIPS

Please try below. I hope you are looking for below kind of functionality. you can modify keypress listener based on your need.

public class demo {
    static Label lbl;
    static Shell shell;
    public static void main(String[] chr){
        Display display = new Display();


        shell = new Shell(display, SWT.SHELL_TRIM);
        final Text txt = new Text(shell,SWT.BORDER);


        shell.setLayout(new FillLayout(SWT.VERTICAL));
        shell.pack();
        shell.layout(true);
        shell.open();

        txt.addKeyListener(new KeyListener() {

            @Override
            public void keyReleased(KeyEvent arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void keyPressed(KeyEvent arg0) {
                // TODO Auto-generated method stub


                if (lbl!=null)lbl.dispose();
                if(txt.getText().length()<5){
                    lbl= new Label(shell,SWT.NONE);
                    lbl.setText("text should be more than 5 character");

                }
                shell.redraw();
                shell.pack();
                shell.layout(true); 

            }
        });
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }
    }
}

You should call layout(true) where the boolean param is used to indicate the content is changed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top