Problem with Java Color (setBackground(Color))

  • Java
  • Thread starter BiGyElLoWhAt
  • Start date
  • Tags
    Color Java
In summary, the person is having trouble setting the background color of their content pane. They have tried to access the colors and add a call to set the background color during initialization, but it still doesn't work. They have also mentioned not being able to save due to a custom class they use. They found a possible explanation on a website and thank the person for the information. They are in a time crunch and any help would be appreciated.
  • #1
BiGyElLoWhAt
Gold Member
1,622
131
Java:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.EmptyBorder;

public class ScheduleGUI extends JFrame implements ActionListener{
   
    String newLine = System.getProperty("line.separator");
   
    private JPanel coursePane;
    private JPanel initializePane;
    private JPanel addPane;
    private JPanel instructionPane;
    private JPanel timePane;
    private JPanel rightContainer;
    private JPanel courseNames;
    private JPanel days;
    private JPanel courseTimes;
    private JPanel courseTimesStart;
    private JPanel courseTimesEnd;
   
    //Browse browse;
   
    /*
     * For the check boxes
     */
    private JLabel mo;
    private JLabel tu;
    private JLabel we;
    private JLabel th;
    private JLabel fr;
    private JLabel sa;
    private JLabel su;
    private JLabel times[];
    /*
     * For the graphic schedule
     */
    private JLabel monday;
    private JLabel tuesday;
    private JLabel wednesday;
    private JLabel thursday;
    private JLabel friday;
    private JLabel saturday;
    private JLabel sunday;
   
    private JTextArea instructions;
   
    private JCheckBox moBox;
    private JCheckBox tuBox;
    private JCheckBox weBox;
    private JCheckBox thBox;
    private JCheckBox frBox;
    private JCheckBox saBox;
    private JCheckBox suBox;
   
    private TextField name;
    private TextField courseName;
   
    private JSpinner startTimeMinutes;
    private JSpinner startTimeHours;
    private JSpinner endTimeMinutes;
    private JSpinner endTimeHours;
    private SpinnerNumberModel minuteModelStart;
    private SpinnerNumberModel minuteModelEnd;
    private SpinnerNumberModel hourModelStart;
    private SpinnerNumberModel hourModelEnd;

   
    private JButton initialize;
    private JButton add;
    private JButton remove;
   
    private JScrollPane scroll;   
   
    private JMenuBar menu;
    private JMenu file;
    private JMenu colors;
    private JMenuItem save;
    private JMenuItem load;
    private JMenuItem exit;
   
    private JRadioButtonMenuItem red;
    private JRadioButtonMenuItem green;
    private JRadioButtonMenuItem blue;
    private ButtonGroup colorGroup;
   
    GridBagConstraints c;
   
