Results 1 to 7 of 7
  1. #1
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

    Default JLabel won't left-align

    Hello,

    I've got a JPanel with a GridBagLayout as its layout manager. I'm trying to add a JLabel to it and I want it pushed all the way to the left side. Here's my code:

    Java Code:
    		JPanel SouthPanel = new JPanel(new GridBagLayout());
    		SouthPanel.setPreferredSize(new Dimension(400, 50));
    		SouthPanel.setBackground(Color.BLUE);
    
    		ClockJLabel CL = new ClockJLabel();
    		
    		GridBagConstraints GBC = new GridBagConstraints();
    		GBC.gridx = GBC.gridy = 0;
    		GBC.anchor = GridBagConstraints.LINE_START;
    		SouthPanel.add(CL, GBC);
    The JLabel doesn't seem to want to left align. It's always centered.

    Can anyone see what the problem might be?

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

    Default

    Is that the only component being added to the SouthPanel? If so, why use GridbagLayout and not BorderLayout placing the label in the BorderLayout.WEST position?

    I also suggest that if this doesn't help you, you create and post an SSCCE to allow us to see for ourselves what the problem may be. You also should read up on Java naming conventions (class names begin with a capital letter and variable and method names begin lower case) since following this will make your code easier for us to understand and thus make it easier for us to help you.

    Much luck!

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    You need to set a non-zero weightx so the layout knows how to distribute extra space.

    db

  4. #4
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    You need to set a non-zero weightx so the layout knows how to distribute extra space.

    db
    Thanks Darryl,

    Adding GBC.weightx = 1 fixed the problem.

    Fubarable,

    Thanks for the suggestions. I'm using the GridBagLayout because it's the only layout manager I know of that keeps components at their preferred size (though I don't know all the layout managers inside and out).

    I'm also going to add conforming-to-Java-naming-conventions to my list of tasks. Also, if I were to post a SSCCE, would it suffice to upload my program in a zip file as an attachment to my post (of course, not the whole program as it stands, but a minimal version that is both compilable and demonstrates the problem I'm dealing with)?

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

    Default

    If you make a decent SSCCE, then it should be small enough to post the code directly in the forum using code tags.

    Glad you have fixed your problem!

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    Quote Originally Posted by gib65 View Post
    I'm also going to add conforming-to-Java-naming-conventions to my list of tasks.
    http://www.oracle.com/technetwork/ja...oc-136057.html

    db
    Last edited by DarrylBurke; 07-30-2010 at 12:24 AM. Reason: Trying to make the URL display correctly -- either the parsing is screwed up or the page has some wierd title attributes

  7. #7
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

Similar Threads

  1. Adding a JLabel to a JPanel - jlabel not showing
    By Bongeh in forum New To Java
    Replies: 17
    Last Post: 04-07-2010, 12:02 AM
  2. Align left
    By britto_bicsjohn in forum AWT / Swing
    Replies: 2
    Last Post: 09-09-2009, 05:05 AM
  3. How to align the output on console?
    By sfe23 in forum New To Java
    Replies: 5
    Last Post: 03-30-2009, 04:28 AM
  4. Right Align columns in JTable
    By Laura Warren in forum New To Java
    Replies: 2
    Last Post: 12-18-2008, 10:01 PM
  5. JTextArea - text align
    By bradder in forum AWT / Swing
    Replies: 1
    Last Post: 11-29-2007, 08:08 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
  •