Java: JOptionPane Radio Buttons

I am working on a simple program to help me calculate eLiquid mixing material. I'm trying to add radio buttons to the JOptionPane.showInputDialog, but I can't seem to link them together. When I run the program, nothing comes up. That is all I have:

JRadioButton nicSelect = new JRadioButton("What is the target Nicotine level? ");
        JRadioButton b1 = new JRadioButton("0");
        JRadioButton b2 = new JRadioButton("3");
        JRadioButton b3 = new JRadioButton("6");
        JRadioButton b4 = new JRadioButton("12");
        JRadioButton b5 = new JRadioButton("18");
        JRadioButton b6 = new JRadioButton("24");

      

+3


source to share


2 answers


As an alternative to using multiple, JRadioButton

you can provide a select interface via JComboBox

by passing a String array to the JOptionPane.showInputDialog:



String[] values = {"0", "3", "6", "12", "18", "24"};

Object selected = JOptionPane.showInputDialog(null, "What is the target Nicotine level?", "Selection", JOptionPane.DEFAULT_OPTION, null, values, "0");
if ( selected != null ){//null if the user cancels. 
    String selectedString = selected.toString();
    //do something
}else{
    System.out.println("User cancelled");
}

      

+7


source


You can create a custom panel and present any options you like, for example:



public class Test {

    public static void main(final String[] args) {
        final JPanel panel = new JPanel();
        final JRadioButton button1 = new JRadioButton("1");
        final JRadioButton button2 = new JRadioButton("2");

        panel.add(button1);
        panel.add(button2);

        JOptionPane.showMessageDialog(null, panel);
    }
}

      

+6


source







All Articles