Changing the Color of My JPanel

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

38 Replies - 1120 Views - Last Post: 16 May 2012 - 11:59 AM Rate Topic: -----

#1 SnoBunny85  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 02-March 10

Changing the Color of My JPanel

Posted 04 May 2012 - 12:42 PM

Ok, I am getting frustrated. I have created this GUI and am trying to make the background of the titleHolder panel to change to another color but no matter what I do I can not get it to work. I have tried creating a totally new panel and I have tried not using a panel at all and no matter what it won't work. I want to take my GUI have two panels.. one to hold the title and one to hold the buttons. I want to make the background behind the title to be a green color I have chosen and the front color to be another lighter green color I have chosen... but it won't work. I can get the title name to show up in the selected color but can't get the background to change. Please help!!

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

public class MainFramePanel extends JPanel {
	
	JButton ExitSystem, InventoryDepartment, AccountingDepartment, Invoices;
	JLabel title;
	

    public MainFramePanel() {
    	
    	//create panel for title
    	//setLayout(new BorderLayout());
    	//JPanel primary = new JPanel();
    	//add (primary, BorderLayout.NORTH);
    	
       	   	
    	//create panel to hold four main buttons
    	JPanel buttonHolder = new JPanel();
    	buttonHolder.setLayout (new BoxLayout (buttonHolder, BoxLayout.Y_AXIS));
    	buttonHolder.setForeground(new Color(240, 250, 240));
    	buttonHolder.setBackground(new Color(46, 139, 87));
    	buttonHolder.setPreferredSize(new Dimension(300,300));
    	add(buttonHolder, BorderLayout.SOUTH);
    	
    	//create a panel to hold the title
    	setLayout(new BorderLayout());
    	JPanel titleHolder = new JPanel();
    	titleHolder.setBackground(new Color(46, 139, 87));
    	titleHolder.setForeground(new Color(240, 250, 240));
    	titleHolder.setPreferredSize(new Dimension(300,300));
    	add(titleHolder, BorderLayout.NORTH);
    	
    	//create the title itself
    	title = new JLabel("Warehouse Inventory System");
    	title.setPreferredSize (new Dimension(600,100));
        title.setForeground(new Color(46, 139, 87);
        title.setBackground(new Color(240, 250, 240);
    	Font font = new Font("Bradley Hand ITC", Font.PLAIN, 45);
    	title.setFont(font);
    	add(title, BorderLayout.NORTH);
    	
    	//add buttons to the main page and color them correctly to match main page
    	ExitSystem = new JButton("Exit System");
    	ExitSystem.setForeground(new Color(46, 139, 87));
    	ExitSystem.setBackground(new Color(240, 250, 240));
		ExitSystem.setAlignmentX(Component.CENTER_ALIGNMENT);
		Font font1 = new Font("Bradley Hand ITC", Font.PLAIN, 30);
		ExitSystem.setFont(font1);
		ExitSystem.setMaximumSize(new Dimension(400, 100));
			
    	InventoryDepartment = new JButton("Inventory Department");
    	InventoryDepartment.setForeground(new Color(46, 139, 87));
    	InventoryDepartment.setBackground(new Color(240, 250, 240));
    	InventoryDepartment.setAlignmentX(Component.CENTER_ALIGNMENT);
    	InventoryDepartment.setFont(font1);  
    	InventoryDepartment.setMaximumSize(new Dimension(400, 100));

    	AccountingDepartment = new JButton("Accounting Department");
    	AccountingDepartment.setForeground(new Color(46, 139, 87));
    	AccountingDepartment.setBackground(new Color(240, 250, 240));
		AccountingDepartment.setAlignmentX(Component.CENTER_ALIGNMENT); 
		AccountingDepartment.setFont(font1);  
		AccountingDepartment.setMaximumSize(new Dimension(400, 100));

    	Invoices = new JButton("Invoices");
    	Invoices.setForeground(new Color(46, 139, 87));
    	Invoices.setBackground(new Color(240, 250, 240));
		Invoices.setAlignmentX(Component.CENTER_ALIGNMENT);   
		Invoices.setFont(font1);
		Invoices.setMaximumSize(new Dimension(400, 100));

    	//add buttons to the display
    	buttonHolder.add(Box.createHorizontalStrut(10));
    	buttonHolder.add(ExitSystem);
    	buttonHolder.add(Box.createHorizontalStrut(10));
    	buttonHolder.add(InventoryDepartment);
    	buttonHolder.add(Box.createHorizontalStrut(10));
    	buttonHolder.add(AccountingDepartment);
    	buttonHolder.add(Box.createHorizontalStrut(10));
    	buttonHolder.add(Invoices);
    	buttonHolder.add(Box.createHorizontalStrut(10));
    	
    	//add display panel to main page
    	add(buttonHolder, BorderLayout.CENTER);
    	
    	//add listeners
    	ButtonListener listener = new ButtonListener();
    	ExitSystem.addActionListener(listener);
    	InventoryDepartment.addActionListener(listener);
    	AccountingDepartment.addActionListener(listener);
    	Invoices.addActionListener(listener);
    }
    	
    	private class ButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			if(event.getSource() == ExitSystem)
    				title.setText("Exit Pushed");
    			if(event.getSource() == InventoryDepartment)
    				title.setText("Inventory Hit");
    			if(event.getSource() == AccountingDepartment)
    				title.setText("Accounting Hit");
    			if(event.getSource() == Invoices)
    				title.setText("Invoices Hit");
    		}
    	}
    	
    	
    	
    
    
    
}



This is main in case you need it...
import javax.swing.*;


public class WarehouseInventorySystem {
        
   
       public static void main(String[] args) {
        //Displaying the main page GUI
        JFrame frame = new JFrame ("Main Page");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new MainFramePanel());
		frame.pack();
		frame.setVisible(true);
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Changing the Color of My JPanel

#2 pbl  Icon User is online

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

Reputation: 8017
  • View blog
  • Posts: 31,124
  • Joined: 06-March 08

Re: Changing the Color of My JPanel

Posted 04 May 2012 - 01:06 PM

Your code won't even compile

		title.setForeground(new Color(46, 139, 87);
		title.setBackground(new Color(240, 250, 240);


You're sure you post the good code ?

Any how. Each region of a BorderLayout can only contain one Component

		add(titleHolder, BorderLayout.NORTH);

		//create the title itself
		title = new JLabel("Warehouse Inventory System");
		title.setPreferredSize (new Dimension(600,100));
		title.setForeground(new Color(46, 139, 87));
		title.setBackground(new Color(240, 250, 240));
		Font font = new Font("Bradley Hand ITC", Font.PLAIN, 45);
		title.setFont(font);
		// here your title Jpanel replace titleHolder that was there
		add(title, BorderLayout.NORTH);


Was This Post Helpful? 1
  • +
  • -

#3 SnoBunny85  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 02-March 10

Re: Changing the Color of My JPanel

Posted 04 May 2012 - 01:25 PM

/**
 * Project: Warehouse Inventory System
 * Name: Cheryl Minor
 * Date: May 2, 2012
 *
 * Rewriting the Warehouse Inventory System that I created in Visual Studio
 */
 
import javax.swing.*;


public class WarehouseInventorySystem {
        
   
       public static void main(String[] args) {
        //Displaying the main page GUI
        JFrame frame = new JFrame ("Main Page");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new MainFramePanel());
		frame.pack();
		frame.setVisible(true);
    }
    
    
}




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

public class MainFramePanel extends JPanel {
	
	JButton ExitSystem, InventoryDepartment, AccountingDepartment, Invoices;
	JLabel title;
	

    public MainFramePanel() {
    	
    	//create panel for title
    	//setLayout(new BorderLayout());
    	//JPanel primary = new JPanel();
    	//add (primary, BorderLayout.NORTH);
    	
       	   	
    	//create panel to hold four main buttons
    	JPanel buttonHolder = new JPanel();
    	buttonHolder.setLayout (new BoxLayout (buttonHolder, BoxLayout.Y_AXIS));
    	buttonHolder.setForeground(new Color(240, 250, 240));
    	buttonHolder.setBackground(new Color(46, 139, 87));
    	buttonHolder.setPreferredSize(new Dimension(300,300));
    	add(buttonHolder, BorderLayout.SOUTH);
    	
    	//create a panel to hold the title
    	setLayout(new BorderLayout());
    	JPanel titleHolder = new JPanel();
    	titleHolder.setBackground(new Color(46, 139, 87));
    	titleHolder.setForeground(new Color(240, 250, 240));
    	titleHolder.setPreferredSize(new Dimension(300,300));
    	add(titleHolder, BorderLayout.NORTH);
    	
    	//create the title itself
    	title = new JLabel("Warehouse Inventory System");
    	title.setPreferredSize (new Dimension(600,100));
    	Font font = new Font("Bradley Hand ITC", Font.PLAIN, 45);
    	title.setFont(font);
    	add(title, BorderLayout.NORTH);
    	
    	//add buttons to the main page and color them correctly to match main page
    	ExitSystem = new JButton("Exit System");
    	ExitSystem.setForeground(new Color(46, 139, 87));
    	ExitSystem.setBackground(new Color(240, 250, 240));
		ExitSystem.setAlignmentX(Component.CENTER_ALIGNMENT);
		Font font1 = new Font("Bradley Hand ITC", Font.PLAIN, 30);
		ExitSystem.setFont(font1);
		ExitSystem.setMaximumSize(new Dimension(400, 100));
			
    	InventoryDepartment = new JButton("Inventory Department");
    	InventoryDepartment.setForeground(new Color(46, 139, 87));
    	InventoryDepartment.setBackground(new Color(240, 250, 240));
    	InventoryDepartment.setAlignmentX(Component.CENTER_ALIGNMENT);
    	InventoryDepartment.setFont(font1);  
    	InventoryDepartment.setMaximumSize(new Dimension(400, 100));

    	AccountingDepartment = new JButton("Accounting Department");
    	AccountingDepartment.setForeground(new Color(46, 139, 87));
    	AccountingDepartment.setBackground(new Color(240, 250, 240));
		AccountingDepartment.setAlignmentX(Component.CENTER_ALIGNMENT); 
		AccountingDepartment.setFont(font1);  
		AccountingDepartment.setMaximumSize(new Dimension(400, 100));

    	Invoices = new JButton("Invoices");
    	Invoices.setForeground(new Color(46, 139, 87));
    	Invoices.setBackground(new Color(240, 250, 240));
		Invoices.setAlignmentX(Component.CENTER_ALIGNMENT);   
		Invoices.setFont(font1);
		Invoices.setMaximumSize(new Dimension(400, 100));

    	//add buttons to the display
    	buttonHolder.add(Box.createHorizontalStrut(10));
    	buttonHolder.add(ExitSystem);
    	buttonHolder.add(Box.createHorizontalStrut(10));
    	buttonHolder.add(InventoryDepartment);
    	buttonHolder.add(Box.createHorizontalStrut(10));
    	buttonHolder.add(AccountingDepartment);
    	buttonHolder.add(Box.createHorizontalStrut(10));
    	buttonHolder.add(Invoices);
    	buttonHolder.add(Box.createHorizontalStrut(10));
    	
    	//add display panel to main page
    	add(buttonHolder, BorderLayout.CENTER);
    	
    	//add listeners
    	ButtonListener listener = new ButtonListener();
    	ExitSystem.addActionListener(listener);
    	InventoryDepartment.addActionListener(listener);
    	AccountingDepartment.addActionListener(listener);
    	Invoices.addActionListener(listener);
    }
    	
    	private class ButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			if(event.getSource() == ExitSystem)
    				title.setText("Exit Pushed");
    			if(event.getSource() == InventoryDepartment)
    				title.setText("Inventory Hit");
    			if(event.getSource() == AccountingDepartment)
    				title.setText("Accounting Hit");
    			if(event.getSource() == Invoices)
    				title.setText("Invoices Hit");
    		}
    	}
    	
    	
    	
    
    
    
}



