0

I didnt group my jradiobuttons so that users can select multiple choices and i can store in the nodes array..but it only reads once. What is wrong with the code? Please enlighten me

private String[] showGUIForNodeDeletion() {

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(map.size(), 1));
        ButtonGroup btnGrp = new ButtonGroup();
        final String nodes[] = new String[10];
        Set<String> keySet = map.keySet();

        for (String name : keySet) {

            btnRadio = new JRadioButton(name);
            btnRadio.setActionCommand(map.get(name).x + "," + map.get(name).y + "," + name);
                        //btnGrp.add(btnRadio);
            panel.add(btnRadio);
        }

        btnRadio.addActionListener(new ActionListener() {
            int x = 0;

            public void actionPerformed(ActionEvent e) {

                nodes[x] = ((JRadioButton) e.getSource()).getActionCommand();
                System.out.println("Node counting " + x);
                x++;
            }
        });

        if (keySet.isEmpty()) {
            JOptionPane.showMessageDialog(AnotherGuiSample.this, "Work Space is empty", "Error", JOptionPane.ERROR_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(AnotherGuiSample.this, panel, "Select node to remove", JOptionPane.INFORMATION_MESSAGE);
        }
        for(int x = 0; x < nodes.length; x++ )
        System.out.println("node is " + nodes[x]);

        return nodes;
    }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user2064467
  • 375
  • 1
  • 4
  • 13
  • If your goal is to allow the user to select multiple items, use JCheckBox, not JRadioButton. Visual cues are important. Most people will assume only one radio button can be selected. You don't want to make users guess how something works, just like you wouldn't put a push-bar on a door that opens inward. – VGR Mar 24 '13 at 12:48

1 Answers1

2

your for loop code should be like this:

UPDATE

Set<String> rbSet = new TreeSet<String>();
for (String name : keySet) {

    btnRadio = new JRadioButton(name);
    btnRadio.setActionCommand(map.get(name).x + "," + map.get(name).y + "," + name);
    btnRadio.addActionListener( new ActionListener()
    {
        public void actionPerformed(ActionEvent evt)
        {
            JRadioButton obj = (JRadioButton)evt.getSource();
            if (obj.isSelected())
            {
                rbSet.add(obj.getActionCommand());
            }
            else 
            {
                rbSet.remove(obj.getActionCommand());
            }
        }
    });
    panel.add(btnRadio);
}
int counter = 0 ;
for (String action : rbSet )
{
    nodes[counter++] = action;
}

What was happening that , you was registering ActionListener with the last object created in for loop since you did it after the for loop. That's why it was firing only for the lat JRaioButton object created and added to the JPanel. You should have register ActionListener in for loop with each JRadioButton created within the loop. This makes the ActionEvent to fire for every JRadioButton you are adding to JPanel.

Vishal K
  • 12,976
  • 2
  • 27
  • 38