Question

Im using windowbuilder to build GUI for my application. I have created a SWT based shell layout for my GUI. I have a textbox in the GUI. I want to get Integer values from the textbox when used as the GUI. I see that the textbox has option only to get Object or Text. Is there any option to get Integer from textbox? If so, how do I do it?

The code I have from the Source of windowbuilder is as follows:

import org.eclipse.swt.SWT;
import otf.EnterLeaveHandler;
public class test {

    protected Shell shell;
    private Text text;
    private final FormToolkit formToolkit = new FormToolkit(
            Display.getDefault());

    public static void main(String[] args) {
        try {
            test window = new test();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void open() {
        Display display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    protected void createContents() {
        shell = new Shell();
        shell.setSize(545, 419);
        shell.setText("SWT Application");
        shell.setLayout(null);

        Button btnNewButton = new Button(shell, SWT.NONE);
        btnNewButton.setBounds(213, 243, 75, 25);
        btnNewButton.setText("Ok");

        Label lblNewLabel = new Label(shell, SWT.NONE);
        lblNewLabel.setBounds(190, 87, 137, 15);
        lblNewLabel.setText("Enter function name");

        text = new Text(shell, SWT.BORDER);
        text.setBounds(213, 129, 76, 21);


    final Label lblNewLabel_1 = formToolkit.createLabel(shell, "New Label", SWT.NONE);
    lblNewLabel_1.setBounds(72, 184, 55, 15);

    final EnterLeaveHandler ne = new EnterLeaveHandler();

    btnNewButton.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseDoubleClick(MouseEvent e) {

            Integer getvalue = Integer.parseInt(text.getText());
            ne.cpuid_filter(getvalue);

        }
    });
    }


 }
Was it helpful?

Solution

You will have to parse it from the String:

try
{
    int actualValue = Integer.parseInt(text.getText());

    System.out.println(actualValue);
}
catch (NumberFormatException e)
{
    System.out.println("Not a number");
}

Be aware that this might throw a NumberFormatException if the text cannot be cast to an integer.

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