Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Aligning Text and Images in Pop up Menus

 
Puneet Gupta
Greenhorn
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!!
Cheers
Puneet Gupta
 
ali rafiq
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(){

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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic