It's not a secret anymore!
The moose likes Swing / AWT / SWT and the fly likes Aligning Text and Images in Pop up Menus Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Aligning Text and Images in Pop up Menus" Watch "Aligning Text and Images in Pop up Menus" New topic

Aligning Text and Images in Pop up Menus

Puneet Gupta

Joined: Jun 11, 2002
Posts: 6
Hi Java Gurus,
How to align Text and images in Menu that Pops up when user clicks on the Menu in Menubar. I want it to look something similar to windows application
pop up menus. Is there any solution besides putting empty transparent images in case of Text only menuItems.
Any pointers and reference will be of great help!!
Puneet Gupta
ali rafiq
Ranch Hand

Joined: Jan 09, 2003
Posts: 75
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class MenuDemo extends JFrame
public JTextPane jtp ;
public JMenuBar jmb;
public MenuDemo(){

jtp = new JTextPane();
jmb = new JMenuBar();
Container con = getContentPane();
con.setLayout(new FlowLayout());

JMenu ju = new JMenu("JUSTIFY");
JCheckBoxMenuItem jcbm = new JCheckBoxMenuItem("LEFT", new ImageIcon("left.gif"));
MenuAction ma = new MenuAction("Left", new ImageIcon("left.gif"));
JMenuItem jm = new JMenuItem("Helloo");
JMenuItem jm1 = new JMenuItem("Click Next");
ju.add(new JSeparator());
JMenuItem jmi = ju.add(ma);
ju.add(new JSeparator());
jtp.setPreferredSize(new Dimension(250,250));
jtp.setBorder(new BevelBorder(BevelBorder.LOWERED));
con.add(jmb, BorderLayout.NORTH);
con.add(jtp, BorderLayout.CENTER);
class MenuAction extends AbstractAction
public MenuAction(String str , Icon icon)
public void actionPerformed(ActionEvent e){
jtp.getStyledDocument().insertString(0,"Action {"+e.getActionCommand() +"} Performed!\n",null);
}catch(Exception ex){}


public static void main(String []args)
MenuDemo md = new MenuDemo();

that a simple file for u ...

if u learn to break the rock .. next day u will be looking to Mountain.
Puneet Gupta

Joined: Jun 11, 2002
Posts: 6
Hi Ali,
I followed your advice but it did not work!! The Pop up menus are displayed as it was being displayed earlier. I am using Java look and feel.
With Windows look and feel it gives the desired result!!!
Any comments will be heartly solicited!!
I agree. Here's the link:
subject: Aligning Text and Images in Pop up Menus
It's not a secret anymore!