This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
Author

Aligning Text and Images in Pop up Menus

Puneet Gupta
Greenhorn

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!!
Cheers
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(){

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 ...


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

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!!
Cheers
Puneet
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Aligning Text and Images in Pop up Menus
 
Similar Threads
JFrame vs JOptionPane
Automated testing tool for GUI in Java
menu bars?
menu bars and frames?
Java Menus Package