Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Aligning Text and Images in Pop up Menus

Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
Puneet Gupta
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
They worship nothing. They say it's because nothing lasts forever. Like this tiny ad:
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic