Java Swing seems to place the 'Menu Text' after the icon (if present) on MenuItems. See example below.
It does NOT look very nice.
Is there a way around this?
On OSX the icon fits in the left margin and the text aligns with all other MenuItems.
Java Swing seems to place the 'Menu Text' after the icon (if present) on MenuItems. See example below.
It does NOT look very nice.
Is there a way around this?
On OSX the icon fits in the left margin and the text aligns with all other MenuItems.
Do you mean something like this :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JTextPaneExample
{
private Icon info = UIManager.getIcon("OptionPane.informationIcon");
private Icon error = UIManager.getIcon("OptionPane.errorIcon");
private void createAndDisplayGUI()
{
JFrame frame = new JFrame("JTextPane Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextPane tpane = new JTextPane();
tpane.setContentType("text/html");
JScrollPane scroller = new JScrollPane();
scroller.setViewportView(tpane);
try
{
java.net.URL url = new java.net.URL("http://maps.google.es/");
//tpane.setPage(url);
}
catch (Exception e)
{
e.printStackTrace();
}
frame.setJMenuBar(createMenuBar());
frame.getContentPane().add(scroller);
frame.setSize(300, 300);
frame.setVisible(true);
}
private JMenuBar createMenuBar()
{
JMenuBar menuBar = new JMenuBar();
JMenu windowMenu = new JMenu("Window");
JMenuItem minimizeItem = new JMenuItem("Minimize");
minimizeItem.setMargin(new java.awt.Insets(0, 10, 0, 0));
minimizeItem.setIcon(info);
minimizeItem.setIconTextGap(1);
minimizeItem.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
JMenuItem zoomItem = new JMenuItem("Zoom");
zoomItem.setMargin(new java.awt.Insets(0, 10, 0, 0));
zoomItem.setIconTextGap(1);
zoomItem.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem("Check Me", null, true);
cbmi.setMargin(new java.awt.Insets(5, 25, 5, 5));
cbmi.setIconTextGap(17);
cbmi.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
windowMenu.add(minimizeItem);
windowMenu.add(zoomItem);
windowMenu.add(cbmi);
menuBar.add(windowMenu);
return menuBar;
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new JTextPaneExample().createAndDisplayGUI();
}
});
}
}
Here is the Output :

You could try either of these approaches:
Unicode characters are appealing, but they offer poor alignment in a variable pitch font:
JMenuBar menuBar = new JMenuBar();
JMenu windowMenu = new JMenu("Window");
windowMenu.add(new JMenuItem("♦ Item"));
windowMenu.add(new JMenuItem("✓ Item"));
windowMenu.add(new JMenuItem("• Item"));
menuBar.add(windowMenu);
frame.setJMenuBar(menuBar);
Better, implement the Icon interface, illustrated here and here, using a fixed-size implementation to control geometry. CellTest shows one approach to rendering an arbitrary unicode glyph.