Hi,

i' having a small problem with panels. i want the panel always to have the same size even when empty. in my program that i'm writting, i can see that the panel doesnt fit all the area where i put it ( i noticed that as i have different colors for each frame and panel).
the panel starts getting bigger when i add things to it.

and another thing, how can i set the Jtable i'm adding to fit all the panel??

use the setSize() methods, but guessing as to what you've done so far is not the best way for us to help you out, so you might want to post your code as well.

the setSize() didnt work ... instead i used setPreferedSize() and it did the desired job.
but the problem now is that i have some buttons stretched in the panel .. i think maybe setting a proper Layout will fix that problem.

my code is a bit too long and got many classes.

the code of that JPanel is stretched over several classes?? somehow I doubt that

may i ask another question related to panel?

i'm using the removeAll() method to clear the panel from all buttons. but it doesnt take any effect on the screen .. how can i update it?

no i didnt mean the panel is from other classes .. but this frame i use is started from a different frame.
for the button stretching thing .. see lines 170 - 218 .. thats where i declare and add them
here is the code:

public class MainWindow extends JFrame implements TreeSelectionListener
{
	JPanel optionPanel= new JPanel();
	JPanel upper= new JPanel();
	JPanel down= new JPanel();
	
	DefaultMutableTreeNode addCashier = new DefaultMutableTreeNode("Add Employee");
	DefaultMutableTreeNode delCashier = new DefaultMutableTreeNode("Del Employee");
	DefaultMutableTreeNode viewCashier = new DefaultMutableTreeNode("View Details");
	
	DefaultMutableTreeNode addCustomer = new DefaultMutableTreeNode("Add Customer");
    DefaultMutableTreeNode delCustomer = new DefaultMutableTreeNode("Del Customer");
    DefaultMutableTreeNode viewCustomer = new DefaultMutableTreeNode("view Customer");
    
    DefaultMutableTreeNode addCustomerGroup = new DefaultMutableTreeNode("Add Group");
    DefaultMutableTreeNode delCustomerGroup = new DefaultMutableTreeNode("Del Group");
    DefaultMutableTreeNode viewCustomerGroup = new DefaultMutableTreeNode("view Group");
    
    DefaultMutableTreeNode addDepartment = new DefaultMutableTreeNode("Add Department");
    DefaultMutableTreeNode delDepartment = new DefaultMutableTreeNode("Del Department");
    DefaultMutableTreeNode viewDepartmentDetails = new DefaultMutableTreeNode("view Details");
    
    DefaultMutableTreeNode item = new DefaultMutableTreeNode("Items");
    
    DefaultMutableTreeNode invoice = new DefaultMutableTreeNode("Invoices");
    
    DefaultMutableTreeNode machine = new DefaultMutableTreeNode("Machines");
    
