Darren Estcourt Darren Estcourt - 5 months ago 15
Java Question

How to display JRadioButton's array text

I have googled this several times and cannot find an answer. I have an initialized array of JRadioButtons. I want to display the JRadioButton's initialized value, whenever the radiobutton is clicked on and the user clicks my "OK" button. Currently my JOptionPane just displays a memory address. But including the element of the array means it won't compile. The error is "no suitable method found for toString(jradiobutton). Please can someone point me in the right direction? I am a newb.

Code snippet is here:

import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.util.Arrays;

public class NewGameGUI implements ActionListener
{
private JRadioButton[] rb = new JRadioButton[20];
JFrame f3;
JButton b;
JLabel label1;
public NewGameGUI()
{
label1 = new JLabel("Please choose a team");
f3 = new JFrame("Ballon d'or");
f3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
rb[0] = new JRadioButton("Arsenal");
rb[1] = new JRadioButton("Aston Villa");
rb[2] = new JRadioButton("Bournemouth");
rb[3] = new JRadioButton("Chelsea");
rb[4] = new JRadioButton("Crystal Palace");
rb[5] = new JRadioButton("Everton");
rb[6] = new JRadioButton("Leicester City");
rb[7] = new JRadioButton("Liverpool");
rb[8] = new JRadioButton("Manchester United");
rb[9] = new JRadioButton("Manchester City");
rb[10] = new JRadioButton("Newcastle United");
rb[11] = new JRadioButton("Norwich City");
rb[12] = new JRadioButton("Southampton");
rb[13] = new JRadioButton("Stoke City");
rb[14] = new JRadioButton("Sunderland");
rb[15] = new JRadioButton("Swansea City");
rb[16] = new JRadioButton("Tottenham Hotspur");
rb[17] = new JRadioButton("Watford");
rb[18] = new JRadioButton("West Brom");
rb[19] = new JRadioButton("West Ham");


label1.setBounds(50,25,200,30);
f3.add(label1);

rb[0].setBounds(50,100,170,30);
rb[1].setBounds(50,150,170,30);
rb[2].setBounds(50,200,170,30);

ButtonGroup bg=new ButtonGroup();
bg.add(rb[0]);
bg.add(rb[1]);
bg.add(rb[2]);
bg.add(rb[3]);
bg.add(rb[4]);
bg.add(rb[5]);
bg.add(rb[6]);
bg.add(rb[7]);
bg.add(rb[8]);
bg.add(rb[9]);
bg.add(rb[10]);
bg.add(rb[11]);
bg.add(rb[12]);
bg.add(rb[13]);
bg.add(rb[14]);
bg.add(rb[15]);
bg.add(rb[16]);
bg.add(rb[17]);
bg.add(rb[18]);
bg.add(rb[19]);

b = new JButton("OK");
b.setBounds(200,250,180,30);
b.addActionListener(this);
f3.add(rb[0]);
f3.add(rb[1]);
f3.add(rb[2]);
f3.add(rb[3]);
f3.add(rb[4]);
f3.add(rb[5]);
f3.add(rb[6]);
f3.add(rb[7]);
f3.add(rb[8]);
f3.add(rb[9]);
f3.add(rb[10]);
f3.add(rb[11]);
f3.add(rb[12]);
f3.add(rb[13]);
f3.add(rb[14]);
f3.add(rb[15]);
f3.add(rb[16]);
f3.add(rb[17]);
f3.add(rb[18]);
f3.add(rb[19]);
f3.add(b);


f3.setSize(500,500);
f3.setLayout(null);
f3.setVisible(true);

// NewGameGUI test = new NewGameGUI();
// test.setFrame(f3);

}

public void setFrame(JFrame f3){
this.f3 = f3;
}
public JFrame getFrame(){

return f3;
}
public void actionPerformed(ActionEvent e)
{
if(rb[0].isSelected())
{
// f3.dispose();
f3.dispose();

JOptionPane.showMessageDialog(f3,"You chose : " + Arrays.toString(rb[0]));
}
if(rb[1].isSelected())
{
JOptionPane.showMessageDialog(f3,"This works too!");
}
if(rb[2].isSelected())
{
System.exit(0);
}

}


}

Answer

Change this

JOptionPane.showMessageDialog(f3,"You chose : " + Arrays.toString(rb[0]));

to

JOptionPane.showMessageDialog(f3,"You chose : " + rb[0].getText());

This should retrieve the text of the JRadioButton instead of the "memory address".