The Artima Developer Community
Sponsored Link

Java Answers Forum
Help changing a JPanel background color

11 replies on 1 page. Most recent reply: Feb 24, 2003 9:32 AM by Mark Schuh

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 11 replies on 1 page
Mark Schuh

Posts: 8
Nickname: shoe2
Registered: Feb, 2003

Help changing a JPanel background color Posted: Feb 14, 2003 9:16 PM
Reply to this message Reply
Advertisement
Greetings,

New to Java. Working with GUI's. Know how to change the background color of the content pane after a button is pressed. But cannot figure out how to change the background color of the JPanel, that hold the buttons, after a button is pushed. Any help is greatly appreciated.

Thanks in advance.

Mark Schuh


Max Lyn

Posts: 11
Nickname: techrolla
Registered: Feb, 2003

Re: Help changing a JPanel background color Posted: Feb 14, 2003 9:50 PM
Reply to this message Reply
call setBackground(Color c) on the JPanel

JPanel panel = new JPanel();

actionPerformed(ActionEvent e)
{
if(e.getSource() == ok)
{

panel.setBackground(Color.red);
}
}

Mark Schuh

Posts: 8
Nickname: shoe2
Registered: Feb, 2003

Re: Help changing a JPanel background color Posted: Feb 15, 2003 10:10 AM
Reply to this message Reply
Thank you so much for your answer, however, I do not know how to apply it to my code. Here is my code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

// Trying to get JPanel background to change colors after
// pressing a button. Current code changes content pane
// background.

public class PanelDemo44 extends JFrame implements ActionListener
{
public static final int WIDTH = 300;
public static final int HEIGHT = 200;

public static void main(String[] args)
{
PanelDemo44 guiWithPanel = new PanelDemo44();
guiWithPanel.setVisible(true);
}

public PanelDemo44()
{
setSize(WIDTH, HEIGHT);
addWindowListener(new WindowDestroyer());
setTitle("Panel Demonstration");
Container contentPane = getContentPane();
contentPane.setBackground(Color.blue);
contentPane.setLayout(new BorderLayout());

JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.white);

buttonPanel.setLayout(new FlowLayout());

JButton stopButton = new JButton("Red");
stopButton.setBackground(Color.red);
stopButton.addActionListener(this);
buttonPanel.add(stopButton);

JButton goButton = new JButton("Green");
goButton.setBackground(Color.green);
goButton.addActionListener(this);
buttonPanel.add(goButton);

contentPane.add(buttonPanel, BorderLayout.SOUTH);
}

public void actionPerformed(ActionEvent e)
{
Container contentPane = getContentPane();

if (e.getActionCommand().equals("Red"))
contentPane.setBackground(Color.red);
else if (e.getActionCommand().equals("Green"))
contentPane.setBackground(Color.green);
else
System.out.println("Error in button interface.");

}
}

Any help with this would be greatly appreciated.

Best Wishes.

Mark Schuh

kanak

Posts: 14
Nickname: vava
Registered: Jan, 2003

Re: Help changing a JPanel background color Posted: Feb 15, 2003 11:18 AM
Reply to this message Reply
setBackground(Color k)

JPanel pan = new JPanel();

actionPerformed(ActionEvent kan)
{
if(kan.getSource() == ok)
{

pan.setBackground(Color.*);//*refers to any colour or the code of the color
}
}

Mark Schuh

Posts: 8
Nickname: shoe2
Registered: Feb, 2003

Re: Help changing a JPanel background color Posted: Feb 15, 2003 1:03 PM
Reply to this message Reply
Greetings and thanks for the reply.

Please excuse me but I am still lost on this one. Can I enter that code to get the buttonPanel to change color with a button click? Or must I do something more with that code than is what is there. I don't know where to insert it?

I have already set the background color to white in the program to start out. Then with a click of the button, I want the content panel to change to applicable color i.e. red and the buttonpanel to change to another color i.e yellow.
The program as written will change the content panel to the colors I want, but how do I get that buttonPanel to change colors when I click?

Thanks in advance for any help--it is greatly appreciated.

Best Wishes.

Mark Schuh

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Help changing a JPanel background color Posted: Feb 17, 2003 6:54 PM
Reply to this message Reply
You may have to use the method:
setOpaque(true);

before you set the background color and do some painting commands.

Mark Schuh

Posts: 8
Nickname: shoe2
Registered: Feb, 2003

Re: Help changing a JPanel background color Posted: Feb 18, 2003 6:26 PM
Reply to this message Reply
Greetings,

Charles, thank you for your kind reply. I guess I am just not getting it, I can't get that buttonpanel to change colors with a button click. Thanks to all who tried to help me out. If anyone else has any guidance for me it would be greatly appreciated. Here is some more:

Can someone tell me why the setBackground command will work for the contentPanel but not for the buttonPanel utilizing my current code if I use something like:

if (e.getActionCommand().equals("Red"))
buttonPanel.setBackground(Color.yellow);

in place of

if (e.getActionCommand().equals("Red"))
contentPane.setBackground(Color.red);

Wha t am I missing?? Is the buttonPanel not registered to listen to the button click and if so, how do I register it to listen. What part(s) of my current code allows the contentPane.setBackground statement to actually work?

Best Wishes

Mark Schuh

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Help changing a JPanel background color Posted: Feb 22, 2003 11:36 AM
Reply to this message Reply
The following is some code which uses several different ways to determine what was clicked and what container the item clicked is in.


/* JPanelColorChanger.java
* @author: Charles Bell
* @version: Feb 22, 2003
*/

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

/** Basic JFrame application with three JPanels. Each JPanel
* has a different Border. An ActionListener is added to
* each JPanel. Three JButtons are added to each JPanel to
* set its background color.
*/
public class JPanelColorChanger extends JFrame implements ActionListener{

private JPanel topPanel;
private JPanel middlePanel;
private JPanel bottomPanel;
private boolean inAnApplet = true;

/** Constructs a JPanelColorChanger object and initializes the GUI.
*/
public JPanelColorChanger(){
super("JPanelColorChanger");
}

/** Called when this is run as an application.
*/
public static void main(String[] args){
JPanelColorChanger colorChanger = new JPanelColorChanger();
colorChanger.inAnApplet = false;
colorChanger.init();
}

/** Initializes the JFrame GUI with three JPanels.
* An ActionListener is added to each JPanel.
* Three JButtons are added to each JPanel to
* set its background color.
*/
public void init(){

/* Set the application to Exit if the user
* closes the JFrame window. If in an applet
* skip it.
*/
if (!inAnApplet) setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
System.out.println("inAnApplet: " + inAnApplet);

/* Initialize the three JPanels
*/
topPanel = new JPanel();
middlePanel = new JPanel();
bottomPanel = new JPanel();

/* Add JButtons to each JPanel so the user
* can change the background color of the JPanel.
*/
JButton redButton1 = new JButton("Red");
JButton greenButton1 = new JButton("Green");
JButton blueButton1 = new JButton("Blue");

JButton redButton2 = new JButton("Red");
JButton greenButton2 = new JButton("Green");
JButton blueButton2 = new JButton("Blue");

JButton redButton3 = new JButton("Red");
JButton greenButton3 = new JButton("Green");
JButton blueButton3 = new JButton("Blue");

topPanel.add(redButton1, BorderLayout.WEST);
topPanel.add(greenButton1, BorderLayout.CENTER);
topPanel.add(blueButton1, BorderLayout.EAST);

middlePanel.add(redButton2, BorderLayout.WEST);
middlePanel.add(greenButton2, BorderLayout.CENTER);
middlePanel.add(blueButton2, BorderLayout.EAST);

bottomPanel.add(redButton3, BorderLayout.WEST);
bottomPanel.add(greenButton3, BorderLayout.CENTER);
bottomPanel.add(blueButton3, BorderLayout.EAST);



/* Add ActionListener to the JButtons.
* Since this class implements the ActionListener
* interface, it can serve as a ActionListener.
*/
redButton1.addActionListener(this);
greenButton1.addActionListener(this);
blueButton1.addActionListener(this);
redButton2.addActionListener(this);
greenButton2.addActionListener(this);
blueButton2.addActionListener(this);
redButton3.addActionListener(this);
greenButton3.addActionListener(this);
blueButton3.addActionListener(this);

/* Add the JPanels to the contentPane of the JFrame. */
getContentPane().add(topPanel, BorderLayout.NORTH);
getContentPane().add(middlePanel, BorderLayout.CENTER);
getContentPane().add(bottomPanel, BorderLayout.SOUTH);

/* Pack the dimensions down to the preferred sizes. */
pack();

/* Center the JFrame. */
setLocationRelativeTo(null);
/* Display the JFrame. */
show();
}

/** Implements the ActionListener interface.
* Uses the action command string obtained from
* the getActionCommand method of ActionEvent to
* set the color selection.
* Uses the ActionEvent getSource() method to find
* component which fired the ActionEvent. Uses
* instanceof to test whether the action event source
* was a JButton. If so, tests if each JPanel is the
* ancestor of the JButton component using the Container
* isAncestorOf method and sets the background color.
* Also gets the Parent container object of the JButton.
* to tell what container object the action event
* originated in.
*/
public void actionPerformed(ActionEvent actionEvent){
System.out.println(actionEvent.getActionCommand());
Color color = null;
if (actionEvent.getActionCommand().compareTo("Red") == 0) color = Color.red;
if (actionEvent.getActionCommand().compareTo("Green") == 0) color = Color.green;
if (actionEvent.getActionCommand().compareTo("Blue") == 0) color = Color.blue;

if (actionEvent.getSource() instanceof JButton) {
System.out.println("JButton clicked");
if (topPanel.isAncestorOf((JButton)actionEvent.getSource())){
System.out.println("JButton in topPanel clicked");
topPanel.setBackground(color);
}else if (middlePanel.isAncestorOf((JButton)actionEvent.getSource())){
System.out.println("JButton in middlePanel clicked");
middlePanel.setBackground(color);
}else if (bottomPanel.isAncestorOf((JButton)actionEvent.getSource())){
System.out.println("JButton in bottomPanel clicked");
bottomPanel.setBackground(color);
}

if (((JButton)actionEvent.getSource()).getParent().equals(topPanel)) System.out.println("topPanel clicked");
if (((JButton)actionEvent.getSource()).equals(middlePanel)) System.out.println("middlePanel clicked");
if (((JButton)actionEvent.getSource()).equals(bottomPanel)) System.out.println("bottomPanel clicked");

}
}



}

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Help changing a JPanel background color Posted: Feb 22, 2003 11:46 AM
Reply to this message Reply
Yopu just need to revise your actionPerformed method as in the following:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
// Trying to get JPanel background to change colors after
// pressing a button. Current code changes content pane
// background.
public class PanelDemo44 extends JFrame implements ActionListener
{
public static final int WIDTH = 300;
public static final int HEIGHT = 200;

public static void main(String[] args)
{
PanelDemo44 guiWithPanel = new PanelDemo44();
guiWithPanel.setVisible(true);
}
public PanelDemo44()
{
setSize(WIDTH, HEIGHT);
//addWindowListener(new WindowDestroyer());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setTitle("Panel Demonstration");
Container contentPane = getContentPane();
contentPane.setBackground(Color.blue);
contentPane.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.white);
buttonPanel.setLayout(new FlowLayout());
JButton stopButton = new JButton("Red");
stopButton.setBackground(Color.red);
stopButton.addActionListener(this);
buttonPanel.add(stopButton);
JButton goButton = new JButton("Green");
goButton.setBackground(Color.green);
goButton.addActionListener(this);
buttonPanel.add(goButton);
contentPane.add(buttonPanel, BorderLayout.SOUTH);
}

public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("Red"))
((JButton)e.getSource()).getParent().setBackground(Color.red);
else if (e.getActionCommand().equals("Green"))
((JButton)e.getSource()).getParent().setBackground(Color.green);
else
System.out.println("Error in button interface.");
}
}
[/pre]

Mark Schuh

Posts: 8
Nickname: shoe2
Registered: Feb, 2003

Re: Help changing a JPanel background color Posted: Feb 23, 2003 3:11 PM
Reply to this message Reply
Greetings,

A warm THANK YOU to Mr. Charles Bell. This was my first time using this forum and Charles, you solved my problem. It worked like a charm. Your other button example was very informative as well and I am studying it to try and learn more about SWING.

Now I am asking for some more information about exactly how you solved the problem. Could you go through the statement

((JButton)e.getSource()).getParent().setBackground(Color.red);

and explain what is happening with each portion of the statement. I would love to see how each portion of the statement was able to actually cause the ButtonPanel to change colors.

Example:

Why does it start out with JButton and not JPanel?
What is getSource? getting the source of something?

What is getParent? And what is its role in the solution?

Where do I learn about these terms? Do you have a good textbook to recommend to understand these terms for a beginner?

Many thanks again to all who responded and especially to Charles Bell who gave me what I needed to get the job done--now I am just trying to figure out how that solution actually worked.

Maybe one day I will have enough Java knowledge to help somebody out on forums like this.

Very appreciative of the forum and the help provided.

Best Wishes

Mark Schuh

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Help changing a JPanel background color Posted: Feb 23, 2003 6:44 PM
Reply to this message Reply
That's a very good question.
That coding sure does look too complicated.

((JButton)e.getSource()).getParent().setBackground(Color.red);

e is the ActionEvent object
which is in the argument of the actionPerformed method.

When you look at the documentation for ActionEvent,
(Its on line at: http://java.sun.com/j2se/1.4.1/docs/api/)
you find that ActionEvent inherits the method getSource() from the class java.util.EventObject

getSource() returns an Object, so e.getSource() returns an Object class.
But the Object that caused the ActionEvent object to be created is really a JButton in this program, so to use methods from the JButton class, you have to cast the Object to a JButton by putting the class name in parentheses.
(JButton)e.getSource()
I put that expression in parentheses since it now refers to the JButton object that fired the ActionEvent.
((JButton)e.getSource())

Then I used the getParent() method from the Container class that JButton inherits from.
getParent() returns a Container object which is the JPanel that the JButton is contained in.

Then I use the setBackground(Color.red) on that Container.

These methods can be put together using the dot or . operator in java.

((JButton)e.getSource()).getParent().setBackground(Color.red);
is the same as the following four statements:

Object object = e.getSource();
JButton button = (JButton)object;
Container container = button.getParent();
container.setBackground(Color.red);

Mark Schuh

Posts: 8
Nickname: shoe2
Registered: Feb, 2003

Re: Help changing a JPanel background color Posted: Feb 24, 2003 9:32 AM
Reply to this message Reply
Greetings,

Thanks again Charles for your reply and very thorough detail. I do not understand it all as I am just learning about classes and inheritance, but I get the general idea. Thank you once again for providing your expertise in forums like this. I noticed you already have tallied some two hundred sixty some responses to requests for help--that is impressive.

Until my next problem....

Best Wishes

Mark Schuh

Flat View: This topic has 11 replies on 1 page
Topic: get paid to help me. Previous Topic   Next Topic Topic: interface question

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use