Results 1 to 2 of 2
  1. #1
    OllyHal is offline Member
    Join Date
    Feb 2012
    Posts
    25
    Rep Power
    0

    Default 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.
    Java 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);
    	  	
    	}
    Thanks in advance :D

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: JLabel Align Text

    I was wondering how to get the text in the JLabel to align to the left.
    I was under the impression that unless you did something to change it the text *does* 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

  1. Cant get text to align in JLabel
    By spect4cle in forum AWT / Swing
    Replies: 1
    Last Post: 02-12-2012, 09:19 PM
  2. Replies: 3
    Last Post: 01-10-2012, 10:33 AM
  3. JLabel won't left-align
    By gib65 in forum AWT / Swing
    Replies: 6
    Last Post: 07-30-2010, 04:07 PM
  4. Java: How do i align the text in the center?
    By nicacutenica in forum New To Java
    Replies: 5
    Last Post: 06-29-2010, 06:50 PM
  5. JTextArea - text align
    By bradder in forum AWT / Swing
    Replies: 1
    Last Post: 11-29-2007, 07: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
  •