centering text in a JMenu

5.8k views Asked by At

Alright, I have been cruising through the net looking for some help with this one, but nothing I try seems to work. I want to have all of the menu texts centered on their menu buttons. When I use setHorizontalTextPosition(JMenu.CENTER) there is no change. In fact, regardless of the constant I use, the menu text remains left justified.

Any ideas?

*UPDATE 1

Still no luck. After re-reading the JMenu API, I realized that setHorizontalTextPosition(int) and setVerticalTextPosition(int) only set the text in relation to an icon and setHorizontalAlignment(int) and setVerticalAlignment(int) set the alignment of both text and icon in the JMenu.

The text positioning methods seem to work as advertised when used with an icon.

The vertical alignment method seems to work on text or icon or both.

However, the horizontal alignment method doesn't seem to work at all. Even just using it alone on text or icon or both, nothing seems to change. I have increased the menu size to 150x50 and, no matter the combination, I can't get the text or icon or either to center horizontally.

5

There are 5 answers

6
David Kroukamp On

Try setHorizontalTextPosition(SwingConstants.CENTER); followed by invalidate();

alternatively use margins:

JMenu optionsMenu = new JMenu("Option");
optionsMenu.setMargin(new Insets(5, 50, 5, 5));

I made a short SSCCE to see if I could re-enact the problem but mine seems perfectly aligned from the start no need for any of the above:

Link to screenshot: http://img713.imageshack.us/img713/1489/67449235.jpg

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.SwingUtilities;

public class JMenuCenterTest extends JFrame {

    public JMenuCenterTest() {
        createUI();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new JMenuCenterTest().setVisible(true);
            }
        });
    }

    private void createUI() {
        setTitle("JMenu Center Test");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300, 300);
        JMenuBar bar = new JMenuBar();
        JMenu menu1 = new JMenu("File");
        //menu1.setMargin(new Insets(5, 50, 5, 5));
        JMenu menu2 = new JMenu("Options");
        JMenu menu3 = new JMenu("Help");
        //menu1.setHorizontalTextPosition(SwingConstants.CENTER);
        // invalidate();
        bar.add(menu1);
        bar.add(menu2);
        bar.add(menu3);
        getContentPane().add(bar, BorderLayout.NORTH);
    }
}

Reference:

6
bcr On

I think setHorizontalAlignment should do what you want whereas setHorizontalTextPosition specifies text position relative to an icon.

2
MadProgrammer On

Okay, I've played around with this and I've come to the conclusion that I don't think it's possible by setting the properties of the JMenu.

Anything you do is been overridden by the UI delegate.

Try this:

JMenu menu = new JMenu("<html><table width='100%'><tr><td halign='center' align='center' bgcolor='red'>File</td></tr></table></html>");

menuBar.add(menu);

You find that no matter how wide you make the menu, the area supplied to the text is ALSO just large enough to render it.

Example of menu text

The only choice I can see you have left is to supply your own UI delegate which is IMHO a really, really bad idea which will only eventually blow up in your face, but that's just my opinion :P

1
Rudi Kershaw On

Going on MadProgrammer's theme of using HTML, I used CSS to position and style the text in the menus. style="text-align:center;" is the CSS used to position the text in the center. In my case, I encapsulated the JMenu title in a <p> paragraph tags inside an <html> element;

clientFilter = new JMenu("<html><p style='text-align:center;'>Client</p></html>");
siteFilter = new JMenu("<html><p style='text-align:center;'>Site</p></html>");
employeeFilter = new JMenu("<html><p style='text-align:center;'>Employee</p></html>");
jobtypeFilter = new JMenu("<html><p style='text-align:center;'>Job Type</p></html>");

Which looks like this;

enter image description here

It's worth noting that in the image above I have also used width:90px;color:blue; in the style attribute, but I removed those two styles from the code example above for simplicity. Hope this helps.

0
CallMarl On

So the solution is:

private JMenuBar menu;
private JMenu file;

Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
double height = (double)screenSize.getHeight();
double widht = (double)screenSize.getWidth();

double frameh = 0.3*height; int frameH =(int)Math.ceil(frameh);
double framew = 0.3*widht;  int frameW = (int)Math.ceil(framew);

double menuh = 0.1*frameh;      int menuH = (int)Math.ceil(menuh);
double menuw = framew;          int menuL = frameW;

double fileh = menuh;       int fileH = menuH;
double filew = 0.1*menuw;   int fileW = (int)Math.ceil(filew);

    int textPos= fileW-10; //at the start of the button you have a little margin and you must leave somme pixel to fixe the center.

Window(){

    initialisation();
    construction();

    this.setTitle("Tutoriel 1.3 HS");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(frameW, frameH);
    this.setLocationRelativeTo(null);
    this.setVisible(true);
}

public void initialisation(){       
    file = new JMenu(("<html><table width='"+textPos+"'><tr><td align='center'>File</td></tr></table></html>"));// this is accept


    file.setPreferredSize(new Dimension(fileW, fileH));
    menu = new JMenuBar();
    menu.setPreferredSize(new Dimension(menuL, menuH));
    menu.add(file);     
}

public void construction(){
    this.setJMenuBar(menu);
}