0

After choose.setVisible(true) I would like first to be able to choose and after pressing the ok button to continue execution. The below code shows the chooser and continues without waiting.

   static class box extends JFrame {
        Checkbox cboxtps = new Checkbox("Grf1", false);
        Checkbox cboxrspt = new Checkbox("Grf2", false);
        JLabel lblQts = new JLabel("Please select graphs");
        JButton btn1 = new JButton("Go");

        public box(String str) {
            super(str);
            setLayout(new GridLayout(4, 1));
            add(lblQts);
            add(cboxtps);
            add(cboxrspt);
            add(btn1);
            btn1.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e)
                {
                    //Execute when button is pressed
                    System.out.println("You clicked the button");
                }
            });
        }
        }


    public static void main(String args[]) {
        box choose = new box("Select Graphs");
        choose.setSize(300, 150);
        choose.pack();
        choose.setVisible(true);


        List<File> filepaths = fileselect();
        list = Splitter(filepaths);
}
Deividi Cavarzan
  • 10,034
  • 13
  • 66
  • 80
A D
  • 79
  • 1
  • 10

1 Answers1

0

Since you are working asynchronously here, you would have to put the code after choose.setVisible(true) into the actionPerformed callback like so:

public void actionPerformed(ActionEvent e) 
{
    System.out.println("You clicked the button");
    List<File> filepaths = fileselect();
    list = Splitter(filepaths);
}

In general, when working with a lot of callbacks it is the best to define helper functions to avoid deep nesting.

Felix Gerber
  • 1,615
  • 3
  • 30
  • 40
Florian
  • 712
  • 5
  • 18