    public ScheduleGUI()
    {
        //browse = new Browse();
        getContentPane().setBackground(Color.GREEN);
        /*
         * initialize labels for text boxes
         */
        mo = new JLabel("M");
        tu = new JLabel("T");
        we = new JLabel("W");
        th = new JLabel("R");
        fr = new JLabel("F");
        sa = new JLabel("S");
        su = new JLabel("N");
       
        monday = new JLabel("Monday");
        tuesday = new JLabel("Tuesday");
        wednesday = new JLabel("Wednesday");
        thursday = new JLabel("Thursday");
        friday = new JLabel("Farraday");
        saturday = new JLabel("Saturday");
        sunday = new JLabel("Sunday");
       
        /*
         * Get 15 minute intervals of time between 8am and 8pm
         * to be used for scheduling;
         */
       
        String zeros = "00";
        String ampm = "am";
        times = new JLabel[49];
        int k = 0;
        for(int i = 0 ; i< 49 ; i++)
        {
            if(i%4 == 0 && i !=0) k++;
            int hour = (7 + k)%12 +1;
            if(k>3) ampm = "pm";
            int minutes = 15*(i%4);
            if(minutes == 0) times[i] = new JLabel(hour + ":" + zeros +ampm);
            else times[i] = new JLabel(hour + ":" + minutes +ampm);
           
        }
       
        /*
         * TODO
         * panes ,
         * 
         * 
         * 
         * 
         * 
         * scroll pane ,
         *
         * text areas ,
         */
       
        moBox = new JCheckBox();
        tuBox = new JCheckBox();
        weBox = new JCheckBox();
        thBox = new JCheckBox();
        frBox = new JCheckBox();
        saBox = new JCheckBox();
        suBox = new JCheckBox();
       
        /*
         * Buttons
         */
        initialize = new JButton("Initialize");
        initialize.addActionListener(this);
       
        add = new JButton("Add");
        add.addActionListener(this);
       
        remove = new JButton("Remove");
        remove.addActionListener(this);
       
        /*
         * File Menu
         */
       
        menu = new JMenuBar();
       
        file = new JMenu("File");
        file.setMnemonic(KeyEvent.VK_F);
       
       
        save = new JMenuItem("Save");
        save.addActionListener(this);
        save.setMnemonic(KeyEvent.VK_S);
       
        load = new JMenuItem("Load");
        load.addActionListener(this);
        load.setMnemonic(KeyEvent.VK_L);
       
        exit = new JMenuItem("Exit");
        exit.addActionListener(this);
        exit.setMnemonic(KeyEvent.VK_X);
       
        file.add(save);
        file.add(load);
        file.add(exit);
       
        /*
         * Color Menu
         */
        colors = new JMenu("Colors");
        colors.setMnemonic(KeyEvent.VK_C);
       
        colorGroup = new ButtonGroup();
               
        red = new JRadioButtonMenuItem("Red");
        red.addActionListener(this);
       
        green = new JRadioButtonMenuItem("Green");
        green.addActionListener(this);
       
        blue = new JRadioButtonMenuItem("Blue");
        blue.addActionListener(this);       
       
        colorGroup.add(red);
        colors.add(red);
        colorGroup.add(green);
        colors.add(green);
        colorGroup.add(blue);
        colors.add(blue);
       
        menu.add(file);
        menu.add(colors);
       
        /*
         * Student Info fields
         */
       
        name = new TextField("Name");
        courseName = new TextField("Course Name");
        minuteModelStart = new SpinnerNumberModel(0,0,45,15);
        hourModelStart = new SpinnerNumberModel(8,1,12,1);
        minuteModelEnd = new SpinnerNumberModel(0,0,45,15);
        hourModelEnd = new SpinnerNumberModel(8,1,12,1);
        startTimeMinutes = new JSpinner(minuteModelStart);
        startTimeHours = new JSpinner(hourModelStart);
        endTimeHours = new JSpinner(hourModelEnd);
        endTimeMinutes = new JSpinner(minuteModelEnd);
       
        scroll = new JScrollPane();   
       
        /*
         *
         * Create GUI
         *
         *
         */
       
        coursePane = new JPanel(new GridBagLayout());
        initializePane = new JPanel(new GridLayout(2,1));
        courseNames = new JPanel();

        days = new JPanel(new GridLayout(2,7));
        addPane = new JPanel(new GridLayout(1,2));
        timePane = new JPanel(new GridLayout(50,1));
        rightContainer = new JPanel(new GridBagLayout());
        courseTimes = new JPanel(new GridLayout(1,4));
        instructionPane = new JPanel();
       
        /*
         * Fill time pane with JLabels
         */
        timePane.add(new JLabel("")); //Create a blank line
        for(int i = 0; i<49 ; i++)
            timePane.add(times[i]);
        add(timePane,BorderLayout.WEST);
       
        /*
         * Add Items to panels
         */
       
        //****************************
       
        /*
         * Course display Pane
         */
       
       
        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.gridheight = 1;
        c.gridwidth = 1;
        c.weightx = .1428;
        c.fill = GridBagConstraints.HORIZONTAL;       
        coursePane.add(monday,c);   

       
        c.gridx = 1;       
        coursePane.add(tuesday,c);
       
        c.gridx = 2;       
        coursePane.add(wednesday,c);
       
        c.gridx = 3;       
        coursePane.add(thursday,c);
       
        c.gridx = 4;       
        coursePane.add(friday,c);
       
        c.gridx = 5;       
        coursePane.add(saturday,c);
       
        c.gridx = 6;       
        coursePane.add(sunday,c);
       
        coursePane.setBorder(new EmptyBorder(10,10,10,10));
       
        scroll.add(coursePane);
       
       
       
        /*
         * Initialize pane
         */
       
        initializePane.add(name);
        initializePane.add(initialize);
        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.gridheight = 2;
        c.gridwidth = 7;
        c.weighty = .2;
        //c.fill = GridBagConstraints.BOTH;
        c.anchor = GridBagConstraints.NORTH;
        rightContainer.add(initializePane, c);
       
        /*
         * Course Name
         */
        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 2;
        c.gridheight = 1;
        c.gridwidth = 7;
        c.weighty = .1;
        //c.fill = GridBagConstraints.BOTH;
        courseNames.add(courseName);
        rightContainer.add(courseNames,c);
       
        /*
         * Course times
         */
       
        courseTimes.add(startTimeHours);
        courseTimes.add(startTimeMinutes);
        courseTimes.add(endTimeHours);
        courseTimes.add(endTimeMinutes);
       
        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 3;
        c.gridheight = 1;
        c.gridwidth = 7;
        c.weighty = .1;
        //c.fill = GridBagConstraints.BOTH;
       
        rightContainer.add(courseTimes,c);
       
        /*
         * Days of the week and check boxes
         */
       
        days.add(moBox);
        days.add(tuBox);
        days.add(weBox );
        days.add(thBox);
        days.add(frBox);
        days.add(saBox);
        days.add(suBox);
        days.add(mo);
        days.add(tu);
        days.add(we);
        days.add(th);
        days.add(fr);
        days.add(sa);
        days.add(su);       
       
        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 4;
        c.gridheight = 2;
        c.gridwidth = 7;
        c.weighty = .2;
        //c.fill = GridBagConstraints.BOTH;
       
        rightContainer.add(days,c);
       
       
        /*
         * Add and remove
         */
       
        addPane.add(add);
        addPane.add(remove);
       
        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 6;
        c.gridheight = 1;
        c.gridwidth = 7;
        c.weighty = .1;
        //c.fill = GridBagConstraints.BOTH;
       
        rightContainer.add(addPane,c);
       
        /*
         * Instructions
         */
       
        instructions = new JTextArea("Instructions go here");
        instructionPane.add(instructions);
       
        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 7;
        c.gridheight = 3;
        c.gridwidth = 7;
        c.weighty = .3;
        c.fill = GridBagConstraints.BOTH;
       
        rightContainer.add(instructionPane,c);
       
       
       
       
        add(scroll,BorderLayout.CENTER);
        add(menu,BorderLayout.NORTH);
        add(rightContainer,BorderLayout.EAST);
       
        setBounds(200,200,800,800);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
       
       
    }
    public static void main(String args[])
    {
        ScheduleGUI sg1 = new ScheduleGUI();
        sg1.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==add)            addClass();
       
