Results 1 to 7 of 7
  1. #1
    Debra is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default displaying values from HashMap

    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"));
      
                }
        });
    }
    The program changes the background color but does not display the hexadecimal.
    Last edited by Debra; 05-13-2012 at 04:35 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Debra is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default Re: displaying values from HashMap

    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"));
     
                }
        });
    }
    The program changes the background color but does not display the hexadecimal.

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: displaying values from HashMap

    I agree with Daryl, buti think you want something like:
    Java Code:
    map.get("Red");
    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.

  5. #5
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: displaying values from HashMap

    Actually your code produce a java.lang.NullPointerException exception because the display JLabel was never instantiated in your code.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default 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)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. HashMap help? Values keep overriding..?
    By sim18 in forum New To Java
    Replies: 2
    Last Post: 05-06-2012, 12:59 PM
  2. Cannot return values from hashmap
    By uhertz in forum New To Java
    Replies: 1
    Last Post: 06-17-2011, 04:16 PM
  3. Displaying HashMap in a JFrame
    By ankit1801 in forum New To Java
    Replies: 1
    Last Post: 05-19-2011, 07:54 AM
  4. how to get the values from hashmap
    By baktha.thalapathy in forum New To Java
    Replies: 5
    Last Post: 05-25-2010, 02:12 PM
  5. HashMap contains all values but doesn't show all values
    By xcallmejudasx in forum New To Java
    Replies: 3
    Last Post: 05-10-2009, 11:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •