Question

I'm making my first attempt at passing information between UI and code. I've posted what I've done so far but am having difficulty finding the proper way to pass information back out without appending the current text.

I understand this is beginner stuff and would also appreciate direction towards a tutorial geared specifically for passing information back and forth between UI and code.

public class UserInterface {
    private Shell shell;
    public static void main(){
        Display display = new Display();
        new UserInterface(display);
        display.dispose();      
    }

    public UserInterface(Display display){
        shell = new Shell(display);
        shell.setSize(400, 250);
        initUI();
        center(shell);
        shell.open();
        while(!shell.isDisposed()){
            if(!display.readAndDispatch()){
                display.sleep();
            }
        }
    }

    public void initUI(){
        final Text text = new Text(shell, SWT.SINGLE);
        text.setBounds(20, 20, 100, 18);

        Label lbl1 = new Label(shell, SWT.BEGINNING);
        lbl1.setBounds(20, 50, 500, 50);
        lbl1.setText("(1) Enter number.\n(2) Click next to enter another number\n(3) Click done to find LCM");

        final Text display = new Text(shell, SWT.SINGLE);
        display.setBounds(20, 110, 360, 100);

        Button button = new Button(shell, SWT.PUSH);
        button.setBounds(140, 16, 100, 35);
        button.setText("Button");

        button.addSelectionListener(new SelectionAdapter(){
            @Override
            public void widgetSelected(SelectionEvent e){
            //display.setText("");
            String output = text.getText();
            display.setText(output);
            }
        });
    }

    public void center(Shell shell){
        Rectangle bds = shell.getMonitor().getBounds();
        Point p = shell.getSize();
        int xPos = (bds.width - p.x) / 2;
        int yPos = (bds.height - p.y) / 2;
        shell.setBounds(xPos, yPos, p.x, p.y);      
    }
}

No correct solution

OTHER TIPS

This code should give you an idea. It has two Text widgets. One of them will act as a console (text appended) and the other one will show the last text you added to the console. Just click the button to add text:

public static void main(String[] args)
{
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new FillLayout(SWT.VERTICAL));

    final Text text = new Text(shell, SWT.MULTI | SWT.WRAP);
    text.setEditable(false);
    final Text lastAppend = new Text(shell, SWT.BORDER);
    lastAppend.setEditable(false);

    Button button = new Button(shell,SWT.PUSH);
    button.setText("Add new line");

    button.addListener(SWT.Selection, new Listener()
    {
        private int counter = 0;
        @Override
        public void handleEvent(Event arg0)
        {
            String append = "TEST " + counter++ + "\n";
            text.append(append);
            lastAppend.setText(append);
        }
    });

    shell.pack();
    shell.setSize(300, 300);
    shell.open();

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

Looks like this:

enter image description here

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