Results 1 to 1 of 1
  1. #1
    tianchen is offline Member
    Join Date
    Jan 2014
    Posts
    34
    Rep Power
    0

    Default How to keep constant propotion using GridBagLayout

    Hi everyone,
    I am working with GridBagLayout. Now I have encountered a problem as follows.

    I have a JPanel A.
    On this JPanel A , I put two other JPanels,say JPanel B and JPanel C. One take the left side of A, one take the right side of A.

    The propotion of width between B and C changes depending on what components in in B and C(precisely speaking, depends on the size of the components in B and C).

    My task is to keep the propotion of width between B and C constant, say 2:1.


    demo code illustrates the problem:

    Java Code:
                    JFrame frame=new JFrame();
    		frame.setSize(500,500);
    		JPanel A=new JPanel(new GridBagLayout());
    		frame.setContentPane(A);
    		
    		JPanel B=new JPanel(new GridBagLayout());
    		B.setBackground(Color.gray);
    
    		GridBagConstraints c=new GridBagConstraints();
    		c.gridx=0;
    		c.gridy=0;
    		c.weightx=0.6;
    		c.weighty=1;
    		c.fill=GridBagConstraints.BOTH;
    		A.add(B,c);
    		
    		
    		JPanel C=new JPanel(new GridBagLayout());
    		C.setBackground(Color.blue);
    		JButton bt=new JButton();
    		//bt.setPreferredSize(new Dimension(200, 40));
    		A.add(bt);
    		c.gridx=1;
    		c.gridy=0;
    		c.weightx=0.4;
    		c.fill=GridBagConstraints.BOTH;
    		A.add(C,c);
    		
    		frame.setVisible(true);
    If the size of the button is set huge(delete the comment mark),the propotion of width between B and C changes accordingly. How can I conque this :) Thank you very much
    Last edited by tianchen; 02-01-2014 at 11:34 PM.

Similar Threads

  1. constant in object ??
    By dojob in forum New To Java
    Replies: 6
    Last Post: 09-17-2013, 09:02 PM
  2. Get integer value from constant string
    By sonikku in forum New To Java
    Replies: 5
    Last Post: 05-17-2013, 09:41 AM
  3. Cannot use as static constant
    By KiranA in forum Eclipse
    Replies: 1
    Last Post: 12-01-2011, 10:19 AM
  4. Constant Variables
    By harshakantha in forum New To Java
    Replies: 3
    Last Post: 06-13-2011, 08:51 AM
  5. Constant size for JTextArea
    By itaipee in forum AWT / Swing
    Replies: 1
    Last Post: 12-30-2008, 05:12 PM

Posting Permissions

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