This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes how to set Box color? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "how to set Box color?" Watch "how to set Box color?" New topic

how to set Box color?

Max Simpson
Ranch Hand

Joined: Sep 23, 2004
Posts: 30
I have tried setting a Box background color by doing the following and haven't had any luck:


The Box just stays the regular old gray. I have tried different colors, caps, lowercase.. nothing works.

Thanks in advance.
kiran raja

Joined: May 11, 2004
Posts: 22
this program is an example for setting the background colour for a Box.May be this may help you.
First set the background as in GraphicsPanel and then add it to the Box .
Please let me know where this works .


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

////////////////////////////////////////////////////////////////// ColorDisplay
class ColorDisplay extends JFrame {

JSlider redBar; // Slider for adjusting red from 0-255
JSlider grnBar;
JSlider bluBar;

int red = 128; // initial red color value
int green = 128;
int blue = 128;

GraphicsPanel palette; // an area to display the color
JTextField txtField; // shows the Java source equivalent

//================================================ constructor
public ColorDisplay() {
palette = new GraphicsPanel();

// Create a textfield for displaying the values
txtField = new JTextField();
txtField.setFont(new Font("Monospaced", Font.PLAIN, 12));
txtField.setText("Color(" + red + ", " + green + ", " + blue +
") or " + "Color(0x" +
Integer.toHexString(blue + 256 * (green + 256 * red)) + ")");

// Create one object that will listen to all sliders
ChangeListener al = new ColorListener();

redBar = new JSlider(JSlider.VERTICAL, 0, 255, red);

grnBar = new JSlider(JSlider.VERTICAL, 0, 255, green);

bluBar = new JSlider(JSlider.VERTICAL, 0, 255, blue);

//=== Put the color palette and textfield in a box
Box paletteText = Box.createVerticalBox();

//=== put the 3 sliders in a gridlayout panel
JPanel colorControls = new JPanel();
colorControls.setLayout(new GridLayout(1, 3));

//=== Now let's build the content pane
Container content = this.getContentPane();
content.setLayout(new BoxLayout(content, BoxLayout.X_AXIS));

}//end constructor

////////////////////////////////////////////////////////////// GraphicsPanel
// A component for drawing ,overrides paintComponent()
class GraphicsPanel extends JPanel {
//======================================== GraphicsPanel constructor
public GraphicsPanel() {
this.setPreferredSize(new Dimension(300, 300));
}//end constructor

//=================================== GraphicsPanel : paintComponent
public void paintComponent(Graphics g) {
super.paintComponent(g); // paint background, border
g.setColor(new Color(red, green, blue)); // Set the color
g.fillRect(0, 0, this.getWidth(), this.getHeight()); // Color everything
}//endmethod paintComponent
}//endclass GraphicsPanel

///////////////////////////////////////////////////// class ColorListener
// This class is created only as a place to put the listener.
// We could have just as easily put the listener in any other class,
// or used an anonymous listener.
// This is defined as an inner class (defined inside of ColorDisplay),
// so it can reference the field variables of that class (specifically,
// the sliders, graphics panel, and text field).
class ColorListener implements ChangeListener {
public void stateChanged(ChangeEvent ae) {
red = redBar.getValue();
green = grnBar.getValue();
blue = bluBar.getValue();
txtField.setText("Color(" + red + ", " + green + ", " + blue +
") or " + "Color(0x" + Integer.toHexString(blue +
256 * (green + 256 * red)) + ")");
}//endmethod stateChanged
}//endclass ColorListener

//================================================================= main
public static void main(String[] args) {
JFrame window = new ColorDisplay();
}//end main

}//end class ColorDisplay
I agree. Here's the link:
subject: how to set Box color?
Similar Threads
JList SelectionForeground problem when BOLD
css background color question
In disabled mode, how do i make JList data to be in black color
change font size or color in javascript alert box
Viewing inside the Box!