'Java add a JLabel in the middle of a JButton

I would like to simply add a JLabel into a JButton and center it horizontally. I've tried many thing but the text stay left side... Here is my code :

    JLabel labeltest = new JLabel("Simple test");
    JButton myButton = new JButton();
    myButton.setHorizontalTextPosition(SwingConstants.CENTER);
    myButton.add(labeltest);

output picture



Solution 1:[1]

Create an Icon of the text and give the Icon the foreground/background colors that you want.

I tried this but the problem comes when I had to do button.setenabled(false); after this the text becomes almost invisible

You can set the disabled Icon to be the same as the Icon and it will not be painted in the disabled state:

JButton button = new JButton( new ImageIcon(...) );
button.setDisabledIcon( button.getIcon() );
button.setEnabled(false);

Of course the problem with this approach is that the user doesn't know the button is disabled. So in reality you would need 2 icons:

  1. one for the normal state
  2. one for the disabled state

Solution 2:[2]

A JButton is also a java.awt.Container. Thus you can set a layout manager. E.g. you can use a GridBagLayout.

public class Main {

  public static void main(String[] args) {
    JFrame frame = new JFrame();

    JToggleButton toggleButton = new JToggleButton();
    JLabel jLabel = new JLabel("3");

    JToggleButton.ToggleButtonModel toggleButtonModel = (JToggleButton.ToggleButtonModel) toggleButton.getModel()
    ToggleForegroundAction toggleForegroundAction = 
            new ToggleForegroundAction(toggleButtonModel, Color.WHITE, Color.RED);
    toggleForegroundAction.setComponent(jLabel);
    toggleButton.setAction(toggleForegroundAction);

    toggleButton.setLayout(new GridBagLayout());
    toggleButton.add(jLabel, new GridBagConstraints());

    JPanel panel = new JPanel();
    panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    panel.setLayout(new BorderLayout());
    panel.add(toggleButton, BorderLayout.CENTER);

    Container contentPane = frame.getContentPane();
    contentPane.add(panel);

    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.pack();
    frame.setVisible(true);
  }
}

An action that toggles the label's foreground color might look like this

  public class ToggleForegroundAction extends AbstractAction {

    private JComponent component;
    private JToggleButton.ToggleButtonModel toggleButtonModel;
    private final Color selectedColor;
    private final Color unselectedColor;

    public ToggleForegroundAction(JToggleButton.ToggleButtonModel toggleButtonModel, Color selectedColor, Color unselectedColor) {
      this.toggleButtonModel = toggleButtonModel;
      this.selectedColor = selectedColor;
      this.unselectedColor = unselectedColor;
    }

    public void setComponent(JComponent component) {
      this.component = component;
      setForeground(component, toggleButtonModel.isSelected());
    }

    @Override
    public void actionPerformed(ActionEvent e) {
      JComponent targetComponent = this.component;
      if (targetComponent == null) {
        targetComponent = (JComponent) e.getSource();
      }
      setForeground(targetComponent, toggleButtonModel.isSelected());
    }

    private void setForeground(JComponent targetComponent, boolean isSelected) {
      Color foreground;
      if (isSelected) {
        foreground = selectedColor;
      } else {
        foreground = unselectedColor;
      }
      targetComponent.setForeground(foreground);
    }
  }

Unselected => Selected

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 camickr
Solution 2 René Link