Results 1 to 7 of 7
Thread: JLabel won't left-align
- 07-28-2010, 09:58 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 86
- Rep Power
- 0
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:
The JLabel doesn't seem to want to left align. It's always centered.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);
Can anyone see what the problem might be?
-
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!
- 07-29-2010, 11:41 AM #3
You need to set a non-zero weightx so the layout knows how to distribute extra space.
db
- 07-29-2010, 05:32 PM #4
Member
- Join Date
- Jun 2010
- Posts
- 86
- Rep Power
- 0
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)?
-
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!
- 07-29-2010, 11:17 PM #6
Last edited by DarrylBurke; 07-29-2010 at 11:24 PM. Reason: Trying to make the URL display correctly -- either the parsing is screwed up or the page has some wierd title attributes
- 07-30-2010, 04:07 PM #7
Member
- Join Date
- Jun 2010
- Posts
- 86
- Rep Power
- 0
Similar Threads
-
Adding a JLabel to a JPanel - jlabel not showing
By Bongeh in forum New To JavaReplies: 17Last Post: 04-06-2010, 11:02 PM -
Align left
By britto_bicsjohn in forum AWT / SwingReplies: 2Last Post: 09-09-2009, 04:05 AM -
How to align the output on console?
By sfe23 in forum New To JavaReplies: 5Last Post: 03-30-2009, 03:28 AM -
Right Align columns in JTable
By Laura Warren in forum New To JavaReplies: 2Last Post: 12-18-2008, 09:01 PM -
JTextArea - text align
By bradder in forum AWT / SwingReplies: 1Last Post: 11-29-2007, 07:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks