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.

Results 1 to 12 of 12

Thread: Storing color names and their hexadecimals in hasmap

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Storing color names and their hexadecimals in hasmap

    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?


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Storing color names and their hexadecimals in hasmap

    Because Strings have to be enclosed by literals. It's taking them as variables.

    Try this

    map.put("Red", "FF0000");

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Storing color names and their hexadecimals in hasmap

    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?

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Storing color names and their hexadecimals in hasmap

    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.

  5. #5
    Junior Member
    Join Date
    May 2012
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Storing color names and their hexadecimals in hasmap

    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

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Storing color names and their hexadecimals in hasmap

    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.

  7. #7
    Junior Member
    Join Date
    May 2012
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Storing color names and their hexadecimals in hasmap

    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.

  8. #8
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Storing color names and their hexadecimals in hasmap

    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.

  9. #9
    Junior Member
    Join Date
    May 2012
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Storing color names and their hexadecimals in hasmap

    I have tried the getContentPane().add(display); but still having the same issue.

  10. #10
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Storing color names and their hexadecimals in hasmap

    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

  11. The Following User Says Thank You to javapenguin For This Useful Post:

    georger55 (May 12th, 2012)

  12. #11
    Junior Member
    Join Date
    May 2012
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Storing color names and their hexadecimals in hasmap

    woooowwwww lol. I never set display = new JLabel. Thank you for helping me find this error. It is always something small

  13. #12
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Storing color names and their hexadecimals in hasmap

    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.

Similar Threads

  1. Why does the Color class have two variables for each color?
    By Melawe in forum Java Theory & Questions
    Replies: 5
    Last Post: May 10th, 2012, 04:21 PM
  2. get method names in all the class files in the jar/war/ear
    By purushothaman in forum Java SE APIs
    Replies: 0
    Last Post: March 20th, 2012, 08:45 AM
  3. Help with this names program
    By RyanT in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 17th, 2011, 03:07 PM
  4. Listing file names in a JList
    By KILL3RTACO in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: October 8th, 2011, 12:52 PM
  5. text color changer based on words/ word classification by color
    By knoxy5467 in forum Java Theory & Questions
    Replies: 25
    Last Post: June 15th, 2011, 07:52 AM