Java Problem with Java Color (setBackground(Color))

  • Thread starter Thread starter BiGyElLoWhAt
  • Start date Start date
  • Tags Tags
    Color Java
Click For Summary
The discussion centers on issues with setting the background color of a content pane in a Java application. The user reports that the method this.getContentPane().setBackground(Color) is not functioning as expected, despite attempts to troubleshoot by adding a setBackground call during initialization. They mention using a custom class for loading and saving, which may be contributing to the problem. The urgency of the situation is emphasized, as the user has a deadline to meet. A referenced Stack Overflow explanation clarifies why the colors might not be visible, suggesting that there may be underlying issues affecting the display. Overall, the user seeks assistance to resolve the color setting problem quickly.
BiGyElLoWhAt
Gold Member
Messages
1,637
Reaction score
138
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
That makes sense why you can't see the colors, then. Thanks.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
Replies
1
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 2 ·
Replies
2
Views
13K
  • · Replies 18 ·
Replies
18
Views
9K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K