    DefaultMutableTreeNode supplier = new DefaultMutableTreeNode("Suppliers");
	
    
    public MainWindow()
	{
		Container c =  getContentPane();
		c.setLayout(new GridBagLayout() );
		c.setBackground(new Color (236,233,216));
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.BOTH;
		gbc.anchor=GridBagConstraints.NORTH;
		
		
		JPanel treePanel= new JPanel();
		treePanel.setBackground(new Color (236,233,216));
		optionPanel.setBackground(new Color (236,233,216));
		//optionPanel.setLayout(new GridLayout(2,1));
		optionPanel.setLayout(new BorderLayout());
		
		upper.setBackground(new Color (236,233,216));
		
		//Parent for tree list
		DefaultMutableTreeNode parent = new DefaultMutableTreeNode("Bravo", true);
		
		// Cashier tree list
		DefaultMutableTreeNode cashier = new DefaultMutableTreeNode("Cashier", true);
	    cashier.add(addCashier);
	    cashier.add(delCashier);
	    cashier.add(viewCashier);
	    parent.add(cashier);
	    
	    // Customer tree list
		DefaultMutableTreeNode customer = new DefaultMutableTreeNode("Customer", true);
	    customer.add(addCustomer);
	    customer.add(delCustomer);
	    customer.add(viewCustomer);
	    parent.add(customer);
	    
	    // Customer Group tree list
	    DefaultMutableTreeNode customerGroup = new DefaultMutableTreeNode("Customer Group", true);
	    customerGroup.add(addCustomerGroup);
	    customerGroup.add(delCustomerGroup);
	    customerGroup.add(viewCustomerGroup);
	    parent.add(customerGroup);
	    
	    // Departments
	    DefaultMutableTreeNode department = new DefaultMutableTreeNode("Departments", true);
	    department.add(addDepartment);
	    department.add(delDepartment);
	    department.add(viewDepartmentDetails);
	    parent.add(department);
	    
	    // Invoice tree list
	    parent.add(invoice);
	    
	    //Item tree list
	    parent.add(item);
	    
	    //Machine tree list
	    parent.add(machine);
	    
	    // Suppliers
	    parent.add(supplier);
	    
	    
	    JTree tree = new JTree(parent);
	    treePanel.add(tree);
		c.add(treePanel,gbc);
		gbc.fill= GridBagConstraints.BOTH;
		gbc.weightx=50.0;
		gbc.weighty=50.0;
		optionPanel.add(upper, BorderLayout.NORTH);
		optionPanel.add(down,BorderLayout.CENTER);
		gbc.fill= GridBagConstraints.BOTH;
		gbc.weightx=50.0;
		gbc.weighty=50.0;
		c.add(optionPanel,gbc);
		
		
		
		tree.addTreeSelectionListener(this);
		
	}
	
