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?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
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.
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.
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(); } }
Last edited by javapenguin; May 12th, 2012 at 08:57 PM.
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.
Last edited by georger55; May 12th, 2012 at 09:10 PM.
Try initializing the variable "display".
Then add it to your GUI.
Also, try setting p1 to be your content pane or something.
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")); } }); } }
Last edited by javapenguin; May 12th, 2012 at 09:30 PM.
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.
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")); } }); } }
Image1.jpgImage2.jpg
georger55 (May 12th, 2012)
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.