0

I know this is me being completely ocd, but I am having an issue centering the JLabel text within the JPopupMenu.

I've tried popup.add(new JLabel("Menu",JLabel.CENTER)); and label.setHorizontalAlignment(SwingConstants.CENTER); label.setHorizontalTextPosition(SwingConstants.CENTER)); but no luck.

pic

Anyone know a fix?

Community
  • 1
  • 1
Simon Rubin
  • 198
  • 11

2 Answers2

2

Set the proper layout of the JPopupMenu such as BoxLayout, FlowLayout etc. and add the JLabel that is positioned based on the layout manager.

Sample code:

JPopupMenu popup = new JPopupMenu();
popup.setLayout(new FlowLayout(FlowLayout.CENTER));
popup.add(new JLabel("Menu"));

Read more about A Visual Guide to Layout Managers where each layout is explained in details along with sample code.

Braj
  • 46,415
  • 5
  • 60
  • 76
0

Thanks I fixed it with:

popup.setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
JLabel label = new JLabel("Menu");
label.setAlignmentX(CENTER_ALIGNMENT);
Simon Rubin
  • 198
  • 11