Code :HashMap<String, String> map = new HashMap<>(); map.put("Red", FF0000); map.put("Blue", 0000FF); map.put("Green", 00FF00); map.put("Orange", FF7538); map.put("Black", 000000);
Why won't the HashMap store the hexidecimal?
Printable View
Code :HashMap<String, String> map = new HashMap<>(); map.put("Red", FF0000); map.put("Blue", 0000FF); map.put("Green", 00FF00); map.put("Orange", FF7538); map.put("Black", 000000);
Why won't the HashMap store the hexidecimal?
Because Strings have to be enclosed by literals. It's taking them as variables.
Try this
map.put("Red", "FF0000");
If i called upon red then would FF0000 still show up such as: If i entered System.out.println("The hexadecimal for red is" + map.get("Red").StringValue()); would that make FF0000 show?
To make FF0000 show, do something like this
System.out.println(map.get("Red"));
The get method returns the value, if there is one, associated with that key.
Code :final HashMap<String, String> map = new HashMap<>(); map.put("Red", "FF0000"); map.put("Blue", "0000FF"); red.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { getContentPane().setBackground(Color.red); display.setText("The hexadecimal for red is " + map.get("Red"));
the background color still changes correctly but the value (in this case FF0000) does not display
I was able to make something similar work, though I had the HashMap be a class variable rather than a final variable like you had it.
Code java:import java.util.HashMap; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JLabel; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton; import java.awt.GridLayout; import java.awt.Color; public class TestingThis extends JFrame { private HashMap<String,String> map; private JButton red, green, blue; private JLabel display; private JPanel contentPane; public TestingThis() { super("This is a test."); contentPane = new JPanel(); setContentPane(contentPane); getContentPane().setLayout(new GridLayout(4,1)); display = new JLabel(); getContentPane().add(display); red = new JButton("Red"); getContentPane().add(red); map = new HashMap<String,String>(); map.put("Red", "FF0000"); map.put("Green", "00FF00"); map.put("Blue", "0000FF"); red.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { getContentPane().setBackground(Color.RED); display.setText("The hexidecimal for red is " + map.get("Red")); }}); green = new JButton("Green"); getContentPane().add(green); green.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { getContentPane().setBackground(Color.GREEN); display.setText("The hexidecimal for green is " + map.get("Green")); }}); blue = new JButton("Blue"); getContentPane().add(blue); blue.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { getContentPane().setBackground(Color.BLUE); display.setText("The hexidecimal for blue is " + map.get("Blue")); }}); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[] args) { new TestingThis(); } }
Code :import javax.swing.*; import java.util.*; import java.awt.*; import java.awt.event.*; import java.awt.color.*; import javax.swing.border.*; public class Test extends JFrame{ private JRadioButton red, blue; private JLabel display; private HashMap<String, String> map = new HashMap<>(); public static void main(String[] args){ Test frame = new Test(); frame.setTitle("Colors"); frame.setSize(700, 300); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public Test(){ JPanel p1 = new JPanel(); p1.setLayout(new GridLayout(1,5)); p1.add(red = new JRadioButton("Red")); p1.add(blue = new JRadioButton("Blue")); add(p1, BorderLayout.NORTH); p1.setBorder(new TitledBorder("Group 1")); ButtonGroup group = new ButtonGroup(); group.add(red); group.add(blue); map.put("Red", "FF0000"); map.put("Blue", "0000FF"); red.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { getContentPane().setBackground(Color.red); display.setText("The hexadecimal for red is #" + map.get("Red")); } }); }
It is showing no errors but it is not running correctly. background still changes color but the text does not appear.
Try initializing the variable "display".
Then add it to your GUI.
Also, try setting p1 to be your content pane or something.
Code java:import javax.swing.*; import java.util.*; import java.awt.*; import java.awt.event.*; import java.awt.color.*; import javax.swing.border.*; public class Test2 extends JFrame{ private JRadioButton red, blue; private JLabel display; private HashMap<String, String> map = new HashMap<String,String>(); public static void main(String[] args){ Test2 frame = new Test2(); frame.setTitle("Colors"); frame.setSize(700, 300); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public Test2(){ JPanel p1 = new JPanel(); p1.setLayout(new GridLayout(1,5)); p1.add(red = new JRadioButton("Red")); p1.add(blue = new JRadioButton("Blue")); p1.setBorder(new TitledBorder("Group 1")); ButtonGroup group = new ButtonGroup(); group.add(red); group.add(blue); display = new JLabel(); setContentPane(p1); p1.add(display); map.put("Red", "FF0000"); map.put("Blue", "0000FF"); red.addActionListener( new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { getContentPane().setBackground(Color.red); display.setText("The hexadecimal for red is #" + map.get("Red")); } }); } }
I have tried the getContentPane().add(display); but still having the same issue.
This code below, a slight alteration of your code, worked for me.
Code java:import javax.swing.*; import java.util.*; import java.awt.*; import java.awt.event.*; import java.awt.color.*; import javax.swing.border.*; public class Test2 extends JFrame{ private JRadioButton red, blue; private JLabel display; private HashMap<String, String> map = new HashMap<String,String>(); public static void main(String[] args){ Test2 frame = new Test2(); frame.setTitle("Colors"); frame.setSize(700, 300); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public Test2(){ JPanel p1 = new JPanel(); p1.setLayout(new GridLayout(1,5)); p1.add(red = new JRadioButton("Red")); p1.add(blue = new JRadioButton("Blue")); p1.setBorder(new TitledBorder("Group 1")); ButtonGroup group = new ButtonGroup(); group.add(red); group.add(blue); display = new JLabel(); setContentPane(p1); p1.add(display); map.put("Red", "FF0000"); map.put("Blue", "0000FF"); red.addActionListener( new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { getContentPane().setBackground(Color.red); display.setText("The hexadecimal for red is #" + map.get("Red")); } }); blue.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { getContentPane().setBackground(Color.blue); display.setText("The hexadecimal for blue is #" + map.get("Blue")); } }); } }
Attachment 1244Attachment 1245
woooowwwww lol. I never set display = new JLabel. Thank you for helping me find this error. It is always something small
The ActionListener. It will call the setText() method, as it appears you already did. It just wasn't doing it because the JLabel had been null.