JFrame background Color

cant set JFrame background Color in netbeans IDE

Page 1 of 1

11 Replies - 13634 Views - Last Post: 15 July 2009 - 08:58 AM Rate Topic: -----

#1 abubakarislam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-May 09

JFrame background Color

Posted 27 May 2009 - 03:55 PM

Hi

i tried setting the background color of a JFrame (inside the function Add_All_The_Panels_To_The_GUI()), but when i ran the code in Netbeans the color remain default. please help if you can.

thanks inadvance




import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;


public class Test_GUI
{
	 /**
	 * @param args the command line arguments
	 */
	static final public int LIST_MAX = 500;

	public static void main(String[] args)
	{
	  Test_GUI_Model Model = new Test_GUI_Model();
	  Test_GUI_View  View  = new Test_GUI_View("Main GUI");
	  Test_GUI_Controller Controller = new Test_GUI_Controller(View, Model);
	}

}

class Test_GUI_Model
{
   /* Function to exit GUI */
   public void GUI_Exit()
   {
	  int user_option;
	  user_option = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit Application",
														"Message Dialog",
														JOptionPane.YES_NO_OPTION);

	  if(JOptionPane.YES_OPTION == user_option){System.exit(0);}
   }
   /* Function to save name enter   */
   public void GUI_Save_Name()
   {
	  JOptionPane.showMessageDialog(null, "Save Name Seleted",
										  "Message Dialog",
										  JOptionPane.PLAIN_MESSAGE);
   }
   /* Function to connect to the outside world */
   public void GUI_Connect()
   {
	  JOptionPane.showMessageDialog(null, "Connect Selected",
										  "Message Dialog",
										  JOptionPane.PLAIN_MESSAGE);
   }
}

class Test_GUI_View extends JFrame
{
   JButton exit				= new JButton("Exit");
   JButton save_name		   = new JButton("Save Name");
   JButton connect			 = new JButton("Connect");
   JPanel  panel_buttons	   = new JPanel();

   DefaultListModel list_model = new DefaultListModel();
   JList	name_list		  = new JList(list_model);
   JPanel   panel_list		 = new JPanel();
   JScrollPane scroller_list   = new JScrollPane();
   

   /* GUI constructor, Setup the GUI */
   Test_GUI_View(String title)
   {
	  super(title);
	  this.setBounds(250, 270, 320, 400);
	  this.setDefaultCloseOperation(EXIT_ON_CLOSE);

	  this.Setup_Buttons_Panel();
	  this.Setup_Contact_Lists_Panel();
	  this.Add_All_The_Panels_To_The_GUI();
  
	  this.setVisible(true);
   }
   /* Setup the Buttons Event Handlers */
   void buttons_action_listeners(ActionListener event_handler)
   {
	  exit.setActionCommand("EXIT");
	  exit.addActionListener(event_handler);
	  save_name.setActionCommand("SAVE");
	  save_name.addActionListener(event_handler);
	  connect.setActionCommand("CONNECT");
	  connect.addActionListener(event_handler);
   }
   /*
	*/
   private void Setup_Buttons_Panel()
   {
	  panel_buttons.setLayout(new GridLayout(4,1,2,2));	
	  panel_buttons.add(exit);
	  panel_buttons.add(save_name);
	  panel_buttons.add(connect);
   }
   /*
	*/
   private void Setup_Contact_Lists_Panel()
   {
	  int position = 0;

	  name_list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
	  list_model.add(position, "Contact List Empty");

	  scroller_list.setAlignmentY(LEFT_ALIGNMENT);
	  scroller_list.setPreferredSize(new Dimension(150,50));
	  scroller_list.getViewport().add(name_list);

	  panel_list.add(scroller_list);
	  panel_list.setLayout(new BoxLayout(panel_list,BoxLayout.PAGE_AXIS));
   }
   /*
	*/
   private void Add_All_The_Panels_To_The_GUI()
   {	
	  Container content_pane = getContentPane();
	  content_pane.add(panel_list, BorderLayout.WEST);
	  content_pane.add(panel_buttons, BorderLayout.CENTER);
	  content_pane.setBackground(new Color(10,10,10));
   }
}

class Test_GUI_Controller implements ActionListener
{
   Test_GUI_View  view;
   Test_GUI_Model model;

   public Test_GUI_Controller(Test_GUI_View v, Test_GUI_Model m)
   {
	  /*create the model and the view*/
	  this.view  = v;
	  this.model = m;
	  /* Assign this action lister (event handler) to the buttons
	   * Added in Test_GUI_View
	   */
	  view.buttons_action_listeners(this);
   }

   public void actionPerformed(ActionEvent event)
   {
	  String event_command = event.getActionCommand();

	  if (this.view.exit.getActionCommand() == event_command)
	  {
		 this.model.GUI_Exit();
	  }
	  else if(this.view.connect.getActionCommand() == event_command)
	  {
		 this.model.GUI_Connect();
	  }
	  else if (this.view.save_name.getActionCommand() == event_command)
	  {
		 this.model.GUI_Save_Name();
	  }
   }
}



Is This A Good Question/Topic? 0
  • +

Replies To: JFrame background Color

#2 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: JFrame background Color

Posted 27 May 2009 - 09:07 PM

Um.. have you tried
this.setBackgroundColor(Color.blue);
blue could be substituted for any color it will accept
Was This Post Helpful? 0
  • +
  • -

#3 Dantheman  Icon User is offline

  • D.I.C Regular

Reputation: 34
  • View blog
  • Posts: 445
  • Joined: 27-May 09

Re: JFrame background Color

Posted 27 May 2009 - 09:59 PM

Follow Fuzzyness' advice, but for the future - do not use code wizards if you don't understand the code yourself. Code wizards are designed for people who know exactly what they're doing, who could've wrote the code themselves but need to save some time. If you are a beginner, learn to hand-code this stuff.
Was This Post Helpful? 0
  • +
  • -

#4 ralph_komer  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 71
  • Joined: 12-May 09

Re: JFrame background Color

Posted 28 May 2009 - 01:36 AM

You cannot set the background color to the frame directly.
You need to set the background color of the contentpane to see the color.

Use the below piece of code:


content_pane.setBackground(Color.black);




inside your Add_All_The_Panels_To_The_GUI() method.

It will fix the issue.
Was This Post Helpful? 0
  • +
  • -

#5 abubakarislam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-May 09

Re: JFrame background Color

Posted 28 May 2009 - 08:16 AM

Hi All

thanks alot for your help, ill try those solutions, ill update the page with my outcome ASAP. i didnt use code wizard, i wrote the code myself.

thanks again to everyone.
Was This Post Helpful? 0
  • +
  • -

#6 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: JFrame background Color

Posted 28 May 2009 - 05:05 PM

View Postralph_komer, on 28 May, 2009 - 12:36 AM, said:

You cannot set the background color to the frame directly.
You need to set the background color of the contentpane to see the color.

Use the below piece of code:


content_pane.setBackground(Color.black);




inside your Add_All_The_Panels_To_The_GUI() method.

It will fix the issue.

I use JCreator, dontuse netbeans so Im not sure if it is different. but you can set the frame background color to a certain color and it will work. Not sure about netbeans. Gonna have to suck it up and learn some netbeans soon xD
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: JFrame background Color

Posted 29 May 2009 - 09:45 PM

View PostDantheman, on 27 May, 2009 - 08:59 PM, said:

Follow Fuzzyness' advice, but for the future - do not use code wizards if you don't understand the code yourself. Code wizards are designed for people who know exactly what they're doing, who could've wrote the code themselves but need to save some time. If you are a beginner, learn to hand-code this stuff.

Agree 100%
Java GUI is so easy to program no need at all for GUI generators
Was This Post Helpful? 0
  • +
  • -

