简体   繁体   中英

Printing text to a textfield in java

I am still working on that program where i asked a question about yesterday.

So i have this piece of code:

public static void main(String[] args) {

    Display display = Display.getDefault();
    Shell shlKoffieHalenApp = new Shell();
    shlKoffieHalenApp.setMinimumSize(new Point(610, 430));
    shlKoffieHalenApp.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
    shlKoffieHalenApp.setSize(610, 430);
    shlKoffieHalenApp.setText("Koffie Halen App");
    shlKoffieHalenApp.setLayout(null);

    List list = new List(shlKoffieHalenApp, SWT.BORDER);
    list.setFont(SWTResourceManager.getFont("Sans", 13, SWT.BOLD));
    list.setItems(new String[] {"Anne", "", "Bas", "", "Daan", "", "Nick", "", "Paul", "", "Peter", "", "Sebastien"});
    list.setBackground(SWTResourceManager.getColor(SWT.COLOR_GRAY));
    list.setBounds(10, 10, 113, 377);

    ButtonAnne = new Button(shlKoffieHalenApp, SWT.CHECK);
    ButtonAnne.setBounds(129, 17, 137, 16);
    ButtonAnne.setText("Aanwezig/Afwezig");

    ButtonBas = new Button(shlKoffieHalenApp, SWT.CHECK);
    ButtonBas.setBounds(129, 67, 137, 16);
    ButtonBas.setText("Aanwezig/Afwezig");

    ButtonDaan = new Button(shlKoffieHalenApp, SWT.CHECK);
    ButtonDaan.setBounds(129, 120, 137, 16);
    ButtonDaan.setText("Aanwezig/Afwezig");

    ButtonNick = new Button(shlKoffieHalenApp, SWT.CHECK);
    ButtonNick.setBounds(129, 173, 137, 16);
    ButtonNick.setText("Aanwezig/Afwezig");

    ButtonPaul = new Button(shlKoffieHalenApp, SWT.CHECK);
    ButtonPaul.setBounds(129, 224, 137, 16);
    ButtonPaul.setText("Aanwezig/Afwezig");

    ButtonPeter = new Button(shlKoffieHalenApp, SWT.CHECK);
    ButtonPeter.setBounds(129, 276, 137, 16);
    ButtonPeter.setText("Aanwezig/Afwezig");

    ButtonSebastien = new Button(shlKoffieHalenApp, SWT.CHECK);
    ButtonSebastien.setBounds(129, 329, 137, 16);
    ButtonSebastien.setText("Aanwezig/Afwezig");

    ButtonKoffie = new Button(shlKoffieHalenApp, SWT.NONE);
    ButtonKoffie.setBounds(394, 53, 121, 34);
    ButtonKoffie.setText("Klik voor koffie!");

    Answer = new Text(shlKoffieHalenApp, SWT.BORDER);
    Answer.setEditable(false);
    Answer.getText();
    Answer.setBounds(355, 106, 200, 30);

    shlKoffieHalenApp.open();
    shlKoffieHalenApp.layout();
    while (!shlKoffieHalenApp.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }

        Random generate = new Random();
        ArrayList<String> names = new ArrayList<String>(); {

        if(ButtonAnne.getSelection() == true) {
            names.add("Anne");
        }
        if(ButtonBas.getSelection() == true) {
            names.add("Bas");
        }
        if(ButtonDaan.getSelection() == true) {
            names.add("Daan");
        }
        if(ButtonNick.getSelection() == true) {
            names.add("Nick");
        }
        if(ButtonPaul.getSelection() == true) {
            names.add("Paul");
        }
        if(ButtonPeter.getSelection() == true) {
            names.add("Peter");
        }
        if(ButtonSebastien.getSelection() == true) {
            names.add("Sebastien");
        }   

            if(ButtonKoffie.getSelection() == true) {
                int randomIndex = generate.nextInt(names.size());

                Answer.setText(names.get(randomIndex) + " moet koffie halen!");                 

            }
        }           
    }
}

Answer is the textfield where i want to print the random name thats picked from the ArrayList. What am i missing here? I tried to creat a method but that did not work out as planned.

You should not put anything in the SWT event loop, user interfaces are event driven, you should not use polling.

You should add event listeners to the components, for example if you want to intercept when a button is pressed, you should add a SelectionListener to it:

buttonKoffie = new Button(shlKoffieHalenApp, SWT.NONE);
buttonKoffie.addSelectionListener(new SelectionListener() {
    @Override
    public void widgetDefaultSelected(SelectionEvent arg0) {    
    }

    @Override
    public void widgetSelected(SelectionEvent arg0) {

        // this method is called when the button is pressed 

        int randomIndex = generate.nextInt(names.size());
        answer.setText(names.get(randomIndex) + " moet koffie halen!");  
    }
});

Read the documentation of the various components to know which listeners you can add and how they behave.

The Button documentation also specifies that getSelection works only for CHECK , RADIO and TOGGLE buttons, it will always return false for PUSH buttons.

Another problem of putting code in the SWT event loop is that you are continuously re-creating the names array, so you will always find it empty. You should create it once and use event listeners of the check buttons to fill it.

Other recommendations:

  • adopt the proper Java naming convention (for example, the name of the variables should start with a lowercase letter)
  • use proper layouts instead of absolute positioning (you should never have to call setBounds )

Replace

Answer.setText(randomIndex + " moet koffie halen!");

with

Answer.setText(names.get(randomIndex) + " moet koffie halen!");

this lines of code:

 int randomIndex = generate.nextInt(names.size());
 Answer.setText(randomIndex + " moet koffie halen!");    

are setting in the textView only the random int you got, you need instead the random element in the list... then you need to use names.get(randomIndex)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM