javaloopsswingjradiobutton

Creating GUI with sub menus


I'm trying to build a GUI which has submenus and may return to main menu and options, so far I've created a main menu but when I try to click the boxes nothing opens also only 4 items are showing when I have coded 5 items. Does anyone know a way around this? Also If anyone knows how to create different boxes not check boxes. Many thanks

public class CoinSorterGUI {

public static void main(String[] args) {
    final JFrame frame=new JFrame("CoinSorterGUI");
    
    class MyItemListener implements ItemListener{
        
        public void itemStateChanged(ItemEvent ev) {
            boolean selected = (ev.getStateChange()== ItemEvent.SELECTED);
            AbstractButton button =(AbstractButton) ev.getItemSelectable();
            String command = button.getActionCommand();
            if (selected) {
                int messageType = -1;
                String message = "";
                if (command.equals("CoinCalculator")) {
                    messageType = JOptionPane.INFORMATION_MESSAGE;
                    message = "Welcome to the CoinCalculator";
            }else if (command.equals("Multiple CoinCalculator")) {
                    messageType = JOptionPane.INFORMATION_MESSAGE;
                    message = "Welcome to the Multiple CoinCalculator";
            }else if (command.equals("Print Coin List")) {
                    messageType = JOptionPane.INFORMATION_MESSAGE;
                    message = "Current coin denominations are; £2, £1, 50p. 20p and 10p";
            }else if (command.equals("Set details")) {
                messageType = JOptionPane.INFORMATION_MESSAGE;
                message = " ** Set details Sub Menu ** "
                        + "    1 -  Set Currency       "
                        + "    2 -  Set minimum input value"
                        + "    3 -  Set Maximum input value"
                        + "    4 -  Return to main menu";
            }else if (command.equals("Display program configuarations")) {
                messageType = JOptionPane.INFORMATION_MESSAGE;
                message = "The current currency is £, the minimum input value is 0 and the maximum input value is 10000";
            }
                JOptionPane.showMessageDialog(frame,
                        message,
                        "CoinSorter",
                        messageType);
            }
                    
          }
        }

        JRadioButton r1 = new JRadioButton("CoinCalculator");
        r1.setActionCommand("Welcome to the CoinCalculator");
 
        JRadioButton r2 = new JRadioButton("Multiple CoinCalculator");
        r2.setActionCommand("Welcome to the Multiple CoinCalculator");
 
        JRadioButton r3 = new JRadioButton("Print Coin List");
        r3.setActionCommand("Current coin denominations are; £2, £1, 50p. 20p and 10p");
 
        JRadioButton r4 = new JRadioButton("Set details");
        r4.setActionCommand(" ** Set details Sub Menu ** "
                + "    1 -  Set Currency       "
                + "    2 -  Set minimum input value"
                + "    3 -  Set Maximum input value"
                + "    4 -  Return to main menu");
        JRadioButton r5 = new JRadioButton("Display program configuarations");
        r5.setActionCommand("The current currency is £, the minimum input value is 0 and the maximum input value is 10000");
    
        final ButtonGroup group = new ButtonGroup();
        group.add(r1);
        group.add(r2);
        group.add(r3);
        group.add(r4);
        group.add(r5);
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);
        Container cont = frame.getContentPane();
 
        cont.setLayout(new GridLayout(0, 1));
        cont.add(new JLabel("Please choose from the options: "));
        cont.add(r1);
        cont.add(r2);
        cont.add(r3);
        cont.add(r4);
 
        frame.setVisible(true);

}
}

Solution

  • only 4 items are showing when I have coded 5 items

        cont.add(r4);
    
        frame.setVisible(true);
    

    Should be:

        cont.add(r4);
        cont.add(r5); // add ALL the buttons!
    
        frame.setVisible(true);