Home > Software design >  How to set the background color of JFrame to its blank value?
How to set the background color of JFrame to its blank value?

Time:10-26

I want to use a 'Clear' button to clear the background color of a JFrame after using a 'color' button to add color to the background. Everything I've been able to find tells me how to change the value to a different color but not how to remove it.

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

public class ColorFrame extends JFrame {
    JButton red, green, blue, clear;
    
    public ColorFrame() {
        super("ColorFrame");
        
        setSize(322, 122);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        FlowLayout flo = new FlowLayout(); 
        setLayout(flo);
        red = new JButton ("Red");
        add(red);
        green = new JButton ("Green");
        add(green);
        blue = new JButton("Blue");
        add(blue);
        clear = new JButton("Clear");
        add(clear);
    
        ActionListener act = new ActionListener() {
            public void actionPerformed (ActionEvent event) {
                if(event.getSource() == red) {
                    getContentPane().setBackground(Color.RED);
                }
                if(event.getSource() == green) {
                    getContentPane().setBackground(Color.GREEN);
                }
                if(event.getSource() == blue) {
                    getContentPane().setBackground(Color.BLUE);
                }   
                if(event.getSource() == clear) {
                    //clear background color
                }
            }
        };

        red.addActionListener(act);
        green.addActionListener(act);
        blue.addActionListener(act);
        clear.addActionListener(act);

        setVisible(true);
    }
    public static void main(String arguments[]) {
        new ColorFrame();
    }
}

CodePudding user response:

"Everything I've been able to find tells me how to change the value to a different color but not how to remove it." - This is because you shouldn't. Even the "default" background color is a color and I think you should not try to "remove" it in any way.

You can of course set the background color of the JFrame (or more specifically, the frame's content pane) back to its default value. For this, you can use one of two approaches:

1. Simply save the default value before you modify it

public class ColorFrame extends JFrame {
    JButton red, green, blue, clear;
    Color defaultColor;

    ...

    ActionListener yourListener = new ActionListener() {
        public void actionPerformed (ActionEvent event) {
            // save the frame background default value before changing it
            defaultColor = getContentPane().getBackground();
            // modify the color as needed and use the stored default later as needed

2. Retrieve the default background color from the UIManager

Each swing component's default properties are stored in the UIManager, specific to each look and feel. Knowing this, you can easily retrieve these default values as needed:

Color defaultColor = UIManager.getColor("Panel.background");
getContentPane().setBackground(defaultColor);

Some additional considerations regarding your code:

  • Use Actions or at least separate ActionListeners if you want different behavior for each button. Having a shared ActionListener which then checks from which component the action was fired from is not recommended.
  • Don't use setSize(). Instead, let the LayoutManager size the components and frame for you by calling pack() on the JFrame after adding all components.
  • Avoid extending JFrame if there is no need to. (I currently see no need to in your code, since you don't fundamentally change the default behaviour of the JFrame)

CodePudding user response:

Set the background to no color, i.e. null:

getContentPane().setBackground(null);
  • Related