Results 1 to 2 of 2
Thread: JLabel Align Text
- 02-18-2012, 01:34 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 25
- Rep Power
- 0
JLabel Align Text
Hi,
I have some code here and I was wondering how to get the text in the JLabel to align to the left.Thanks in advance :DJava Code:JLabel UsernameLbl = new JLabel("Username"); JLabel PasswordLbl = new JLabel("Password"); JLabel CPasswordLbl = new JLabel("Confirm Password"); JLabel NLbl = new JLabel(""); JTextField UsernameTxt = new JTextField(10); JPasswordField PasswordFld = new JPasswordField(10); JPasswordField CPFld = new JPasswordField(10); JButton okbtn = new JButton("OK"); JButton CancelBtn = new JButton("Cancel"); JFrame RFrame = new JFrame(); JPanel RPanel = new JPanel(new GridBagLayout()); JPanel CPanel = new JPanel(new GridBagLayout()); public void CreateRegisterGUI(){ //Grid Layout GridBagConstraints c = new GridBagConstraints(); GridBagConstraints b = new GridBagConstraints(); //center panel c.insets = new Insets(10,10,10,10); c.gridx = 0; c.gridy = 1; UsernameLbl.setHorizontalAlignment(SwingConstants.LEFT); RPanel.add(UsernameLbl,c); c.gridx = 0; c.gridy = 2; PasswordLbl.setAlignmentX(SwingConstants.LEFT); RPanel.add(PasswordLbl,c); c.gridx = 0; c.gridy = 3; CPasswordLbl.setAlignmentX(SwingConstants.LEFT); RPanel.add(CPasswordLbl,c); c.gridx = 1; c.gridy = 1; RPanel.add(UsernameTxt,c); c.gridx = 1; c.gridy = 2 ; RPanel.add(PasswordFld,c); c.gridx = 1; c.gridy = 3 ; RPanel.add(CPFld,c); c.gridx = 0; c.gridy = 4; RPanel.add(okbtn,c); c.gridx = 1; c.gridy = 4; RPanel.add(CancelBtn,c); //bottom panel CPanel.add(NLbl, b); RFrame.add(RPanel,BorderLayout.CENTER); RFrame.add(CPanel,BorderLayout.AFTER_LAST_LINE); RFrame.pack(); RFrame.setLocationRelativeTo(null); RFrame.setSize(450,210); RFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); RFrame.setVisible(true); RFrame.setResizable(false); }
- 02-18-2012, 02:03 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: JLabel Align Text
I was under the impression that unless you did something to change it the text *does* align to the left.I was wondering how to get the text in the JLabel to align to the left.
... after checking the API docs ... "The label is aligned against the leading edge of its display area, and centered vertically" which I guess, in my left-to-right part of the world makes it left aligned.
Perhaps it is the label rather than its text that is not aligned as you want. I would guess you are not using the GridBagConstraints correctly (because that's easy to do :) But that's only a guess. It would help if you posted a Short, Self Contained, Correct Example and described the intended behaviour. (It would make the code easier to follow if your example used standard Java coding conventions and had variables start with a lower case letter.)
Similar Threads
-
Cant get text to align in JLabel
By spect4cle in forum AWT / SwingReplies: 1Last Post: 02-12-2012, 09:19 PM -
How to horizontal align multi-line Text in a JTable-Cell
By Hotkey in forum AWT / SwingReplies: 3Last Post: 01-10-2012, 10:33 AM -
JLabel won't left-align
By gib65 in forum AWT / SwingReplies: 6Last Post: 07-30-2010, 04:07 PM -
Java: How do i align the text in the center?
By nicacutenica in forum New To JavaReplies: 5Last Post: 06-29-2010, 06:50 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