Results 1 to 2 of 2
  1. #1
    spect4cle is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Cant get text to align in JLabel

    Ok, so this is what I have got so far:

    Cant get text to align in JLabel-screen_shot_2012_02_12_at_18_19_52.png

    I want the "Output" label to appear to be aligned in the top left. How do I achieve this?

    My code snipper is:

    Java Code:
    		pane.setLayout(new BorderLayout());
    		// store a gridlayout and place in the center of the borderlayout
    		JPanel output_area = new JPanel(new FlowLayout(FlowLayout.LEFT));
    		JPanel input_area = new JPanel(new FlowLayout(FlowLayout.LEFT));
    		JPanel keyboard = new JPanel(new GridBagLayout());
    		pane.add(output_area, BorderLayout.NORTH);
    		pane.add(input_area, BorderLayout.CENTER);
    		pane.add(keyboard, BorderLayout.SOUTH);
    		
    		output = new JLabel("Output:");
    		output_area.add(output);
    		
    		area = new JTextArea(5,20);
    		area.setOpaque(false);
    		area.setEditable(false);
    		output_area.add(area);
    		
    		input = new JLabel("Input:");
    		input_area.add(input);
    		
    		inputArea = new JTextField(10);
    		Color color = output.getBackground();
    		inputArea.setEditable(false);
    		inputArea.setBackground(color);
    		inputArea.setBorder(null);
    		input_area.add(inputArea);
    Any help appreciated, regards

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: Cant get text to align in JLabel

    It looks like your panel has two components, a label and a text area. A FlowLayout displays the components in the center of the vertical space required by the components.

    Try using a horizontal BoxLayout. Then you can control the vertical alignment of the components added to the panel. See: Fixing Alignment Problems.
    Last edited by camickr; 02-12-2012 at 09:24 PM.

Similar Threads

  1. Replies: 3
    Last Post: 01-10-2012, 10:33 AM
  2. Replies: 4
    Last Post: 02-05-2011, 06:47 PM
  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
  •