Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How would I do this in Java? (checkboxes/options)?

I am trying to allow a user to select any number of 5 checkboxes before hitting an OK button to continue on with the program, which will behave accordingly depending on which ones were selected.

For a simple example, let's say I make a short program where I ask a user to select checkboxes, and I will display a message with certain strings out of a set of 5, but only ones corresponding to the selected boxes will be shown. Can anyone tell me how I can do this?

(i.e., let's say I have 5 strings, "Hello", "Pumpkin", "Spice" "42", "Goodbye". I would display the string "Pumpkin" in a message only if the second checkbox was previously selected, and "42" only if the fourth one was. These checkboxes would be selected previously by a user, and they would have like an "OK" button or something to display the message when they are ready. I need some way that a user can choose more than one option in a choice while waiting to continue with a program until the user is ready.)

3 Answers

Relevance
  • 6 years ago

    About selecting any number of boxes: That's how check boxes work in almost any GUI, Java Swing included.

    In your actionPerformed method for the button, check the .isSelected() state of each JCheckBox item:

    String output = "", sep="";

    if (box1.isSelected()) {

    .... output = output + sep + "Hello";

    .... sep = ", ";

    }

    if (box2.isSelected()) {

    .... output = output + sep + "Pumpkin";

    .... sep = ", ";

    }

    ...and so on. At the end, output is a comma-separated list of the selected items. The "sep" variable handling there shows how I like to manage putting commas only between entries when there are two or more to print.

  • 6 years ago

    All you need to do this job is Microsoft InfoPath 2010 or newer. Java contains security flaws that might cause security issues.

    Microsoft InfoPath is part of the Microsoft Office package, which includes the most common Microsoft Word.

  • John
    Lv 7
    6 years ago

    i am not understand what does it mean "only if the second checkbox was previously selected" the checkbox cannot be checked and previously checked.

    try this....

    import java.awt.event.ActionEvent;

    import java.awt.event.ActionListener;

    import javax.swing.JButton;

    import javax.swing.JCheckBox;

    import javax.swing.JFrame;

    import javax.swing.JOptionPane;

    import javax.swing.JPanel;

    public class Program extends JFrame implements ActionListener {

    private JCheckBox[] chkOptions;

    private JButton btnOk;

    public static void main(String[] args) {

    Program app = new Program();

    app.setVisible(true);

    }

    public Program() {

    JPanel pane = new JPanel();

    String[] options = { "Hello", "Pumpkin", "Spice", "42", "Goodbye" };

    buildOption(pane, options);

    btnOk = new JButton("OK");

    btnOk.addActionListener(this);

    pane.add(btnOk);

    getContentPane().add(pane);

    setSize(300, 300);

    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);

    }

    private void buildOption(JPanel pane, String[] options) {

    chkOptions = new JCheckBox[options.length];

    for (int i = 0; i < options.length; i++) {

    chkOptions[i] = new JCheckBox(options[i]);

    pane.add(chkOptions[i]);

    }

    }

    @Override

    public void actionPerformed(ActionEvent ae) {

    String message = getOptionMessage();

    if (btnOk == ae.getSource()) {

    JOptionPane.showMessageDialog(null, message);

    }

    }

    private String getOptionMessage() {

    String message = "No checkbox was selected";

    for (int i = chkOptions.length - 1; i >= 0; i--) {

    if (chkOptions[i].isSelected()) {

    message = String.format("The checkbox \"%s\" was selected",

    chkOptions[i].getText());

    break;

    }

    }

    return message;

    }

    }

Still have questions? Get your answers by asking now.