#8 ronaldjames  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 42
  • Joined: 29-June 09

Re: JFrame background Color

Posted 07 July 2009 - 06:52 AM

Just simple

Try this..


contentPane_object.setBackground(Color.RED);

RED can be replaced by any color that is available.
Was This Post Helpful? 0
  • +
  • -

#9 ronaldjames  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 42
  • Joined: 29-June 09

Re: JFrame background Color

Posted 07 July 2009 - 07:13 AM

Simple..

contentPane_object.setBackground(Color.RED);

RED can be replaced with any color which is available.

Was This Post Helpful? 0
  • +
  • -

#10 jinnyishere  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 127
  • Joined: 06-July 09

Re: JFrame background Color

Posted 07 July 2009 - 04:07 PM

Hey!! I'm the type of person who learns through examples...So often times I would browse for different examples when I get stuck on something...Hence, if you are like me maybe this will help you...a simple basic example on background color changing...hope it will help


/* Need to import all these for the program to work
	This program allows the user to push a button 
	And it will change the background color
*
*/


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

public class ColorWindow extends JFrame
{
	/* In here we declare the name of the button and the message
		JButton creates a button with a name, and JLabel writes
		The message on the window. Also we create a panel using
		JPanel, and we set the size of the window		
	*
	*/ 
	private JLabel messageLabel;
	private JButton redButton;
	private JButton yellowButton;
	private JButton blueButton;
	private JPanel panel;
	private final int WINDOW_WIDTH = 200;
	private final int WINDOW_HEIGHT = 180;

public ColorWindow()
{
	/* We set the title of the window	
	*
	*/
	setTitle("Colors");

	/* We set the size of the window
	*
	*/
	setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
	
	/* Here if the X on the top right corner is pushed, the window will close
	*
	*/

	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
	/* Here we will set what to display on the window
	*
	*/
	
	messageLabel = new JLabel("Click a button to select a color.");
	
	/* Here we set the name of the buttons
	*
	*/
	
	redButton = new JButton("Red");
	yellowButton = new JButton("Yellow");
	blueButton = new JButton("Blue");
	
	/* Here we assign the action to the button
	*
	*/

	redButton.addActionListener(new RedButtonListener());
	yellowButton.addActionListener(new YellowButtonListener());
	blueButton.addActionListener(new BlueButtonListener());
	 
	/* Here we create the panel
	*
	*/
	panel = new JPanel();
	
	/* Here we add everything to the panel
	*
	*/

	panel.add(messageLabel);
	panel.add(redButton);
	panel.add(yellowButton);
	panel.add(blueButton);
	
	add(panel);
	
	/* Finally, we set the window to be visible by making it true
	*
	*/
	
	setVisible(true);
}

/* Now we have to create action for the buttons
	Here will determines what will happen if a button is push
*
*/
 
private class RedButtonListener implements ActionListener
{
	public void actionPerformed(ActionEvent e)
	{
		panel.setBackground(Color.RED);
		messageLabel.setForeground(Color.BLUE);
	}
}

private class YellowButtonListener implements ActionListener
{
	public void actionPerformed(ActionEvent e)
	{
		panel.setBackground(Color.YELLOW);
		messageLabel.setForeground(Color.BLACK);
	}
}

private class BlueButtonListener implements ActionListener
{
	public void actionPerformed(ActionEvent e)
	{
		panel.setBackground(Color.BLUE);
		messageLabel.setForeground(Color.YELLOW);
	}
}


	public static void main(String[] args)
	{
		new ColorWindow();
	}
}



Was This Post Helpful? 0
  • +
  • -

#11 ronaldjames  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 42
  • Joined: 29-June 09

Re: JFrame background Color

Posted 09 July 2009 - 12:44 AM

View Postjinnyishere, on 8 Jul, 2009 - 05:07 AM, said:

Hey!! I'm the type of person who learns through examples...So often times I would browse for different examples when I get stuck on something...Hence, if you are like me maybe this will help you...a simple basic example on background color changing...hope it will help


/* Need to import all these for the program to work
	This program allows the user to push a button 
	And it will change the background color
*
*/


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

public class ColorWindow extends JFrame
{
	/* In here we declare the name of the button and the message
		JButton creates a button with a name, and JLabel writes
		The message on the window. Also we create a panel using
		JPanel, and we set the size of the window		
	*
	*/ 
	private JLabel messageLabel;
	private JButton redButton;
	private JButton yellowButton;
	private JButton blueButton;
	private JPanel panel;
	private final int WINDOW_WIDTH = 200;
	private final int WINDOW_HEIGHT = 180;

public ColorWindow()
{
	/* We set the title of the window	
	*
	*/
	setTitle("Colors");

	/* We set the size of the window
	*
	*/
	setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
	
	/* Here if the X on the top right corner is pushed, the window will close
	*
	*/

	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
	/* Here we will set what to display on the window
	*
	*/
	
	messageLabel = new JLabel("Click a button to select a color.");
	
	/* Here we set the name of the buttons
	*
	*/
	
	redButton = new JButton("Red");
	yellowButton = new JButton("Yellow");
	blueButton = new JButton("Blue");
	
	/* Here we assign the action to the button
	*
	*/

	redButton.addActionListener(new RedButtonListener());
	yellowButton.addActionListener(new YellowButtonListener());
	blueButton.addActionListener(new BlueButtonListener());
	 
	/* Here we create the panel
	*
	*/
	panel = new JPanel();
	
	/* Here we add everything to the panel
	*
	*/

	panel.add(messageLabel);
	panel.add(redButton);
	panel.add(yellowButton);
	panel.add(blueButton);
	
	add(panel);
	
	/* Finally, we set the window to be visible by making it true
	*
	*/
	
	setVisible(true);
}

/* Now we have to create action for the buttons
	Here will determines what will happen if a button is push
*
*/
 
private class RedButtonListener implements ActionListener
{
	public void actionPerformed(ActionEvent e)
	{
		panel.setBackground(Color.RED);
		messageLabel.setForeground(Color.BLUE);
	}
}

private class YellowButtonListener implements ActionListener
{
	public void actionPerformed(ActionEvent e)
	{
		panel.setBackground(Color.YELLOW);
		messageLabel.setForeground(Color.BLACK);
	}
}

private class BlueButtonListener implements ActionListener
{
	public void actionPerformed(ActionEvent e)
	{
		panel.setBackground(Color.BLUE);
		messageLabel.setForeground(Color.YELLOW);
	}
}


	public static void main(String[] args)
	{
		new ColorWindow();
	}
}





Good to know tht u r putting effort to learn JAVA.

So here is another example for the same program but here instead of JButton I used JList...

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.event.*;
public class listdemo extends JFrame
{
JList colorlist;
Container c;
String colornames[]={"red","gray","green","pink","blue","black"};
Color colors[]={Color.red,Color.gray,  Color.green, Color.pink, Color.blue, Color.black};
public listdemo()
{
super("list test");
c=getContentPane();
c.setLayout(new FlowLayout());
colorlist=new JList(colornames);
colorlist.setVisibleRowCount(3);
c.add(new JScrollPane(colorlist));
colorlist.addListSelectionListener( new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent e)
{
c.setBackground(colors[colorlist.getSelectedIndex()]);
}
}
);
setSize(150,150);
show();
}
public static void main(String args[])
{
listdemo a=new listdemo();
a.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
}

Was This Post Helpful? 0
  • +
  • -

#12 krasavitsa01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-July 09

Re: JFrame background Color

Posted 15 July 2009 - 08:58 AM

Hello,

It is not even necessary to change the background color. Simply add a Panel from the Palette and then go to Properties and next to the background and [number, number number], click on the button with "...". Choose your color and press enter.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1