Label Alignment

I'm making a couple of little Swing widgets for editing data, I've come accross this little alignment problem and it's doing my head in. When the component is disabled, it shows a label, but this label is not aligned the way I want it, and nothing I do (setVerticalAlignment/setVerticalTextPosition) seems to affect it!

I've mocked up a little demo that reproduces the problem without you having to trawl through all my code, the main method creates a JFrame with a custom JPanel in it, which toggles between enabled and disabled every 1500 ms.

Can anyone see what I'm doing wrong, I know this is not going to be difficult, probably a one liner, but I can't for the life of me figure it out.

Here's the demo code..

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

publicclass LabelAlignmentextends JPanel{

private Color BORDER_COLOR = Color.GRAY;

private Color BACKGROUND_COLOR = Color.WHITE;

private Font LABEL_FONT =new Font("dialog",Font.PLAIN,12);

privateboolean enabled;

private JPanel content, enabledPanel, disabledPanel;

private JLabel disabledLabel;

public LabelAlignment(){

// create main content panel

content =new JPanel();

content.setLayout(new BoxLayout(content,BoxLayout.X_AXIS));

content.setBackground(BACKGROUND_COLOR);

setBackground(Color.WHITE);

setBorder(BorderFactory.createLineBorder(BORDER_COLOR));

setLayout(new BorderLayout(0,0));

// create enabled/disabled panels, and add them to content panel

enabledPanel = buildEnabledPanel();

disabledPanel = buildDisabledPanel();

content.add(enabledPanel);

content.add(disabledPanel);

add(content,BorderLayout.NORTH);

setEnabled(true);

}

private JPanel buildEnabledPanel(){

JPanel panel =new JPanel();

panel.setBackground(Color.GREEN);

panel.setPreferredSize(new Dimension(195,30));

return panel;

}

private JPanel buildDisabledPanel(){

JPanel panel =new JPanel(new FlowLayout(FlowLayout.LEFT,1,1));

disabledLabel =new JLabel("Some Text");

disabledLabel.setBackground(BACKGROUND_COLOR);

disabledLabel.setFont(LABEL_FONT);

panel.add(Box.createHorizontalStrut(3));

panel.add(disabledLabel);

panel.setBackground(BACKGROUND_COLOR);

return panel;

}

publicvoid setEnabled(boolean b){

System.out.println("setEnabled("+b+")");

super.setEnabled(b);

disabledPanel.setPreferredSize(new Dimension(enabledPanel.getSize()));

enabledPanel.setVisible(b);

disabledPanel.setVisible(!b);

}

/**

* This is just for testing

*/

publicstaticvoid main(String [] args)throws IOException{

JFrame frame =new JFrame("TestFrame");

LabelAlignment la =new LabelAlignment();

JPanel p1 =new JPanel();

p1.add(la);

frame.getContentPane().add(p1);

frame.addWindowListener(new WindowAdapter(){

publicvoid windowClosing(WindowEvent e){

System.exit(0);

}

});

frame.pack();

frame.setVisible(true);

boolean toggle =false;

while(true){

try{

Thread.sleep(1500);

}

catch (InterruptedException ie){};

la.setEnabled(toggle);

toggle = (toggle ?false :true);

}

}

}

[5936 byte] By [chez_mo] at [2007-9-30 21:25:11]
# 1

how do you want the text aligned? if you want it centered, I'd use a borderlayout for the panel, add the label to the center position, and then set the horizontal alignment to center for the jlabel: private JPanel buildDisabledPanel(){

JPanel panel = new JPanel(new BorderLayout());

disabledLabel = new JLabel("Some Text");

disabledLabel.setHorizontalAlignment(JLabel.CENTER);

disabledLabel.setBackground(BACKGROUND_COLOR);

disabledLabel.setFont(LABEL_FONT);

panel.add(disabledLabel, BorderLayout.CENTER);

panel.setBackground(BACKGROUND_COLOR);

return panel;

}

mauryballstein at 2007-7-7 2:57:09 > top of Java-index,Desktop,Core GUI APIs...
# 2
what alignment do you want?
Sebastiaan_Kortleven at 2007-7-7 2:57:09 > top of Java-index,Desktop,Core GUI APIs...
# 3
I wanted it left aligned, and centered vertically.
chez_mo at 2007-7-7 2:57:09 > top of Java-index,Desktop,Core GUI APIs...
# 4
I can use a BorderLayout and then add it to the West but then the edge of the text in the label is too close to the edge of the component. I'd like a 3-5 pixel gap to make it look nice.
chez_mo at 2007-7-7 2:57:09 > top of Java-index,Desktop,Core GUI APIs...
# 5

this is wat you need:

private JPanel buildDisabledPanel(){

JPanel panel = new JPanel(new GridLayout(1,1));

disabledLabel = new JLabel("Some Text");

disabledLabel.setBackground(BACKGROUND_COLOR);

disabledLabel.setFont(LABEL_FONT);

disabledLabel.setOpaque(true);

panel.add(disabledLabel);

disabledLabel.setHorizontalAlignment(JLabel.LEFT);

disabledLabel.setVerticalAlignment(JLabel.CENTER);

disabledLabel.setBorder(new LineBorder(Color.BLACK));

return panel;

}

you can add the empty space by adding an extra EmptyBorder to the label..

Sebastiaan_Kortleven at 2007-7-7 2:57:09 > top of Java-index,Desktop,Core GUI APIs...
# 6
that'll do the job... ta.I've also come up with another way in the meantime... if you change the hGap and vGap for the BorderLayout you can shuffle the text to the position desired.
chez_mo at 2007-7-7 2:57:09 > top of Java-index,Desktop,Core GUI APIs...
# 7

Why bother with the disabled panel at all? Just add the label directly and set it's alignment methods.

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

public class LabelAlignment extends JPanel

{

private Color BORDER_COLOR = Color.GRAY;

private Color BACKGROUND_COLOR = Color.WHITE;

private Font LABEL_FONT = new Font("dialog",Font.PLAIN,12);

private boolean enabled;

private JPanel content, enabledPanel;

private JLabel disabledLabel;

public LabelAlignment()

{// create main content panel

content = new JPanel();

content.setLayout(new BoxLayout(content,BoxLayout.X_AXIS));

content.setBackground(BACKGROUND_COLOR);

setBackground(Color.WHITE);

setBorder(BorderFactory.createLineBorder(BORDER_COLOR));

setLayout(new BorderLayout(0,0));

// create enabled/disabled panels, and add them to content panel

enabledPanel = buildEnabledPanel();

buildDisabledLabel();

content.add(enabledPanel);

content.add(disabledLabel);

add(content,BorderLayout.NORTH);

setEnabled(true);

}

private JPanel buildEnabledPanel()

{

JPanel panel = new JPanel();

panel.setBackground(Color.GREEN);

panel.setPreferredSize(new Dimension(195,30));

return panel;

}

private JLabel buildDisabledLabel()

{

//JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT,1,1));

disabledLabel = new JLabel("Some Text");

disabledLabel.setVerticalTextPosition( SwingConstants.CENTER );

disabledLabel.setHorizontalTextPosition( SwingConstants.LEFT );

disabledLabel.setBackground(BACKGROUND_COLOR);

disabledLabel.setFont(LABEL_FONT);

return disabledLabel;

}

public void setEnabled(boolean b)

{

System.out.println("setEnabled("+b+")");

super.setEnabled(b);

disabledLabel.setPreferredSize(new Dimension(enabledPanel.getSize()));

enabledPanel.setVisible(b);

disabledLabel.setVisible(!b);

}

/** * This is just for testing */

public static void main(String [] args) throws IOException

{

JFrame frame = new JFrame("TestFrame");

LabelAlignment la = new LabelAlignment();

JPanel p1 = new JPanel();

p1.add(la);

frame.getContentPane().add(p1);

frame.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

frame.pack();

frame.setVisible(true);

boolean toggle = false;

while( true )

{

try

{

Thread.sleep(1500);

}

catch( InterruptedException ie )

{

};

la.setEnabled(toggle);

toggle = (toggle ? false : true);

}

}

}

ToddCorley at 2007-7-7 2:57:09 > top of Java-index,Desktop,Core GUI APIs...
# 8

I'd use boxes for more control...

private JPanel buildDisabledPanel(){

JPanel panel = new JPanel(new BorderLayout(0,0));

disabledLabel = new JLabel("Some Text");

disabledLabel.setBackground(BACKGROUND_COLOR);

disabledLabel.setFont(LABEL_FONT);

Box hBox = Box.createHorizontalBox();

//change the value here to adjust the indentation

hBox.add(Box.createHorizontalStrut(5));

hBox.add(disabledLabel);

panel.add(hBox, BorderLayout.CENTER);

panel.setBackground(BACKGROUND_COLOR);

return panel;

}

mauryballstein at 2007-7-7 2:57:09 > top of Java-index,Desktop,Core GUI APIs...
# 9
The example I provided was HUGELY simplified, the disabled panel is an intricate beast in my actual implementation, the JLabel is just representative... but thanks for the input :D
chez_mo at 2007-7-7 2:57:09 > top of Java-index,Desktop,Core GUI APIs...