Results 1 to 17 of 17
  1. #1
    alinaqvi90 is offline Member
    Join Date
    May 2010
    Posts
    32
    Rep Power
    0

    Unhappy gridBagLayout anchor ... wanna move all components to top left

    Hi All


    I have a panel and in that panel I have 3 labels. This is obviously just test code which will become more complicated but the idea is to have a panel of a certain width and then add labels within that panel such that they are top-left/WEST aligned. The sample code is attached with this message and if you run it you will see that java renders all components center aligned which is a massive pain.

    I have tried changing the x-weights of labels and it does tend to work very unpredictably. Another thing I have done is to add an empty spacer label right at the end of the row with large width and high x-weighting which tends to push all others to the left. But it does not seem like an elegant solution. Especailly considering that I will have a large number of rows in the app I am working on.


    Any suggestions?


    ps: I cant use swing unfortunately... I am working on a legacy app.


    thanks in advance :)
    Attached Files Attached Files

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Try adding BorderLayout constraints when you add the panel to the applet. I haven't done much work with applets, but I've run into this problem with Swing and assume common problems would have common causes/fixes.

    Java Code:
         this.add(mainPanel, BorderLayout.WEST);

  3. #3
    alinaqvi90 is offline Member
    Join Date
    May 2010
    Posts
    32
    Rep Power
    0

    Default

    noop no joy :(

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

    Default

    You will want to create an SSCCE and post it here for further help. Please read the 3rd link in my signature links immediately below this line.

  5. #5
    alinaqvi90 is offline Member
    Join Date
    May 2010
    Posts
    32
    Rep Power
    0

    Default

    Hi Fubarable

    I believe the attachment fulfills the criteria for an SSCCE. Please copy and paste the text in a new class file(HelloWorldApplet.java) in an IDE and run it. It will show you the problemo.


    thanks

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

    Default

    OK, but why are you using GridBagLayout when the effect you desire suggests that you not use this. If you want labels placed upper left, I'd use BorderLayout as suggested and BoxLayout or FlowLayout for the inner JPanel.

  7. #7
    alinaqvi90 is offline Member
    Join Date
    May 2010
    Posts
    32
    Rep Power
    0

    Default

    Hi

    No I need GridBagLayout.

    As mentioned before I will have multiple rows of this. With different insets, paddings and grids for different components.....

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

    Default

    Hm, then perhaps you could post a slightly more involved sample program that more closely mimics your requirements. Also could you describe your requirements in greater detail?

  9. #9
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    What's the problem you are seeing when you add the BorderLayout constraint?

    Does it actually move the mainPanel over to the West? If so try creating/adding another panel.

    Java Code:
         //Create a Panel which moves the mainPanel to the West
         JPanel panel2 = new JPanel();
         panel2.setLayout(new BorderLayout());
         panel2.add(mainPanel, BorderLayout.WEST);
    
         //Add the new Panel to the JApplet, making sure to add it to the North
         this.add(panel2, BorderLayout.NORTH);

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    Quote Originally Posted by alinaqvi90 View Post
    No I need GridBagLayout.
    Homework, eh?

    db

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

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Homework, eh?

    db
    I'm not assuming that. I think that he thinks he needs it due to all the bells, whistles and do-dads that come the GridBagLayout. That's why I'm asking for greater detail in his actual needs.

  12. #12
    doomsword2001 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default

    hey had this problem today was really annoying.
    this solved my prob:
    Java Code:
    c.weightx = 1d;
    c.weighty = 1d;
    dont skip "d" i think it makes a difference.
    Good luck

  13. #13
    doomsword2001 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default

    by the way i think you dont need this:
    Java Code:
    c.anchor = GridBagConstraints.WEST;//LEFT
    if what i suggested before doesnt work try editing this aswell
    Java Code:
    c.fill = GridBagConstraints.NONE;
    I use GridBagConstraints.BOTH

  14. #14
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    I find it easier to nest the GridBagLayout (GBL) panels from the inside out when working with unevenly sized components like labels. GBLs change their size and shape based on contents.

    First, build a JPanel with just the labels and associated component. For example, if you want a column of labels next to a column of text fields or check boxes you could stick each label and associated object in its own JPanel with FlowLayout keeping each panel the same width to get the right spacing, Then you could place each of these panels in another panel with vertical orientation and place it as one column of a GBL or next to another such JPanel.

    Panels are the key to alignment. Identical components will fit nicely in a GBL. But if they are not the same size, you might need to place related groups in smaller panels and nest them in the GBL cells. Otherwise you will drive yourself crazy, especially when you need to rearrange something.

    Hope this helps.

  15. #15
    alinaqvi90 is offline Member
    Join Date
    May 2010
    Posts
    32
    Rep Power
    0

    Default

    This was the best solution I could come up with. A high vertical weighting empty label at bottom North anchored to keep all to the top
    and a A high horizontal weighting empty label at right WEST anchored to keep all to the left.


    The short answer to all the people who are advising to use some thing other than the GridBagLayout. Well they are useless. Countless number of times I have started work and nearly completed to find out the layout manager doesnt allow some thing basic like add different spacing btw diff comp's, diff size comp's, alignment issues and what not.



    Java Code:
    public class TopLeftAlignedPanel extends Panel
    	{
    		public TopLeftAlignedPanel()
    		{
    			this.setLayout( new GridBagLayout() );
    			
    			GridBagConstraints c = new GridBagConstraints();
    			c.gridx = 1000;
    			c.gridy = 0;
    			c.gridwidth = 1;
    			c.gridheight = 1;
    			c.fill = GridBagConstraints.NONE;
    			c.anchor = GridBagConstraints.WEST;//LEFT
    			c.weightx = 1000;
    			c.weighty = 0;
    			c.ipadx = 0;
    			c.ipady = 0;
    			//Insets(top, left, bottom, right);
    			c.insets = new Insets( 0,0,0,0 );
    
    			Label rightEmptyLabel = new Label( "" );
    			rightEmptyLabel.setBackground(( Color.pink ) );
    			setCompSize(rightEmptyLabel, 3, 3 );
    			this.add( rightEmptyLabel , c);
    				
    				
    			c.gridx = 0;
    			c.gridy = 1000;
    			c.gridwidth = 1;
    			c.gridheight = 1;
    			c.fill = GridBagConstraints.NONE;
    			c.anchor = GridBagConstraints.NORTH;//upwards
    			c.weightx = 0;
    			c.weighty = 1000;
    			c.ipadx = 0;
    			c.ipady = 0;
    			//Insets(top, left, bottom, right);
    			c.insets = new Insets( 0,0,0,0 );
    
    			Label bottomEmptyLabel = new Label( "" );
    			bottomEmptyLabel.setBackground(( Color.pink ) );
    			setCompSize(bottomEmptyLabel, 3, 3 );
    			this.add( bottomEmptyLabel , c);
    			
    		}
    
    public static void setCompSize(Component comp , int width, int height)
    	{
    		Dimension size = new Dimension( width, height );
    		
    		comp.setSize(size);
    		comp.setPreferredSize(size);
    		comp.setMinimumSize(size);
    	}
    	}
    Last edited by alinaqvi90; 11-15-2010 at 04:50 PM. Reason: Forgot to add method.

  16. #16
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    This was the best solution I could come up with. A high vertical weighting empty label at bottom North anchored to keep all to the top
    and a A high horizontal weighting empty label at right WEST anchored to keep all to the left.
    Maybe I still haven't understood your requirement, but as per that description I would simply put all those components in a JPanel using GBL and add that panel to another JPanel with a FlowLayout(FlowLayout.LEFT, 0, 0). Then add the outer panel to the GUI.

    db

  17. #17
    alinaqvi90 is offline Member
    Join Date
    May 2010
    Posts
    32
    Rep Power
    0

    Default [solved]

    Quote Originally Posted by Darryl.Burke View Post
    Maybe I still haven't understood your requirement, but as per that description I would simply put all those components in a JPanel using GBL and add that panel to another JPanel with a FlowLayout(FlowLayout.LEFT, 0, 0). Then add the outer panel to the GUI.

    db
    Wouldnt resolve the align-top issue though within the main component panel.


    Thanks a lot for all the input peeps :)

Similar Threads

  1. Adding and removing components from a GridBagLayout
    By peterhabe in forum New To Java
    Replies: 4
    Last Post: 09-19-2010, 10:13 PM
  2. anchor tag in html
    By meghana in forum Eclipse
    Replies: 3
    Last Post: 08-18-2010, 03:21 PM
  3. Arrange components in a GridBagLayout
    By ze snow in forum New To Java
    Replies: 1
    Last Post: 02-27-2010, 02:22 PM
  4. just wanna ask about images...
    By ashton in forum New To Java
    Replies: 11
    Last Post: 01-21-2009, 12:16 PM
  5. gridbaglayout: increase/decrease size of components.
    By newtojava7 in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 07:22 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
  •