ahhh, i got it!! Thankyou!!
I just added title to my titleholder jpanel!!
anyway you can suggest how i would change my action listeners so that when i click my buttons that new jpanels will pop up??
if not, i will spend some time on it and see if i can figure it out. Thanks so much!
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is online

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

Reputation: 8017
  • View blog
  • Posts: 31,124
  • Joined: 06-March 08

Re: Changing the Color of My JPanel

Posted 04 May 2012 - 01:33 PM

Not a good idea to add dynamically JComponent to other, you can screw up your layout quite fast. Better to look at other alternatives like make the JPanel setVisible(false) and just change its visibility in your actionPerformed()
Was This Post Helpful? 1
  • +
  • -

#5 SnoBunny85  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 02-March 10

Re: Changing the Color of My JPanel

Posted 04 May 2012 - 01:38 PM

so just add the panel to the MainFramePanel and just set its visibility to false and then when the button is clicked change the visibility to true???
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is online

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

Reputation: 8017
  • View blog
  • Posts: 31,124
  • Joined: 06-March 08

Re: Changing the Color of My JPanel

Posted 04 May 2012 - 01:43 PM

Yup :)

Don't really see what you try to achieve but in your NORTH region you can:

   JPanel north = new JPanel(new GridLayout(2,1));
   buttonHolder.setVisible(false);
   north.add(title);
   north.add(buttonHolder);
   add(north, BorderLayout.NORTH);


   public void actionPerformed(ActionEvent e) {
     ...
     buttonHolder.setVisible(true);


Happy Swinging
Was This Post Helpful? 1
  • +
  • -

#7 SnoBunny85  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 02-March 10

Re: Changing the Color of My JPanel

Posted 04 May 2012 - 02:09 PM

I am writing an inventory system where when i click the inventory department button another panel pops up that displays my inventory department... i am not very good with the action portion of these buttons because we did not cover them that much in school...

	//add Inventory Department Panel
    	JPanel inventoryDepartment = new JPanel(new GridLayout(2,1));
    	buttonHolder.setVisible(false);
    	inventoryDepartment.add(title);
    	inventoryDepartment.add(buttonHolder);
    	add(inventoryDepartment, BorderLayout.NORTH);
    	
    	public void actionPerformed(ActionEvent e)
    	{
    		buttonHolder.setVisible(true):
    	} 



i already have this:
//add listeners
    	ButtonListener listener = new ButtonListener();
    	ExitSystem.addActionListener(listener);
    	InventoryDepartment.addActionListener(listener);
    	AccountingDepartment.addActionListener(listener);
    	Invoices.addActionListener(listener);


and this
	private class ButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			//exiting the system
    			if(event.getSource() == ExitSystem)
    			    System.exit(0);
    			
    			//showing the inventory department
    			if(event.getSource() == InventoryDepartment)
    				inventoryDepartment.setVisible(true);
    				
    			//showing the accounting department	
    			if(event.getSource() == AccountingDepartment)
    				accountingDepartment.setVisible(true);
    				
    			//showing the invoices	
    			if(event.getSource() == Invoices)
    				invoices.setVisible(true);
    		}
    	}
    	


apparently the inventoryDepartment.setVisible(true) statement is wrong because I get an error when i compile it... just not sure where to go....
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is online

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

Reputation: 8017
  • View blog
  • Posts: 31,124
  • Joined: 06-March 08

Re: Changing the Color of My JPanel

Posted 04 May 2012 - 02:22 PM

You will have to put your JPanel inventoryDepartment in your instance variables so the actionPerformed() method can see it

If you want to display different JPanel based on a certain context I strongly suggest the use of a CardLayout

http://docs.oracle.c...CardLayout.html

or even better a JTabbedPane

http://docs.oracle.c...TabbedPane.html

with the JTabbedPane the user can navigate from one JPanel to another without having your code to switch from one panel to another
Was This Post Helpful? 1
  • +
  • -

#9 SnoBunny85  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 02-March 10

Re: Changing the Color of My JPanel

Posted 05 May 2012 - 09:28 AM

was wondering if you could look at my code and tell me how to fix this... it says it can't find symbol class deleteListener...

	public class ButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			//exiting the system
    			if(event.getSource() == ExitSystem)
    			    System.exit(0);
    			
    			
    			//inventory department
    			//selecting inventory to add or delete
    			if(event.getSource() == InventoryDepartment)
    			{
    				//creating the new frame for the inventory department page
    				JFrame frame2 = new JFrame("Inventory Department");
    				frame2.setVisible(true);
    				frame2.setSize(600,600);
    				    				   				
    				//main panel for the inventory department   				
    				JPanel inventoryDepartment = new JPanel();
    				inventoryDepartment.setLayout(new BorderLayout());
    				inventoryDepartment.setBackground(new Color(46, 139, 87));
    				inventoryDepartment.setPreferredSize(new Dimension(300,300));
    				frame2.add(inventoryDepartment);
    				
    				//inventory panel that will hold the inventory list
    				JPanel inventory = new JPanel();
    				inventory.setLayout(new BorderLayout());
    				inventory.setBackground(new Color(240, 250, 240));
    				inventory.setMaximumSize(new Dimension(100, 100));
    				inventoryDepartment.add(inventory, BorderLayout.SOUTH);
    				
    				
    				//panel to hold the access buttons to other sites
    				JPanel buttons = new JPanel();
    				buttons.setLayout(new BorderLayout());
    				buttons.setBackground(new Color(46, 139, 87));
    				buttons.setPreferredSize(new Dimension(200, 200));
    				inventoryDepartment.add(buttons, BorderLayout.NORTH);
    				
    				//title of page
    				JLabel title = new JLabel("Inventory Department", SwingConstants.CENTER);
    				title.setForeground(new Color(240, 250, 240));
    				Font titleFont = new Font("Bradley Hand ITC", Font.PLAIN, 50);
					title.setFont(titleFont);
    				inventoryDepartment.add(title, BorderLayout.NORTH);
    				    				
    				//creating the inventory list
    				final DefaultListModel listModel;
    				listModel = new DefaultListModel();
    				listModel.addElement("item 1");
    				listModel.addElement("item 2");
    				listModel.addElement("item 3");
    				listModel.addElement("item 4");
    				listModel.addElement("item 5");
    				
    				final JList list;
    				list = new JList(listModel);
    				list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    				list.setSelectedIndex(0);
    				list.setVisibleRowCount(5);
    				JScrollPane listScrollPane = new JScrollPane(list);
    				
    				String addString = "Add Item";
    				final JButton addButton = new JButton(addString);
    				//AddListener addListener = new AddListener(addButton);
    				addButton.setActionCommand(addString);
    				//addButton.addActionListener(addListener);
    				addButton.setEnabled(false);
    				
    				String deleteString = "Delete Item";
    				final JButton deleteButton = new JButton(deleteString);
    				deleteButton.setActionCommand(deleteString);
    				deleteButton.addActionListener(new deleteListener());
    				
    				JTextField itemName = new JTextField(10);
    				//item.addActionListener(addListener);
    				//item.getDocument().addDocumentListener(String item = listModel.getElementAt(list.getSelectedIndex()).toString());
    				
    				inventory.add(list);
    				inventory.add(addButton, BorderLayout.EAST);
    				inventory.add(deleteButton, BorderLayout.WEST);
    				inventory.add(Box.createHorizontalStrut(5));
    				inventory.add(new JSeparator(SwingConstants.VERTICAL));
    				inventory.add(Box.createHorizontalStrut(5));
    				inventory.add(itemName);
    				inventory.add(listScrollPane, BorderLayout.CENTER);
    				
    				class deleteListener implements ActionListener
    				{
    					public void actionPerformed(ActionEvent e)
    					{
    						int index = list.getSelectedIndex();
    						listModel.remove(index);
    						
    						int size = listModel.getSize();
    						if(size == 0)
    						{
    							deleteButton.setEnabled(false);
    						}
    						else
    						{
    							if(index == listModel.getSize())
    							{
    								index--;
    							}
    							
    							list.setSelectedIndex(index);
    							list.ensureIndexIsVisible(index);
    						}
    					}
    				}
    				
    				
    				
       			}
    				


Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is online

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

Reputation: 8017
  • View blog
  • Posts: 31,124
  • Joined: 06-March 08

Re: Changing the Color of My JPanel

Posted 05 May 2012 - 09:35 AM

Your delteListener class is defined inside a the actionPerformed() method. Class cannot be defined inside method.
Was This Post Helpful? 1
  • +
  • -

#11 SnoBunny85  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 02-March 10

Re: Changing the Color of My JPanel

Posted 05 May 2012 - 10:36 AM

alrighty! Thankyou
Was This Post Helpful? 0
  • +
  • -

#12 SnoBunny85  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 02-March 10

Re: Changing the Color of My JPanel

Posted 05 May 2012 - 11:35 AM

I need to make my inventory3 panel shorter in height.. how do I do this... I tried changing the BOXLAYOUT portion to BOXLayout.X_AXIS and BOXLayout.Y_AXIS and changing the maximum size but all it did was make it fill the entire panel...

	if(event.getSource() == InventoryDepartment)
    			{
    				//creating the new frame for the inventory department page
    				JFrame frame2 = new JFrame("Inventory Department");
    				frame2.setVisible(true);
    				frame2.setSize(600,600);
    				    				   				
    				//main panel for the inventory department   				
    				JPanel inventoryDepartment = new JPanel();
    				inventoryDepartment.setLayout(new BorderLayout());
    				inventoryDepartment.setBackground(new Color(46, 139, 87));
    				inventoryDepartment.setPreferredSize(new Dimension(600,600));
    				frame2.add(inventoryDepartment);
    				
    		
    				
    				//title of page
    				JLabel title = new JLabel("Inventory Department", SwingConstants.CENTER);
    				title.setForeground(new Color(240, 250, 240));
    				Font titleFont = new Font("Bradley Hand ITC", Font.PLAIN, 50);
					title.setFont(titleFont);
    				inventoryDepartment.add(title, BorderLayout.NORTH);
    				
    			
    			    				
    				//creating the inventory list
    				DefaultListModel listModel;
    				listModel = new DefaultListModel();
    				listModel.addElement("item 1");
    				listModel.addElement("item 2");
    				listModel.addElement("item 3");
    				listModel.addElement("item 4");
    				listModel.addElement("item 5");
    				listModel.addElement("item 6");
    				
    				JList list;
    				list = new JList(listModel);
    				list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    				list.setSelectedIndex(0);
    				list.setVisibleRowCount(5);
    				JScrollPane listScrollPane = new JScrollPane(list);
    				
    				String addString = "Add Item";
    				JButton addButton = new JButton(addString);
    			       			    				
    				String deleteString = "Delete Item";
    				JButton deleteButton = new JButton(deleteString);
    			    				
    				JTextField itemName = new JTextField(10);
    				String name = listModel.getElementAt(list.getSelectedIndex()).toString();
    				
    				
    				JPanel inventory2 = new JPanel();
    				inventory2.setMaximumSize(new Dimension(100,100));
    				inventory2.setBackground(new Color(240, 250, 240));
    				//inventory2.setForeground(new Color())
    				
    				JPanel inventory3 = new JPanel();
    				inventory3.setBackground(new Color(46,139,87));
    				inventory3.setMaximumSize(new Dimension(100,100));
    				
    				inventory2.setLayout(new BoxLayout(inventory2, BoxLayout.LINE_AXIS));
    				inventory3.setLayout(new BoxLayout(inventory3, BoxLayout.X_AXIS));
    				inventory2.add(deleteButton);
    				inventory2.add(Box.createHorizontalStrut(5));
    				inventory2.add(new JSeparator(SwingConstants.VERTICAL));
    				inventory2.add(Box.createHorizontalStrut(5));
    				inventory2.add(itemName);
    				inventory2.add(addButton);
    				inventory2.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
    				inventory3.add(listScrollPane);
    				inventoryDepartment.add(inventory2, BorderLayout.PAGE_END);
    				inventoryDepartment.add(inventory3);
    				
    				
    			}


Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is online

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

Reputation: 8017
  • View blog
  • Posts: 31,124
  • Joined: 06-March 08

Re: Changing the Color of My JPanel

Posted 05 May 2012 - 12:00 PM

The component in the CENTER region of a BorderLayout will take all the available space
The only way to minimize its size is to give larger size to the component in the EAST, WEST, NORTH and SOUTH region.

You can cheat a bit by doing something like:

JPanel center = new JPanel(new BorderLayout());
center.add(new JLabel("\n"), BorderLayout.NORTH);
center.add(new JLabel("\n"), BorderLayout.SOUTH);
center.add(new JLabel("            ", BorderLayout.WEST);
center.add(new JLabel("            ", BorderLayout.EAST);
center.add(inventory3, BorderLayout.CENTER);
inventoryDepartment.add(center);


Or if you want to reduce it's height
JPanel center = new JPanel(new BorderLayout());
JPanel north = new JPanel(new GridLayout(3,1));
north.add(new JLabel(" "));
north.add(new JLabel(" "));
north.add(new JLabel(" "));
center.add(north, BorderLayout.NORTH);

JPanel south = new JPanel(new GridLayout(3,1));
south.add(new JLabel(" "));
south.add(new JLabel(" "));
south.add(new JLabel(" "));
center.add(south, BorderLayout.NORTH);

center.add(new JLabel("            ", BorderLayout.WEST);
center.add(new JLabel("            ", BorderLayout.EAST);
center.add(inventory3, BorderLayout.CENTER);
inventoryDepartment.add(center);



There is always a way :)
Was This Post Helpful? 1
  • +
  • -

#14 SnoBunny85  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 02-March 10

Re: Changing the Color of My JPanel

Posted 06 May 2012 - 04:08 PM

can you do this???
	if(event.getSource() == InventoryDepartment)
    			{
    				
    				JFrame frame2 = new JFrame ("Inventory Department");
        			frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        			frame2.getContentPane().add(new InventoryDepartment());
					frame2.pack();
					frame2.setVisible(true);
    				
    			}


Basically accessing an entirely different class from within this action? It says it can't find Inventory Department..

but I have my main page set up like this
import javax.swing.*;


public class WarehouseInventorySystem {
        
   
       public static void main(String[] args) {
        //Displaying the main page GUI
        JFrame frame = new JFrame ("Main Page");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new MainFramePanel());
		frame.pack();
		frame.setVisible(true);
    }
    
    
}




and it accesses the MainFramePanel.java that I have in the same folder... just don't get why it wouldn't work this way??
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is online

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

Reputation: 8017
  • View blog
  • Posts: 31,124
  • Joined: 06-March 08

Re: Changing the Color of My JPanel

Posted 06 May 2012 - 04:38 PM

Should work. Post your InventoryDepartment.java file
and

if(event.getSource() == InventoryDepartment)
   			{
   				
   				JFrame frame2 = new JFrame ("Inventory Department");
       			frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       			frame2.getContentPane().add(new InventoryDepartment());


variable name should start with a lowercase letter
your JButton name should be inventoryDepartment or it becomes confusing, not for the compiler but for us stupid human beings
Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3