0

I am attempting to create a JFrame with multiple check boxes that the user can select (changing values in an array of Boolean) as well as a submit button. When the Submit button is pressed, the method should return the array of boolean. However, I can't figure out how to get the code to wait for the submission to be pressed. I tried a couple very poorly designed solutions with booleans and a while loop, but those didn't consistently work, nor are they the correct way to do this I'm fairly sure.

Code is below:

public class UniversalUtility {
    public void uI() {
        boolean[] selections = utilitySelection();
        for (boolean i : selections) {
            System.out.println(i);
        }
    }


    private boolean submitted;

    public boolean[] utilitySelection() {
        JFrame frame = new JFrame("Utility Selector");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel(new GridLayout(0,1));
        Border border = BorderFactory.createTitledBorder("Utilities");
        panel.setBorder(border);
        boolean[] selections = new boolean[4];

        JCheckBox check1 = new JCheckBox("VS to VCF Friendly ");
        panel.add(check1);
        check1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                selections[0] = true;
            }
        });
        JCheckBox check2 = new JCheckBox("HGMD Fix");
        panel.add(check2);
        check2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                selections[1] = true;
            }
        });
        JCheckBox check3 = new JCheckBox("CADD Preparer");
        panel.add(check3);
        check3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                selections[2] = true;
            }
        });
        JCheckBox check4 = new JCheckBox("Line Chunks");
        panel.add(check4);
        check4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                selections[3] = true;
            }
        });

        JButton button = new JButton("Submit");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                submitted = true;
                frame.dispose();
            }
        });

        Container contentPane = frame.getContentPane();
        contentPane.add(panel, BorderLayout.CENTER);
        contentPane.add(button, BorderLayout.SOUTH);
        frame.setSize(300,200);
        frame.setVisible(true);
        while (!submitted) {}
        return selections; 
    }

    public static void main(String[] args) {
        (new UniversalUtility()).uI();
    }
}

Thanks in advance for your help.

Lukas Vlahos
  • 41
  • 1
  • 5

0 Answers0