Note that there are some explanatory texts on larger screens.

plurals
  1. POReferencing JRadioButtons in ActionListener
    primarykey
    data
    text
    <p>I am trying to write a GUI that displays a hex value and it's associated color based on whichever JRadioButton is selected. My ActionListener looks up a hashMap entry that stores the individual radio buttons as an object key and it's associated hex value (as a string).</p> <p>I can get the hex values with the hashMap.get pieces, but how do I get the action listener to reference any JRadioButton rather than just 'jrbBlue' or whatever is hard-coded? </p> <p>Eclipse doesn't like it if I put JRadioButton.addActionListener(Error - "Cannot make a static reference to the non-static method addActionListener(ActionListener) from the type AbstractButton") or jpRadioButton.addActionListener, my JPanel for the buttons (it wants to change addActionListener to addComponentListener and a bunch of other addWhatevers, none of which work).</p> <p>I realize there are other ways to write this whole thing, but I'm tying to work with what I have and I'm still learning.</p> <p>Thanks in advance.</p> <pre><code>import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.border.*; public class Colors extends JFrame { static Map&lt;Object, String&gt; hashMap = new HashMap&lt;Object, String&gt;(); String hex = "Hex Value"; //---- The JLabel message (hex value of the color selected) private JLabel jlblMessage = new JLabel(hex, JLabel.CENTER); //---- Create the radio buttons private static JRadioButton jrbBlue = new JRadioButton("Blue"); private static JRadioButton jrbPurplish = new JRadioButton("Purplish"); private static JRadioButton jrbRed = new JRadioButton("Red"); private static JRadioButton jrbYellow = new JRadioButton("Yellow"); private static JRadioButton jrbGreen = new JRadioButton("Green"); private static JRadioButton jrbOrange = new JRadioButton("Orange"); private static JRadioButton jrbCyan = new JRadioButton("Cyan"); private static JRadioButton jrbCoral = new JRadioButton("Coral"); private static JRadioButton jrbFuscia = new JRadioButton("Fuscia"); private static JRadioButton jrbViolet = new JRadioButton("Violet"); private static JRadioButton jrbDodgerBlue = new JRadioButton("Dodger Blue"); private static JRadioButton jrbGrey = new JRadioButton("Grey"); private static JRadioButton jrbWhite = new JRadioButton("White"); private static JRadioButton jrbCrimson = new JRadioButton("Crimson"); private static JRadioButton jrbDarkOrchid = new JRadioButton("Dark Orchid"); private static JRadioButton jrbFirebrick = new JRadioButton("Firebrick"); private static JRadioButton jrbHotPink = new JRadioButton("Hot Pink"); private static JRadioButton jrbMaroon = new JRadioButton("Maroon"); private static JRadioButton jrbDarkBlue = new JRadioButton("Dark Blue"); private static JRadioButton jrbTurquoise = new JRadioButton("Turquoise"); public Colors() { //---- JLabel placement jlblMessage.setBorder(new LineBorder(Color.BLACK, 2)); add(jlblMessage, BorderLayout.CENTER); //---- Add the radio buttons to the JPanel JPanel jpRadioButtons = new JPanel(); jpRadioButtons.setLayout(new GridLayout(3, 1)); jpRadioButtons.add(jrbBlue); jpRadioButtons.add(jrbPurplish); jpRadioButtons.add(jrbRed); jpRadioButtons.add(jrbYellow); jpRadioButtons.add(jrbGreen); jpRadioButtons.add(jrbOrange); jpRadioButtons.add(jrbCyan); jpRadioButtons.add(jrbCoral); jpRadioButtons.add(jrbFuscia); jpRadioButtons.add(jrbViolet); jpRadioButtons.add(jrbDodgerBlue); jpRadioButtons.add(jrbGrey); jpRadioButtons.add(jrbWhite); jpRadioButtons.add(jrbCrimson); jpRadioButtons.add(jrbDarkOrchid); jpRadioButtons.add(jrbFirebrick); jpRadioButtons.add(jrbHotPink); jpRadioButtons.add(jrbMaroon); jpRadioButtons.add(jrbDarkBlue); jpRadioButtons.add(jrbTurquoise); add(jpRadioButtons, BorderLayout.WEST); //---- Add all the buttons to the same group ButtonGroup group = new ButtonGroup(); group.add(jrbBlue); group.add(jrbPurplish); group.add(jrbRed); group.add(jrbYellow); group.add(jrbGreen); group.add(jrbOrange); group.add(jrbCyan); group.add(jrbCoral); group.add(jrbFuscia); group.add(jrbViolet); group.add(jrbDodgerBlue); group.add(jrbGrey); group.add(jrbWhite); group.add(jrbCrimson); group.add(jrbDarkOrchid); group.add(jrbFirebrick); group.add(jrbHotPink); group.add(jrbMaroon); group.add(jrbDarkBlue); group.add(jrbTurquoise); //jrbBlue.setSelected(true); //jlblMessage.setForeground(Color.decode("#0000FF")); //---- Action Listener jrbBlue.addActionListener(new ActionListener() { //&lt;---- How to Reference whichever button is selected? @Override public void actionPerformed(ActionEvent e) { jlblMessage.setForeground(Color.decode("#000000")); jlblMessage.setText(hashMap.get((JRadioButton)e.getSource())); getContentPane().setBackground(Color.decode(hashMap.get((JRadioButton)e.getSource()))); } }); } public static void main(String[] args) { Colors frame = new Colors(); frame.pack(); frame.setTitle("Colors"); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.setSize(900,300); //---- Color library map hashMap.put(jrbBlue, "#0000FF"); hashMap.put(jrbPurplish, "#DF01D7"); hashMap.put(jrbRed, "#FF0000"); hashMap.put(jrbYellow, "#FFFF00"); hashMap.put(jrbGreen, "#00FF00"); hashMap.put(jrbOrange, "#FF8C00"); hashMap.put(jrbCyan, "#00FFFF"); hashMap.put(jrbCoral, "#FF7F50"); hashMap.put(jrbFuscia, "#FF00FF"); hashMap.put(jrbViolet, "#00FF00"); hashMap.put(jrbDodgerBlue, "#1E90FF"); hashMap.put(jrbGrey, "#C0C0C0"); hashMap.put(jrbWhite, "#FFFFFF"); hashMap.put(jrbCrimson, "#DC143C"); hashMap.put(jrbDarkOrchid, "#9932CC"); hashMap.put(jrbFirebrick, "#B22222"); hashMap.put(jrbHotPink, "#FF69B4"); hashMap.put(jrbDarkBlue, "#00008B"); hashMap.put(jrbMaroon, "#800000"); hashMap.put(jrbTurquoise, "#48D1CC"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload