Results 1 to 7 of 7
Thread: displaying values from HashMap
- 05-13-2012, 03:41 AM #1
Member
- Join Date
- May 2012
- Posts
- 2
- Rep Power
- 0
displaying values from HashMap
The program changes the background color but does not display the hexadecimal.Java 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")); } }); }Last edited by Debra; 05-13-2012 at 04:35 AM.
- 05-13-2012, 03:59 AM #2
Re: displaying values from HashMap
We haven't a clue about the context of that code. To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-13-2012, 04:21 AM #3
Member
- Join Date
- May 2012
- Posts
- 2
- Rep Power
- 0
Re: displaying values from HashMap
The program changes the background color but does not display the hexadecimal.Java 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")); } }); }
- 05-13-2012, 04:21 AM #4
Re: displaying values from HashMap
I agree with Daryl, buti think you want something like:
Also, it only needs to be static if the map is declared and to be used in a static context, that is if the containing class isn't being instantiated.Java Code:map.get("Red");
- 05-13-2012, 05:07 AM #5
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: displaying values from HashMap
Actually your code produce a java.lang.NullPointerException exception because the display JLabel was never instantiated in your code.
Website: Learn Java by Examples
- 05-13-2012, 06:29 AM #6
Re: displaying values from HashMap
Debra, please don't edit your posts after there are responses (except maybe to correct a spelling mistake), as that takes a response out of context and makes the thread difficult to follow.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-13-2012, 06:30 AM #7
Re: displaying values from HashMap
Also learn that Swing constructors and methods, with very few exceptions, must be invoked on the EDT and not on the Main thread.
Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
HashMap help? Values keep overriding..?
By sim18 in forum New To JavaReplies: 2Last Post: 05-06-2012, 12:59 PM -
Cannot return values from hashmap
By uhertz in forum New To JavaReplies: 1Last Post: 06-17-2011, 04:16 PM -
Displaying HashMap in a JFrame
By ankit1801 in forum New To JavaReplies: 1Last Post: 05-19-2011, 07:54 AM -
how to get the values from hashmap
By baktha.thalapathy in forum New To JavaReplies: 5Last Post: 05-25-2010, 02:12 PM -
HashMap contains all values but doesn't show all values
By xcallmejudasx in forum New To JavaReplies: 3Last Post: 05-10-2009, 11:35 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks