Center align JButton's JLabel

dshrenik
dshrenik used Ask the Experts™
on
I am adding a JLabel to a JButton. I want to provide center alignment.

If possible, please provide some sample code. Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
This code does not seem to work. If possible, please let me know why:
JLabel labelButton1 = new JLabel("Read From Machine", SwingConstants.CENTER);
		labelButton1.setFont(ViewConstants.BUTTON_FONT);
		labelButton1.setSize(button1.getPreferredSize());
		button1.add(labelButton1);

Open in new window

Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

JLabel title = new JLabel("Read From Machine", JLabel.CENTER);

Author

Commented:
Event that did not work.
try this.

    labelButton1 .setHorizontalTextPosition(JLabel.CENTER);
    labelButton1 .setVerticalTextPosition(JLabel.CENTER);
   

Author

Commented:
Even that did not work!
Awarded 2011
Awarded 2011

Commented:
I think by default lable will be cener=terd on the button
just use

btn.add(lbl)

and it would be centered

I just tried and it worked that way

Author

Commented:
Unfortunately, it isn't! Could you tell me what I could be missing.

If possible, could you give me your code? Thanks!
Awarded 2011
Awarded 2011

Commented:
/*************************************************************************
* Buttons.java
*
*
*
***************************************************************************/

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Buttons extends JFrame
{

private JButton resetButton;
private JButton leftButton;
private JButton colorButton;
private JPanel buttonPanel;

private eventHandle evt;
private FlowLayout flt;
private FlowLayout flt1;
//Point point;
private Color color;

public Buttons()
{
super("Buttons Window");

flt = new FlowLayout(FlowLayout.CENTER);
flt1 = new FlowLayout(FlowLayout.LEFT);

buttonPanel = new JPanel();
buttonPanel.setLayout(flt);

    JLabel lbl = new JLabel("ttt");   


resetButton = new JButton("Reset");
    resetButton.add(lbl);  ///this is where I added lable
leftButton = new JButton("Left");
colorButton = new JButton("Blue");
evt = new eventHandle();

buttonPanel.add(leftButton);  // add left button
buttonPanel.add(colorButton);  // add color button
buttonPanel.add(resetButton);  // add reset button
getContentPane().add(buttonPanel);  // button panel

leftButton.addActionListener(evt);  // action listeners for all buttons
colorButton.addActionListener(evt);
resetButton.addActionListener(evt);
setBounds(20, 120, 320, 200);

setLocationRelativeTo(null);


color = buttonPanel.getBackground();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}

class eventHandle implements ActionListener
{

public void actionPerformed(ActionEvent e)
{
{
if (e.getSource() == leftButton)
{

buttonPanel.setLayout(flt1);
buttonPanel.validate();    


}
else if (e.getSource() == colorButton)
{
buttonPanel.setBackground(Color.BLUE);

} else  if (e.getSource() == resetButton)
{


buttonPanel.setBackground(color);
buttonPanel.setLayout(flt);
buttonPanel.validate();     

}
}
}
}

public static void main(String[] args)
{

Buttons btnwindow = new Buttons();
}
}  // end class Buttons

Open in new window

Awarded 2011
Awarded 2011

Commented:
The one with ttt is lable on button
label-on-button.PNG

Author

Commented:
Could you tell me if it works even when you resize the button to different dimensions? Thanks!
Awarded 2011
Awarded 2011
Commented:
this will center it:

/*************************************************************************
* Buttons.java
*
*
*
***************************************************************************/

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Buttons extends JFrame
{

private JButton resetButton;
private JButton leftButton;
private JButton colorButton;
private JPanel buttonPanel;

private eventHandle evt;
private FlowLayout flt;
private FlowLayout flt1;
//Point point;
private Color color;

public Buttons()
{
super("Buttons Window");

flt = new FlowLayout(FlowLayout.CENTER);
flt1 = new FlowLayout(FlowLayout.LEFT);

buttonPanel = new JPanel();
//buttonPanel.setLayout(flt);
    leftButton = new JButton("Left");
    colorButton = new JButton("Blue");

    evt = new eventHandle();
    


    JLabel lbl = new JLabel("ttt");


resetButton = new JButton();

   // resetButton.setWidth(100);
   // resetButton.add(lbl);




    Box buttonBar = Box.createHorizontalBox();
buttonBar.add(Box.createHorizontalGlue());
buttonBar.add(lbl);
buttonBar.add(Box.createHorizontalGlue());
      resetButton.add(buttonBar);

   

    buttonPanel.setLayout(new BorderLayout());



buttonPanel.add(resetButton);  // add left button
//buttonPanel.add(colorButton);  // add color button
//buttonPanel.add(resetButton);  // add reset button
getContentPane().add(buttonPanel);  // button panel

leftButton.addActionListener(evt);  // action listeners for all buttons
colorButton.addActionListener(evt);
resetButton.addActionListener(evt);
setBounds(20, 120, 320, 200);

setLocationRelativeTo(null);


color = buttonPanel.getBackground();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}

class eventHandle implements ActionListener
{

public void actionPerformed(ActionEvent e)
{
{
if (e.getSource() == leftButton)
{

buttonPanel.setLayout(flt1);
buttonPanel.validate();    


}
else if (e.getSource() == colorButton)
{
buttonPanel.setBackground(Color.BLUE);

} else  if (e.getSource() == resetButton)
{


buttonPanel.setBackground(color);
buttonPanel.setLayout(flt);
buttonPanel.validate();     

}
}
}
}

public static void main(String[] args)
{

Buttons btnwindow = new Buttons();
}
}  // end class Buttons

Open in new window

Awarded 2011
Awarded 2011

Commented:
Big Button created by the above code (this button btw still responds to events OK).
bigbutton.PNG

Author

Commented:
It does not work when I do this:

resetButton.setPreferredSize(new Dimension(100,300));
Awarded 2011
Awarded 2011

Commented:
buttonPanel has BorderLayout and BorderLayout does not pay attention to the preferred size

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial