Centering text in JMenu

Well, I surfed the net looking for some help with this, but I'm not trying anything to work. I want all menu texts to focus on the menu buttons. When I use setHorizontalTextPosition(JMenu.CENTER) , there are no changes. In fact, regardless of the constant I use, the menu text remains justified.

Any ideas?

* UPDATE 1

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

Text positioning methods seem to work the way they are advertised using the icon.

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

However, the horizontal alignment method does not work at all. Even just using it only by text or icon, or both, nothing changes. I increased the size of the menu to 150x50 and, regardless of the combination, I can not get the text or icon or horizontally horizontally.

+3
source share
5 answers

Following the MadProgrammer theme using HTML , I used CSS to position and style the text in the menu. style="text-align:center;" is the CSS used to center text. In my case, I encapsulated the JMenu header in the <p> paragraph tags inside the <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 is worth noting that in the image above, I also used width:90px;color:blue; in the style attribute, but I removed these two styles from the above code example for simplicity. Hope this helps.

+3
source

Try setHorizontalTextPosition(SwingConstants.CENTER); , and then invalidate();

alternatively use the fields :

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

I did a short SSCCE to see if I can repeat the problem, but I seem to be completely aligned from the start, there is no need for any of the above:

Screenshot link: 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); } } 

Link:

+2
source

Well, I played around with it, and I came to the conclusion that I do not think it is possible by setting the properties of JMenu .

Everything you do has been overridden by a user interface delegate.

Try the following:

 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 will find that no matter how wide you create the menu, the area provided to the text is ALSO large enough to display it.

Example of menu text

The only choice that I see from you is to provide your own user interface, which is IMHO, really, a very bad idea that will only finally explode in your face, but this is just my opinion: P

+2
source

I think setHorizontalAlignment should do what you need, and setHorizontalTextPosition indicates the position of the text relative to the icon.

+1
source

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); } 
+1
source

All Articles