Results 1 to 2 of 2

Thread: panel resizing

  1. #1
    simo_mon is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default panel resizing

    hi i am writing a supplier information gui,
    the jpanels that i put the text box's and labels on don't resize when i maximise the jframe or adjust the jframes width or height ??

    i am trying to manipulate the behaviour with gridbagconstraints.fill ??
    Java Code:
    
    public class EnterSupplierGui extends JFrame implements ActionListener,KeyListener,PropertyChangeListener{
    
    	
    	private ActionListener parentListener;
    	private KeyListener parentKeyListener;
    	private PropertyChangeListener parentPropertyChangeListener;
    	private String frameTitle = "Note Gui";
    
        
    	private Border etchedBorder = BorderFactory.createEtchedBorder();
    	private Font textFieldFont = new Font("Serif", Font.PLAIN, 22);
    	private Font buttonFont = new Font("Serif", Font.PLAIN, 12);
    	private Font heading = new Font("Serif", Font.PLAIN, 15);
    	private Utility u = new Utility();
    	private JTextArea ta;
        
        
        private ArrayList<JComponent> JComponentList = new ArrayList<JComponent>();
    	
    	
    	public EnterSupplierGui(String noteName,ActionListener actionListener,KeyListener keyListener,PropertyChangeListener propertyChangedListener,int x_coord, int y_coord)
    	{
    		
    		   this.parentPropertyChangeListener = propertyChangedListener;
    		   this.parentListener = actionListener;
    		   this.parentKeyListener = keyListener;
    		 //set the title to the frame 
    		   this.frameTitle = noteName;
    		   this.setTitle(noteName);
    		   this.setLocation(x_coord, y_coord);
    		   // set the close operation		   
    		   this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    		   // get the content pane 
    		   Container contentPane = getContentPane();
               this.addPropertyChangeListener(this.parentPropertyChangeListener);
    		   
    		   Dimension fixed = new Dimension(700,500);
    		  // this.setPreferredSize(fixed);
    		   
    		   // set the background colour
    		   contentPane.setBackground(Color.white);
    		   
    		   GridBagLayout gbLayout = new GridBagLayout();
    		   contentPane.setLayout(gbLayout);
    		   
    		   // set up the gridbag constraints 
    		   GridBagConstraints gBC = new GridBagConstraints();
    			
    		   // set up the coordinates 
    		   setGridBagConstraints(0,0,gBC);
    		   gBC.fill = GridBagConstraints.BOTH;
    		
               contentPane.add(this.getTextFieldPane("supplierNamePane", "supplier_name_textField", "", "supplierName", "       Supplier Name"),gBC);
    		   
    		   // set up the coordinates 
    		   setGridBagConstraints(0,1,gBC);
               
    		   contentPane.add(this.getTextFieldPane("contactNamePane", "contact_name_textField", "", "contactName", "       Contact Name"),gBC);
               
    		   
    		// set up the coordinates 
    		   setGridBagConstraints(0,2,gBC);
               
    		   contentPane.add(this.getTextFieldPane("workPhonePane", "work_phone_textField", "", "workPhone", "       Work Phone"),gBC);
               
               
    		   setGridBagConstraints(0,3,gBC);
               
    		   contentPane.add(this.getTextFieldPane("mobilePhonePane", "mobile_phone_textField", "", "mobilePhone", "       Mobile Phone"),gBC);
    		   
    		   setGridBagConstraints(0,4,gBC);
               
    		   contentPane.add(this.getTextFieldPane("email", "email_textField", "", "email", "       Email"),gBC);
    		   
    		   setGridBagConstraints(0,5,gBC);
    		   //gBC.weighty = 0.8;
    		   gBC.fill = GridBagConstraints.BOTH;
               
    		   contentPane.add(this.getTextAreaPane("text_area_pane", "text_area"),gBC);
    	
    		   
    		   setGridBagConstraints(0,6,gBC);
    		  // gBC.weighty = 0.1;
    		   gBC.fill = GridBagConstraints.HORIZONTAL;
    		   
    		   
    		   String[] tempList = {"                              contact"
    				               ,"            work number"
    				               ,"          mobile number"
    				               ,"                  email"};		   
    		   
    		   contentPane.add(this.getComboBoxPaneProvideList(tempList, "add_info","add_info"),gBC);
    		   
    		   
    		   setGridBagConstraints(0,7,gBC);
    		  // gBC.weighty = 0.1;
    		   gBC.fill = GridBagConstraints.HORIZONTAL;
               
    	
               
    		   contentPane.add(this.get2ButtonPane("button_pane", "save_button", "save", "exit_button", "exit"),gBC);
    		   
    		   
    		  // u.getDateString();
    		   
               // pack everything 
    		   this.pack();
    		   // set the visibility
    		   this.setVisible(true);
    		
    		
    				
    	}
    	
    	
    	public EnterSupplierGui getGuiRef()
    	{
    		
    		return this;
    		
    	}
    	
    	public void addJComponent(JComponent in)
    	{
    		this.add(in);
    	}
    	
    	public JComponent getJComponentByName(String name)
    	{
    		JComponent result = null;
    		
    		if(this.JComponentList.size()==0)
    		{
    			return result;
    		}
    		else
    		{
    			
    			for(int i = 0;i< this.JComponentList.size();i++)
    			{
    				if(this.JComponentList.get(i).getName()!=null)
    				{
    					if(this.JComponentList.get(i).getName().equalsIgnoreCase(name)==true)
    					{
    						result= this.JComponentList.get(i);
    					}	
    					
    				}
    				else
    				{
    					//System.out.println(" no reference ");
    			
    				}
    								
    			}
    			
    		}
    			
    		return result;
    		
    	}
    	
    	
    	
    	 public void setGridBagConstraints(int x,int y,GridBagConstraints gBC)
    	    {
    	    	
    			gBC.gridx = x;
    			gBC.gridy = y;
    			gBC.ipadx =0;
    			gBC.ipady =0;
    			
    	        	
    	    }
    	
    	 public JPanel getLabelPane(String panelName,String firstLabelFieldName,String firstLabelText,String secondLabelFieldName,String secondLabelText)
    		{
    			JPanel panel = new JPanel();
    			panel.setName(panelName);
    		
    			
    			
    			
    			GridLayout gl = new GridLayout(0,2);
    			
    			panel.setLayout(gl);
    			
    			panel.add(this.getJTextLabel(firstLabelFieldName, firstLabelText));
    			panel.add(this.getJTextLabel(secondLabelFieldName, secondLabelText));
    			panel.setBorder(this.etchedBorder);
    					
    			this.JComponentList.add(panel);
    			
    			return panel;
    			
    		}
    	 
    		public JScrollPane getTextAreaPane(String panelName,String textAreaName)
    		{
    //			JPanel panel = new JPanel();
    			
    			 ta = new JTextArea("", 5, 50);
                 ta.setName(textAreaName);
    		     ta.setLineWrap(true);
    		     ta.addKeyListener(this.parentKeyListener);
    		     this.JComponentList.add(ta);
    				
    				
    			 JScrollPane  scrollPane = new JScrollPane(ta);
    			 scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    
    
    			
    			
    			scrollPane.setName(panelName);
    			//scrollPane.setBackground(Color.gray);
    	
    			this.JComponentList.add(scrollPane);
    			
    			return scrollPane;
    			
    		}
    	 
    	 
    	 
    	 
    	 
    	public JPanel getTextFieldPane(String panelName,String textFieldName,String textFieldText,String labelName,String labelText)
    	{
    		JPanel panel = new JPanel();
    		panel.setName(panelName);
    		GridLayout gl = new GridLayout(0,2);
    		panel.setLayout(gl);
    		panel.add(this.getJTextLabel(labelName, labelText));
    		panel.add(this.getJTextField(textFieldName, textFieldText));
    		panel.setBorder(this.etchedBorder);
    				
    		this.JComponentList.add(panel);
    		
    		return panel;
    		
    	}
    	
    	public JPanel get2ButtonPane(String panelName,String firstButtonName,String firstButtonText,String secondButtonName,String secondButtonText)
    	{
    		JPanel panel = new JPanel();
    		panel.setName(panelName);
    		GridLayout gl = new GridLayout(0,2);
    		panel.setLayout(gl);
    		panel.setBackground(Color.gray);
    		panel.add(this.getJButton(firstButtonName, firstButtonText));
    		panel.add(this.getJButton(secondButtonName, secondButtonText));
    		panel.setBorder(this.etchedBorder);
    				
    		this.JComponentList.add(panel);
    		
    		return panel;
    		
    	}
    	
    	
    	public JPanel get3ButtonPane(String panelName,String firstButtonName,String firstButtonText,String secondButtonName,String secondButtonText,String thirdButtonName,String thirdButtonText)
    	{
    		JPanel panel = new JPanel();
    		panel.setName(panelName);
    		GridLayout gl = new GridLayout(0,2);
    		panel.setLayout(gl);
    		panel.setBackground(Color.gray);
    		panel.add(this.getJButton(firstButtonName, firstButtonText));
    		panel.add(this.getJButton(secondButtonName, secondButtonText));
    		panel.add(this.getJButton(thirdButtonName, thirdButtonText));
    		panel.setBorder(this.etchedBorder);
    				
    		this.JComponentList.add(panel);
    		
    		return panel;
    		
    	}
    	 
    	public JTextArea getJTextArea(String name)
    	{
    		JTextArea textArea = new JTextArea("here is some");
    		// set the name of the component
    		textArea.setName(name);
    		textArea.setPreferredSize(new Dimension(400,400));
    		textArea.setFont(this.textFieldFont);
    		textArea.setEditable(true);
    		textArea.setBackground(Color.red);
    		textArea.addKeyListener(this);
    		// add the textfield to the component list 
    		
    		this.JComponentList.add(textArea);
    			
    		return textArea;
    				
    	}
    	
    	
    	
    	 
    	 
    	public JTextField getJTextField(String name,String text)
    	{
    		JTextField textField = new JTextField(text);
    		// set the name of the component
    		textField.setName(name);
    		textField.setActionCommand(name);
    		textField.setFont(this.textFieldFont);
    		textField.addKeyListener(this);
    		// add the textfield to the component list 
    		this.JComponentList.add(textField);
    		textField.addActionListener(this);
    		textField.addActionListener(this.parentListener);
    			
    		return textField;
    				
    	}
    	
    	public JLabel getJTextLabel(String name,String text)
    	{
    		JLabel jLabel = new JLabel(text);
    		// set the name of the component
    		jLabel.setName(name);
    		jLabel.setFont(this.textFieldFont);
    		jLabel.setAlignmentX(0);
    		
    		// add the textfield to the component list 
    		this.JComponentList.add(jLabel);
    		
    			
    		return jLabel;
    				
    	}
    	
        public JButton getJButton(String ActionCommandName,String buttonTitle)
        {
        	
        	JButton b3 = new JButton(buttonTitle);
     	    //Use the default text position of CENTER, TRAILING (RIGHT).
     	   
     	    b3.setActionCommand(ActionCommandName);
     	    //b3.setEnabled(false);
    
     	    //Listen for actions on buttons 1 and 3.
            
     	    this.JComponentList.add(b3);
     	    // controller action list 
     	    b3.addActionListener(this.parentListener);
     	    b3.addActionListener(this);
    
     	    b3.setFont(this.textFieldFont);
        	
        	return b3;
        	
        }
        
        private JComponent getComboBoxPaneProvideList(String[] list,String title,String ActionCommandName)
    	{
    	// create a panel
    		JPanel panel = new JPanel();
    		
    		Dimension PaneSize = new Dimension(400,50);
    		
    		// set the preferred size
    		panel.setPreferredSize(PaneSize);
    		
    		//create a layout 
    		GridBagLayout gbLayout = new GridBagLayout();
    		
    		// set background colout and border
    		
    		//Color test = new Color(204,204,255);
    		//panel.setBackground(test);
    		panel.setLayout(gbLayout);
    		panel.setBorder(etchedBorder);
    	    
    	    
    	    
    	    // set up the gridbag constraints 
    	    GridBagConstraints gBC = new GridBagConstraints();
    	    
    	    
    	    
    	   JLabel nameLabel = this.getJTextLabel("add_information", "add supplier information     ");
    	   
    	   setGridBagConstraints(0,0,gBC);
    	   
    	   panel.add(nameLabel,gBC);
    	   
    	   setGridBagConstraints(1,0,gBC);
    	   gBC.insets = new Insets(5,5,5,5);
    	   //gBC.fill = GridBagConstraints.HORIZONTAL;
    	   	   
    	   panel.add(this.getJComboBoxProvideStringArray(list, "title", ActionCommandName),gBC);
    	   
    	   
    	 
    	    return panel;
    	}
     
     
    	
        
        public JComboBox getJComboBoxProvideStringArray(String[] input,String buttonTitle,String ActionCommandName)
        {
        	
        	int length = input.length -1 ;
        	//System.out.println(" the string array length is " + length );
        	for(int i=0;i<length;i++)
        	{
        
        		
        	}
        	//System.out.println(input);
        	
    	
    	    JComboBox cb = new JComboBox(input);
    	    cb.setSelectedIndex(length);
    	    cb.setPreferredSize(new Dimension(200,100));
    	    cb.setSelectedIndex(0);
    	    ((BasicComboBoxRenderer)cb.getRenderer()).setHorizontalAlignment(JLabel.RIGHT);
    	    cb.setActionCommand(ActionCommandName);
    	    cb.setFont(this.textFieldFont);
    	    
        		
     	    // controller action list 
     	    cb.addActionListener(this.parentListener);
     	    cb.addActionListener(this);
        	
        	return cb;
        	
        }
    	
    	
    	
    	
    	@Override
    	public void actionPerformed(ActionEvent arg0) {
    		// TODO Auto-generated method stub
    		
    		//this.firePropertyChange("stringTest", "oldValue", "newValue");
    		
    		//this.getJComponentByName("Title");
    		
    		if(arg0.getActionCommand().equalsIgnoreCase("exit_button"))
    		{
    			System.out.println(" the exit butotn has been pushed ");
    			
    			this.dispose();
    			
    		}
    		
    		
    		if(this.getJComponentByName("textField")!=null)
    		{
    			JTextField temp = (JTextField) this.getJComponentByName("textField");
    			//System.out.println(" the text is " + temp.getText());
    			this.firePropertyChange("noteName", "", temp.getText());
    			
    			
    		}
    		else
    		{
    			
    			//System.out.println(" title name null ");
    			
    		}
    		
    		
    		if(this.getJComponentByName("textField")!=null)
    		{
    			JTextField temp = (JTextField) this.getJComponentByName("textField");
    			//System.out.println(" the text is " + temp.getText());
    			this.firePropertyChange("noteName", "", temp.getText());
    			
    			
    		}
    		else
    		{
    			
    			//System.out.println(" title name null ");
    			
    		}
    
    		
    		
    		//this.firePropertyChange("stringTest", "oldValue", "newValue");
    		//this.firePropertyChange("stringTest", "oldValue", "newValue");
    		
    	}
    
    
    	@Override
    	public void keyPressed(KeyEvent e) {
    		// TODO Auto-generated method stub
    		
    		  
    		
    		
    	}
    
    
    	@Override
    	public void keyReleased(KeyEvent arg0) {
    		// TODO Auto-generated method stub
    		
    		
    	}
    
    
    	@Override
    	public void keyTyped(KeyEvent e) {
    		// TODO Auto-generated method stub
    		
    		  int key = e.getKeyCode();
    		  
    		 
    		  
    		  
         if (key == KeyEvent.VK_ACCEPT)
         {
            Toolkit.getDefaultToolkit().beep(); 
            //System.out.println("\n\n\n\n  ENTER pressed");
            }
    
    
    		
    	}
    
    
    	@Override
    	public void propertyChange(PropertyChangeEvent evt) {
    		// TODO Auto-generated method stub
    		
    		////System.out.println(" property changed ");
    		
    	}
    
    
    	public ArrayList<JComponent> getJComponentList() {
    		return JComponentList;
    	}
    
    
    	public void setJComponentList(ArrayList<JComponent> componentList) {
    		JComponentList = componentList;
    	}
    	
    	
    
    }

    any suggestions would be appreciated :)
    still not 100% convinced i know what i am doing with the gridbagcontraints...

    thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You may do well to set weightx and weighty properties of your GridBagConstraints object:
    Java Code:
        setGridBagConstraints(0, 0, gBC);
        gBC.fill = GridBagConstraints.BOTH;
        gBC.weightx = 1.0;
        gBC.weighty = 1.0;
    Or you could make weighty 0.0 for everything but the jscrollpane if you want that component the only one that stretches up and down.
    Last edited by Fubarable; 08-15-2009 at 02:15 PM.

Similar Threads

  1. Window Resizing issue
    By er91ic512 in forum AWT / Swing
    Replies: 2
    Last Post: 07-28-2009, 12:48 PM
  2. JFrame resizing
    By carderne in forum New To Java
    Replies: 3
    Last Post: 07-22-2009, 07:42 PM
  3. panel resizing
    By kumar_gemi in forum New To Java
    Replies: 3
    Last Post: 03-13-2009, 02:51 AM
  4. Image Resizing and DPi
    By Rob_ in forum Java Applets
    Replies: 4
    Last Post: 11-27-2008, 02:21 PM
  5. Image resizing
    By alley in forum Java 2D
    Replies: 2
    Last Post: 11-13-2007, 10:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •