Center align JButton's JLabel

I am adding a JLabel to a JButton. I want to provide center alignment.

If possible, please provide some sample code. Thanks!
dshrenikAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dshrenikAuthor 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

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

chaitu chaituCommented:
JLabel title = new JLabel("Read From Machine", JLabel.CENTER);
0
dshrenikAuthor Commented:
Event that did not work.
0
chaitu chaituCommented:
try this.

    labelButton1 .setHorizontalTextPosition(JLabel.CENTER);
    labelButton1 .setVerticalTextPosition(JLabel.CENTER);
   
0
dshrenikAuthor Commented:
Even that did not work!
0
for_yanCommented:
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
0
dshrenikAuthor Commented:
Unfortunately, it isn't! Could you tell me what I could be missing.

If possible, could you give me your code? Thanks!
0
for_yanCommented:
/*************************************************************************
* 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

0
for_yanCommented:
The one with ttt is lable on button
label-on-button.PNG
0
dshrenikAuthor Commented:
Could you tell me if it works even when you resize the button to different dimensions? Thanks!
0
for_yanCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
for_yanCommented:
Big Button created by the above code (this button btw still responds to events OK).
bigbutton.PNG
0
dshrenikAuthor Commented:
It does not work when I do this:

resetButton.setPreferredSize(new Dimension(100,300));
0
for_yanCommented:
buttonPanel has BorderLayout and BorderLayout does not pay attention to the preferred size
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.