-1

I am trying to set my buttons to change the panel background color in which the buttons reside using a borderlayout manager. I have no problems when I use flowlayout but can't figure it out with the border layout. I feel like I am missing something fundamental. I have found similar threads with panels and color changing but none have been able to answer my question. Here's what I have so far:

import java.awt.*;     // Needed for BorderLayout class
import javax.swing.*;  // Needed for Swing classes
import java.awt.event.*;//Needed for Action Listener


public class BorderPanelWindow extends JFrame
{
   public BorderPanelWindow()
   { 
      // Set the title bar text.
      setTitle("Border Layout");

      // Specify an action for the close button.
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      // Add a BorderLayout manager to the content pane.
      setLayout(new BorderLayout());

      // Create five panels.
      JPanel panel1 = new JPanel();
      JPanel panel2 = new JPanel();
      JPanel panel3 = new JPanel();
      JPanel panel4 = new JPanel();
      JPanel panel5 = new JPanel();

      // Create five buttons.
      JButton rbutton = new JButton("Red");
      JButton bbutton = new JButton("Blue");
      JButton gbutton = new JButton("Green");
      JButton ybutton = new JButton("Yellow");
      JButton obutton = new JButton("Orange");

      //create actions for the buttons
      ColorChanger yellowAction = new ColorChanger(Color.YELLOW); 
      ColorChanger redAction = new ColorChanger(Color.RED); 
      ColorChanger blueAction = new ColorChanger(Color.BLUE);
      ColorChanger greenAction = new ColorChanger(Color.GREEN);
      ColorChanger orangeAction = new ColorChanger(Color.ORANGE);

      //set actions for the buttons
      rbutton.addActionListener(redAction);
      bbutton.addActionListener(blueAction);
      gbutton.addActionListener(greenAction);
      ybutton.addActionListener(yellowAction);
      obutton.addActionListener(orangeAction);


      // Add the buttons to the panels.
      panel1.add(rbutton);
      panel2.add(bbutton);
      panel3.add(gbutton);
      panel4.add(ybutton);
      panel5.add(obutton);

      // Add the five panels to the content pane.
      add(panel1, BorderLayout.NORTH);
      add(panel2, BorderLayout.SOUTH);
      add(panel3, BorderLayout.EAST);
      add(panel4, BorderLayout.WEST);
      add(panel5, BorderLayout.CENTER);

      // Pack and display the window.
      pack();
      setVisible(true);
   }

   private class ColorChanger implements ActionListener 
   { 
      //fields
      private Color backgroundColor;

      //constructor
      public ColorChanger(Color c) 
      { 
         backgroundColor = c; 
      } 


      public void actionPerformed(ActionEvent e) 
      {
         setBackground(backgroundColor); 
      }  

   } 



   public static void main(String[] args)
   {
       new BorderPanelWindow();
   }
}

I've managed to get the code to compile

splungebob
  • 5,357
  • 2
  • 22
  • 45
JRave
  • 81
  • 1
  • 8

1 Answers1

0

Try this one

    public void actionPerformed(ActionEvent e) {
        for (int i = 0; i < getContentPane().getComponentCount(); i++) {
            getContentPane().getComponent(i).setBackground(backgroundColor);
        }
    }

You are changing the color of JFrame which is parent of all JPanel that is added into it. You have to set the background color of all JPanel added into it.


--EDIT--

As per your last comment - I only want the region where the buttons reside to change color

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() instanceof JButton) {
            ((JButton) e.getSource()).getParent().setBackground(backgroundColor);
        }
    }
Braj
  • 46,415
  • 5
  • 60
  • 76
  • ur awesome! that was a huge help, but could you help me understand it. I don't understand how/what getContentPane().getComponent(i) is referencing? – JRave Mar 21 '14 at 23:03
  • Read it [here](http://docs.oracle.com/javase/7/docs/api/javax/swing/RootPaneContainer.html) – Braj Mar 21 '14 at 23:07
  • Directly from JavaDoc of [JFrame](http://docs.oracle.com/javase/6/docs/api/javax/swing/JFrame.html) that says: *The content pane provided by the root pane should, as a rule, contain all the non-menu components displayed by the JFrame. This is different from the AWT Frame case. As a conveniance add and its variants, remove and setLayout have been overridden to forward to the contentPane as necessary.* – Braj Mar 21 '14 at 23:10
  • Sry disregard my previous post. that's close but I only want the region where the buttons reside to change color? – JRave Mar 21 '14 at 23:16
  • Find updates in my post. Get the parent of `JButton` that is a `JPanel` and change the background color. – Braj Mar 21 '14 at 23:30
  • ok that i understand. never would have that to use instanceof. you've been a huge help Braj. – JRave Mar 21 '14 at 23:41
  • Its a good practice to use `instanceof` operator before any casting operation. In this program you know it but in a bigger application it can break. – Braj Mar 21 '14 at 23:43