        else if(e.getSource()==save)    browse.save(menu, generateString());
       
        else if(e.getSource()==red)     getContentPane().setBackground(Color.red);
       
        else if(e.getSource()==green)   getContentPane().setBackground(Color.green);
       
        else if(e.getSource()==blue)    getContentPane().setBackground(Color.blue);

       
    }
   
    private void addClass()
    {
       
    }
   
    private String generateString()
    {
       
        return "";
    }

}

Everywhere I read, I see I want this.getContentPane.setBackground(Color);
This doesn't work for me. It didn't on the last project either, and I just let it slide thinking I'd figure it out. Now here I am, same problem. Any help would be appreciated.

Notes:
You might get some "errors". Just don't hit save. I use a custom class Browse for my loading and saving. I took out the mentions of it that I think should be a problem.

I thought I just wasn't accessing the colors properly, but I added a setBackground call in the initialization and still nothing. What am I doing wrong? I have to turn this in in 1 hour, as well =/

Any help greatly appreciated.
 
Technology news on Phys.org
  • #3
That makes sense why you can't see the colors, then. Thanks.
 

1. What does the setBackground(Color) method do in Java?

The setBackground(Color) method in Java is used to set the background color of a component, such as a button or panel. It takes in a Color object as a parameter and changes the background color of the component to the specified color.

2. Why am I having trouble setting the background color of my Java program?

There could be several reasons why you are having trouble setting the background color of your Java program. Some common issues include not importing the java.awt.Color library, not using the correct syntax for the setBackground method, or setting the background color of a component that is not visible on the screen.

3. How can I set a custom color as the background in Java?

In order to set a custom color as the background in Java, you can create a Color object using the RGB values of the color you want. For example, to create a custom purple color, you can use the code: Color purple = new Color(128, 0, 128). Then, you can use the setBackground method to set the background color of your component to the custom color.

4. Can I change the background color of a specific component in Java?

Yes, you can change the background color of a specific component in Java by using the setBackground method on that specific component. This allows you to have different background colors for different components in your program.

5. Is it possible to animate the background color in Java?

Yes, it is possible to animate the background color in Java by using a timer and changing the color gradually over time. You can also use libraries such as JavaFX to create more complex animations for your background color.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
8
Views
203
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
10
Views
1K
Replies
2
Views
737
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
808
  • Programming and Computer Science
Replies
6
Views
3K
Back
Top