• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Aligning Text and Images in Pop up Menus

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
Cheers
Puneet Gupta
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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(){

super("Demo");
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"));
jcbm.setHorizontalTextPosition(JMenuItem.RIGHT);
jcbm.setMnemonic('L');
MenuAction ma = new MenuAction("Left", new ImageIcon("left.gif"));
JMenuItem jm = new JMenuItem("Helloo");
ju.add(jm);
JMenuItem jm1 = new JMenuItem("Click Next");
ju.add(new JSeparator());
ju.add(jm1);
JMenuItem jmi = ju.add(ma);
ju.add(new JSeparator());
ju.add(jcbm);
jmb.add(ju);
jtp.setPreferredSize(new Dimension(250,250));
jtp.setBorder(new BevelBorder(BevelBorder.LOWERED));
con.add(jmb, BorderLayout.NORTH);
con.add(jtp, BorderLayout.CENTER);
setVisible(true);
setSize(300,300);
}
class MenuAction extends AbstractAction
{
public MenuAction(String str , Icon icon)
{
super(str,icon);
}
public void actionPerformed(ActionEvent e){
try{
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
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
Cheers
Puneet
reply
    Bookmark Topic Watch Topic
  • New Topic