	public void valueChanged(TreeSelectionEvent e)
	{
		TreePath path = e.getNewLeadSelectionPath();
		  TreeNode treeNode = (TreeNode)path.getLastPathComponent();
		 
		  if (treeNode == addCashier)
		  {
			  //optionPanel.removeAll();
			  upper.removeAll();
			  down.removeAll();
			  upper.add(new JButton("OK"));
			  
		  }
		  if (treeNode == delCashier)
		  {
			  upper.removeAll();
			  down.removeAll();
			  upper.setLayout(new FlowLayout(10,10,0));
			  upper.revalidate();
			  down.revalidate();
			  upper.add(new JButton("Not OK"));
			  
		  }
		  if (treeNode == viewCashier)
		  {
			  //optionPanel.removeAll();
			  upper.removeAll();
			  down.removeAll();
			  upper.setLayout(new FlowLayout(10,10,0));
			  upper.validate();
			  down.validate();
			  JComboBox cashierNames= new JComboBox();
			  JButton showAllCashiers = new JButton("View All");
			  
			  Statement stmt = null;
	            ResultSet rs = null;
	            try 
	            {
					stmt = LoginFrame.conn.createStatement();				
					rs = stmt.executeQuery("select id,first_name from cashier;"); // get ids and names to fill combo box
					int numCols = rs.getMetaData().getColumnCount ();
					while ( rs.next() ) 
					{
						cashierNames.addItem(rs.getString(1)+"-"+rs.getString(2));
					}
					cashierNames.addActionListener( new ActionListener()
					 {
						   public void actionPerformed(ActionEvent e) 
						   { 
							   
							   down.removeAll();
							   JComboBox cb = (JComboBox)e.getSource();
							   
								int index= cb.getSelectedIndex(); // index where we clicked to retrieve id from database
								down.setLayout(new GridLayout(3,4,4,4));
								
								// All columns that need to be filled
								JLabel idL= new JLabel("ID");
								JTextField idF = new JTextField(" ");
								idF.setEditable(false);
								JLabel firstNameL= new JLabel("F-Name");
								JTextField firstNameF = new JTextField(" ");
								firstNameF.setEditable(false);
								JLabel lastNameL= new JLabel("L-Name");
								JTextField lastNameF = new JTextField(" ");
								lastNameF.setEditable(false);
								JLabel phoneL= new JLabel("Phone");
								JTextField phoneF = new JTextField(" ");
								phoneF.setEditable(false);
								JLabel wageL= new JLabel("Wage");
								JTextField wageF = new JTextField(" ");
								wageF.setEditable(false);
								JLabel workOnMachineL= new JLabel("Machine #");
								JTextField workOnMachineF = new JTextField(" ");
								workOnMachineF.setEditable(false);
						
								try  // filling the Jfields from Database
					            { 
									Statement stmt = null;
					            
									ResultSet rs = null;
									stmt = LoginFrame.conn.createStatement();
									rs = stmt.executeQuery("select * from cashier where id="+(index+1)+";");
									rs.next();
									
									idF.setText(rs.getString(1));
									firstNameF.setText(rs.getString(2));
									lastNameF.setText(rs.getString(3));
									phoneF.setText(rs.getString(4));
									wageF.setText(rs.getString(5));
									workOnMachineF.setText(rs.getString(6));
									
									down.revalidate();
									down.add(idL);
									down.add(idF);
									down.add(wageL);
									down.add(wageF);
									down.add(firstNameL);
									down.add(firstNameF);
									down.add(phoneL);
									down.add(phoneF);
									down.add(lastNameL);
									down.add(lastNameF);
									down.add(workOnMachineL);
									down.add(workOnMachineF);
					            } 
								catch(SQLException e1)
								{
									e1.printStackTrace();
								}
						   }
					 });
					
	            }
	            catch (SQLException e1) 
	            {
					
					e1.printStackTrace();
				}
	            upper.add(cashierNames);
	            upper.add(showAllCashiers);
	            
	            showAllCashiers.addActionListener( new ActionListener()
				 {
					   public void actionPerformed(ActionEvent e) 
					   { 
						   down.removeAll();
						   down.setLayout(new GridBagLayout());
						   GridBagConstraints gbc = new GridBagConstraints();
						   gbc.fill= GridBagConstraints.BOTH;
						   down.revalidate();
						   
						   String colNames[]; // for column titles
					       String data[][];
					       DefaultTableModel model;
					       JTable table;
					       JScrollPane pane;
					       
					       Statement stmt = null;
				           ResultSet rs = null;
					       try 
				            {
								stmt = LoginFrame.conn.createStatement();
								rs=stmt.executeQuery("select * from cashier;");
								int numCols = rs.getMetaData().getColumnCount ();
								rs = stmt.executeQuery("Describe cashier;"); 
								
								colNames= new String[numCols];
								int j=0;
								while (rs.next()) // filing the names of tables to ColNames
									colNames[j++]=new String(rs.getString(1));
																	
								
								data= new String[0][numCols];
						        model = new DefaultTableModel(data,colNames);
						        table = new JTable(model){
						                            public boolean isCellEditable(int rowIndex, int colIndex) {
						                                    return false;   //Disallow the editing of any cell
						                             }
						                            }; 
						        pane = new JScrollPane(table);
						        
						        rs=stmt.executeQuery("select * from cashier;");
								
								
								while (rs.next()) // inserting data to Jtable from database
					            {
					                String d[]= new String[numCols];
					                for (int k=0; k<numCols; k++)
					                {
					                    d[k]=rs.getString(k+1);
					                }
					              
					                model.insertRow(table.getRowCount(),d);
					                
					                
					            }
								down.setLayout(new GridLayout(1,1));
								down.add(pane,gbc);
				            }
					       
					       catch (SQLException e1) 
				            {
								
								e1.printStackTrace();
							}
					   }
				 });
		  }
			  
	}
	
	
}

use

JPanel p = new JPanel() {

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(100, 100);
            }

            public Dimension getMinimumSize() {
                return new Dimension(100, 100);
            }

            public Dimension getMaximumSize() {
                return new Dimension(100, 100);
            }
        };

Other methods of JPanel class You can overwrite here if You want.
Such thinking is more general.

may i ask another question related to panel?

i'm using the removeAll() method to clear the panel from all buttons. but it doesnt take any effect on the screen .. how can i update it?

for example, line 123:
use

upper.add(new JButton("OK"));
 upper.repaint();

although i think is a better way.

You may need a call to validate() before the repaint().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.