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 8 of 8

Thread: Changing Panel Color with a ComboBox

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Changing Panel Color with a ComboBox

    So I'm working on a program to change the Color of a Panel with a Combo Box.

    So far I have the Panel and the ComboBox with th options. However, when you click the option nothing happens. This is my first GUI program and I'm not too familiar with how to carry out actions but here's what I have so far. I'm just looking to get pointed in the right direction.

     
     
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.JComboBox;
     
    public class ThreeColorsFrame extends JFrame {
        private JPanel ColorPanel;
        private static final int FRAME_WIDTH = 300;
        private static final int FRAME_HEIGHT = 300;
         private JComboBox facenameCombo;
            private ActionListener listener;
     
       public ThreeColorsFrame()
        {
     
           setSize(FRAME_WIDTH, FRAME_HEIGHT);
           ColorPanel = new JPanel();
           add(ColorPanel, BorderLayout.CENTER);
           createComboBox();
     
        }
     
     
     
     
     
       public void createComboBox()
       {
         JPanel northPanel = new JPanel();
     
          facenameCombo = new JComboBox();
          facenameCombo.addItem(Color.RED);
          facenameCombo.addItem(Color.GREEN);
          facenameCombo.addItem(Color.BLUE);
          facenameCombo.setEditable(true);
          facenameCombo.addActionListener(listener);
           setColor();
     
          northPanel.add(facenameCombo);
     
     
          add(northPanel, BorderLayout.NORTH);
     
       }
     
     
       public void setColor()
       {
          // Get font name
          Color color
                = (Color) facenameCombo.getSelectedItem();
     
         /* if (color.equals("Red"))
                  {Color thecolor = Color.RED;
     
          ColorPanel.setBackground(thecolor);}
     
          if (color.equals("Green"))
                   {Color thecolor = Color.GREEN;
     
          ColorPanel.setBackground(thecolor);}
     
          if (color.equals("Blue"))
                   {Color thecolor = Color.BLUE;
     
          ColorPanel.setBackground(thecolor);}
     
          // Set font of text field
          */
    ColorPanel.setBackground(color);}
     
     
     
     
     
     
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Changing Panel Color with a ComboBox

    The code you posted does not appear to implement a listener. It defines a listener, but never implements the actionPerformed method, which is required. Recommend you read through the following links:
    How to Use Combo Boxes (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)

    Basically, you need to instantiate the listener variable, which the code below will do but you must complete the code for the results you want
    listener = new ActionListener(){
        public void actionPerformed(ActionEvent e){
            //place code here
        }
    };
    Last edited by copeg; March 2nd, 2011 at 03:13 PM.

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Changing Panel Color with a ComboBox

    So I'm trying to get the action listener right but I get a ; Expected error at the second to last }. What am I doing wrong?

     
      public void createComboBox()
       {
         JPanel northPanel = new JPanel();
     
          facenameCombo = new JComboBox();
          facenameCombo.addItem(Color.RED);
          facenameCombo.addItem(Color.GREEN);
          facenameCombo.addItem(Color.BLUE);
          facenameCombo.setEditable(true);
          facenameCombo.addActionListener(listener);
     
          northPanel.add(facenameCombo);
     
     
          add(northPanel, BorderLayout.NORTH);
     
     
     
     
             listener = new ActionListener(){
        public void actionPerformed(ActionEvent e){
    facenameCombo = (JComboBox)e.getSource();
    Color color = (Color) facenameCombo.getSelectedItem();
    ColorPanel.setBackground(color);}
     
     
             }
     
     
        }

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Changing Panel Color with a ComboBox

    You need a semicolon at the end of the anonymous class definition (my code above did not reflect this - edited)

  5. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Changing Panel Color with a ComboBox

    Lol so I have no errors now. The only problem is nothing happens when I Choose an option form the Combo Box nothing happens

    public class ThreeColorsFrame extends JFrame {
        private JPanel ColorPanel;
        private static final int FRAME_WIDTH = 300;
        private static final int FRAME_HEIGHT = 300;
         private JComboBox facenameCombo;
            private ActionListener listener;
     
       public ThreeColorsFrame()
        {
     
           setSize(FRAME_WIDTH, FRAME_HEIGHT);
           ColorPanel = new JPanel();
           add(ColorPanel, BorderLayout.CENTER);
           createComboBox();
     
        }
     
     
     
     
     
       public void createComboBox()
       {
         JPanel northPanel = new JPanel();
     
          facenameCombo = new JComboBox();
          facenameCombo.addItem(Color.RED);
          facenameCombo.addItem(Color.GREEN);
          facenameCombo.addItem(Color.BLUE);
          facenameCombo.setEditable(true);
          facenameCombo.addActionListener(listener);
     
          northPanel.add(facenameCombo);
     
     
          add(northPanel, BorderLayout.NORTH);
     
     
     
     
             listener = new ActionListener(){
        public void actionPerformed(ActionEvent e){
    facenameCombo = (JComboBox)e.getSource();
    Color color = (Color) facenameCombo.getSelectedItem();
    ColorPanel.setBackground(color);}
     
     
     
             };
     
     
        }
     
     
    }


    I draw the box, give it an action listener, and an action to perform, and I missing something?

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Changing Panel Color with a ComboBox

    Add some println's in there to see the values of the variables...in particular, the listener object when you try to add the listener will be null. You must first instantiate it (create the anonymous class) and then add it as the listener to the JComboBox (in its current for it is being done the other way around).

  7. #7
    Junior Member
    Join Date
    Jan 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Changing Panel Color with a ComboBox

    How do you instantiate an anonymous class?

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Changing Panel Color with a ComboBox

    Quote Originally Posted by bengregg View Post
    How do you instantiate an anonymous class?
    You did already by implementing the action listener. Google the term anonymous class and you will see many explanations. My recommendation above might be better illustrated with the following:
    String myString = null;
    System.out.println(myString);
    myString = "Testing";

    Println will print null. The same is the case with the listener variable in your code listing - you want to assign it to something before trying to use it

Similar Threads

  1. combobox unicode problem
    By trexi in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: January 18th, 2011, 11:06 AM
  2. ComboBox and TextField
    By fahad in forum AWT / Java Swing
    Replies: 2
    Last Post: May 31st, 2010, 08:47 AM
  3. changing the visited links color in JEditorpane
    By nasi in forum AWT / Java Swing
    Replies: 5
    Last Post: April 18th, 2010, 08:44 AM
  4. do actions in ComboBox
    By java_cs in forum AWT / Java Swing
    Replies: 2
    Last Post: October 1st, 2009, 10:53 AM
  5. Java program to display 2D Array in ComboBox
    By crazydeo in forum AWT / Java Swing
    Replies: 1
    Last Post: May 29th, 2008, 09:13 AM