JMenuBar and JMenuItem
I need to have a JMenuItem in a JMenuBar but I'm having difficulties. If I have a JMenu with no children JMenuItems below it and I add that JMenu to the JMenuBar, I get a small square below the JMenu when I click it. I tried to add a JMenuItem to the JMenuBar in place of the childless JMenu but when I do that, the JMenuItem spans the width of the JMenuBar and that isn't good. Is there a way to have a JMenuItem as a part of a JMenuBar without the adverse effects described just now? Thanks, Jeremy
I used gridBagLayout to do it as follows
ivjTestFrameJMenuBar = new javax.swing.JMenuBar();
ivjTestFrameJMenuBar.setLayout(new GridBagLayout());
ivjTestFrameJMenuBar.setName("TestFrameJMenuBar");
java.awt.GridBagConstraints constraintsJMenu1 = new java.awt.GridBagConstraints();
constraintsJMenu1.gridx = 0; constraintsJMenu1.gridy = 0;
constraintsJMenu1.insets = new java.awt.Insets(4, 4, 4, 4);
ivjTestFrameJMenuBar.add(getJMenu1(), constraintsJMenu1);
java.awt.GridBagConstraints constraintsJMenuItem = new java.awt.GridBagConstraints();
constraintsJMenuItem.gridx = 1; constraintsJMenuItem.gridy = 0;
constraintsJMenuItem.insets = new java.awt.Insets(4, 4, 4, 4);
ivjTestFrameJMenuBar.add(new javax.swing.JMenuItem("Item 1"), constraintsJMenuItem);
java.awt.GridBagConstraints constraintsJMenu2 = new java.awt.GridBagConstraints();
constraintsJMenu2.gridx = 2; constraintsJMenu2.gridy = 0;
constraintsJMenu2.anchor = java.awt.GridBagConstraints.WEST;
constraintsJMenu2.weightx = 1.0;
constraintsJMenu2.insets = new java.awt.Insets(4, 4, 4, 4);
ivjTestFrameJMenuBar.add(getJMenu2(), constraintsJMenu2);see how it works for you, it seemed to work ok for me
Sok
Similar Messages
-
MouseEvents on JPopupMenu and JMenuItem
Hi
I am trying to get the mouse events from JPopupMenu
and JMenuItem , but nothing happens
I display the popup once a button is pressed , the popup is displayed but once I click it there is no response
can some one help
I am attaching the code
import javax.swing.*;
import java.awt.event.*;
public class Pop extends JFrame implements ActionListener{
JPopupMenu popup = new JPopupMenu();
JMenuItem item = new JMenuItem("one");
JMenuItem item2 = new JMenuItem("two");
JButton ok = new JButton("ok");
Pop(){
H hand = new H();
popup.add(item);
popup.add(item2);
popup.addMouseListener(hand);
item.addMouseListener(hand);
item2.addMouseListener(hand);
ok.addActionListener(this);
getContentPane().add(ok);
setSize(300,300);
show();
public void actionPerformed(ActionEvent ee){
popup.show(ok , 5,5);
public static void main(String[] args){
new Pop();
class H extends MouseAdapter{
public void mouseClicked (MouseEvent e){
JOptionPane.showMessageDialog(null,e.toString());
}If you are just trying to get something done when a menuitem is clicked, then you can try to use actionListener instead of the mouselistener. Try the following:
ActionListener al = new ActionListener()
public void actionPerformed()
OptionPane.showMessageDialog(null,e.toString());
item.addActionListener(al);
item2.addActionListener(al2);
Now when you click an item, the dialog box would be shown. -
How to put an image in front of the JMenu text?
JMenuItem has a constructor to insert the icon.
Is their a reason why it not in the JMenu constructors?
Sometimes I just want to put a 16 pixel gap in front of the JMenuItem or JMenu if it has no Image. Can one do that without inserting a blank
Image?
ThanksHave you tried just setting it via setIcon()? Since both JMenu and
JMenuItem inherit from AbstractButton, this may work; I say may
since more than likely it is up to the L&F UI to do the right thing.
A quick browse shows that it ought to work for at least the basic
L&Fs though.
As far as the empty gap, looks like you might be out of luck by
default, but this too is up to the L&F. For instance, some look and
feels line up the text properly if an item doesn't have an icon.
As a quick fix, though, you could probably just set the icon to
something like
menuItem.setIcon( new Icon() {
public int getIconWidth() { return 16; }
public int getIconHeight() { return 0; }
public void paintIcon(Component c, Graphics g, int x, int y) {}
});: jay -
InternalFrameListener and JMenuItem problem
Hi there, I did the following (just an outline, I know, that program is not working.... :))
class A{
static JMenuItem m_saveMenu;
A(){
JMenuBar mB = new JMenuBar();
m_saveMenu = new JMenuItem("Save As...");
class B{
JInternalFrame iFrame = new JInternalFrame();
iFrame.addInternalFrameListener(new MyListener());
class MyListener extends InternalFrameAdapter{
public void internalFrameActivated (InternalFrameEvent e){
A.m_saveMenu.setEnabled(true); }
public void internalFrameDeactivated(InternalFrameEvent e){
A.m_saveMenu.setEnabled(false);
So if I (in my working program) create a new JInternalFrame and activate it I get the following error message:
Exception occurred during event dispatching:
java.lang.NullPointerException
why could that be??
Thx
Charlyhi,
I mean this is obvious: You didn't initialize the menu item, you just declared it! And the listener might be used before the constructor of class A is called.
You could do the following now:
static JMenuItem m_saveMenu=new JMenuItem("Save As...");
unless you are sure that the constructor of class A and with it the initialization of the menu item goes ahead.
best regards, Michael -
How to add a JMenubar and a JTable in a JFrame in a single application
Hi all,
I require an urgent help from you.I am a beginer in programming Swing.I want to add a menu,combobox,and a table in a single application.I did coding as below:
package com.BSS;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.border.*;
import javax.swing.event.*;
public class newssa extends JFrame
public JMenuBar menuBar;
public JToolBar toolBar;
public JFrame frame;
private JLabel jLabel1;
private JLabel jLabel2;
private JLabel jLabel3;
private JLabel jLabel4;
private JLabel jLabel5;
private JLabel jLabel6;
private JComboBox jComboBox1;
private JComboBox jComboBox2;
private JComboBox jComboBox3;
private JComboBox jComboBox4;
private JComboBox jComboBox5;
private JComboBox jComboBox6;
private JTable jTable1;
private JScrollPane jScrollPane1;
private JPanel contentPane;
public newssa()
super();
initializeComponent();
this.setVisible(true);
private void initializeComponent()
jLabel1 = new JLabel();
jLabel2 = new JLabel();
jLabel3 = new JLabel();
jLabel4 = new JLabel();
jLabel5 = new JLabel();
jLabel6 = new JLabel();
jComboBox1 = new JComboBox();
jComboBox2 = new JComboBox();
jComboBox3 = new JComboBox();
jComboBox4 = new JComboBox();
jComboBox5 = new JComboBox();
jComboBox6 = new JComboBox();
frame=new JFrame();
//Included here
JMenuBar menuBar = new JMenuBar();
JMenu general = new JMenu("General");
menuBar.add(general);
JMenu actions =new JMenu("Actions");
menuBar.add(actions);
JMenu view=new JMenu("View");
menuBar.add(view);
JMenu Timescale=new JMenu("TimeScale");
menuBar.add(Timescale);
Timescale.add("Today CTRL+D");
Timescale.add("Current Week CTRL+W");
Timescale.add("Current Month CTRL+M");
Timescale.add("Current Quarter CTRL+Q");
Timescale.add("Current Year CTRL+Y");
Timescale.add("Custom TimeScale CTRL+U");
JMenu start=new JMenu("Start");
menuBar.add(start);
JMenu options=new JMenu("Options");
menuBar.add(options);
JMenu help=new JMenu("Help");
menuBar.add(help);
JFrame.setDefaultLookAndFeelDecorated(true);
frame.setJMenuBar(menuBar);
frame.pack();
frame.setVisible(true);
toolBar = new JToolBar("Formatting");
toolBar.addSeparator();
//Before this included new
String columnNames[] = { "ColorStatus", "Flash", "Service Order","Configuration","Configuration Description"};
// Create some data
String dataValues[][] =
{ "blue", "flash", "ORT001" },
{ "AVCONF", "av configuration with warrenty"}
// Create a new table instance
//jTable1 = new JTable( dataValues, columnNames );
jTable1 = new JTable(dataValues,columnNames);
jScrollPane1 = new JScrollPane(jTable1);
contentPane = (JPanel)this.getContentPane();
//scrollPane = new JScrollPane( table );
//topPanel.add( scrollPane, BorderLayout.CENTER );
// jLabel1
jLabel1.setText("Service Centers");
// jLabel2
jLabel2.setText("Service Areas");
// jLabel4
jLabel3.setText("Skills");
jLabel4.setText("Availablity Types");
// jLabel5
jLabel5.setText("From Date");
// jLabel6
jLabel6.setText("To");
// jComboBox1
jComboBox1.addItem("Coimbatore");
jComboBox1.addItem("Chennai");
jComboBox1.addItem("Mumbai");
jComboBox1.addItem("New Delhi");
jComboBox1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
jComboBox1_actionPerformed(e);
// jComboBox2
jComboBox2.addItem("North Zone");
jComboBox2.addItem("South Zone");
jComboBox2.addItem("Central Zone");
jComboBox2.addItem("Eastern Zone");
jComboBox2.addItem("Western Zone");
jComboBox2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
jComboBox2_actionPerformed(e);
// jComboBox3
jComboBox3.addItem("Microsoft Components");
jComboBox3.addItem("Java Technologies");
jComboBox3.addItem("ERP");
jComboBox3.addItem("Others");
jComboBox3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
jComboBox3_actionPerformed(e);
// jComboBox4
jComboBox4.addItem("One");
jComboBox4.addItem("Two");
jComboBox4.addItem("Three");
jComboBox4.addItem("Four");
jComboBox4.addItem("Five");
jComboBox4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
jComboBox4_actionPerformed(e);
// jComboBox5
jComboBox5.addItem("12/12/2004");
jComboBox5.addItem("13/12/2004");
jComboBox5.addItem("14/12/2004");
jComboBox5.setEditable(true);
jComboBox5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
jComboBox5_actionPerformed(e);
// jComboBox6
jComboBox6.addItem("12/11/2004");
jComboBox6.addItem("13/11/2004");
jComboBox6.addItem("14/11/2004");
jComboBox6.setEditable(true);
jComboBox6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
jComboBox6_actionPerformed(e);
// jTable1
jTable1.setModel(new DefaultTableModel(4, 4));
// jScrollPane1
jScrollPane1.setViewportView(jTable1);
// contentPane
contentPane.setLayout(null);
addComponent(contentPane, jLabel1, 2,29,84,18);
addComponent(contentPane, jLabel2, 201,33,76,18);
addComponent(contentPane, jLabel3, 384,32,59,18);
addComponent(contentPane, jLabel4, 2,77,85,18);
addComponent(contentPane, jLabel5, 197,79,84,18);
addComponent(contentPane, jLabel6, 384,80,60,18);
addComponent(contentPane, jComboBox1, 85,32,100,22);
addComponent(contentPane, jComboBox2, 276,32,100,22);
addComponent(contentPane, jComboBox3, 419,30,100,22);
addComponent(contentPane, jComboBox4, 88,76,100,22);
addComponent(contentPane, jComboBox5, 276,79,100,22);
addComponent(contentPane, jComboBox6, 421,78,100,22);
addComponent(contentPane, jScrollPane1, 33,158,504,170);
// newssa
this.setTitle("SSA Service Scheduler");
this.setLocation(new Point(0, 0));
this.setSize(new Dimension(560, 485));
/** Add Component Without a Layout Manager (Absolute Positioning) */
private void addComponent(Container container,Component c,int x,int y,int width,int height)
c.setBounds(x,y,width,height);
container.add(c);
// TODO: Add any appropriate code in the following Event Handling Methods
private void jComboBox1_actionPerformed(ActionEvent e)
int index = jComboBox1.getSelectedIndex();
switch(index)
case 0: System.out.println("Area Coimbatore Selected "); break;
case 1: System.out.println("Area Chennai selected"); break;
case 2: System.out.println("Mumbai being selected"); break;
case 3: System.out.println("New Delhi being selected"); break;
private void jComboBox2_actionPerformed(ActionEvent e)
int index = jComboBox2.getSelectedIndex();
switch(index)
case 0: System.out.println("North Zone Selcted "); break;
case 1: System.out.println("South Zone being selected"); break;
case 2: System.out.println("Central Zone being selected"); break;
case 3: System.out.println("Eastern Zone being selected"); break;
case 4: System.out.println("Western Zone being selected"); break;
private void jComboBox3_actionPerformed(ActionEvent e)
int index = jComboBox3.getSelectedIndex();
switch(index)
case 0: System.out.println("Microsoft Components being selected"); break;
case 1: System.out.println("Java Technologies being selected"); break;
case 2: System.out.println("ERP Tehnologies being selected"); break;
case 3: System.out.println("Other's selected"); break;
private void jComboBox4_actionPerformed(ActionEvent e)
int index = jComboBox4.getSelectedIndex();
switch(index)
case 0: System.out.println("One selected"); break;
case 1: System.out.println("Two selected"); break;
case 2: System.out.println("Three selected"); break;
case 3: System.out.println("Four selected"); break;
case 4: System.out.println("Five selected"); break;
private void jComboBox5_actionPerformed(ActionEvent e)
int index = jComboBox5.getSelectedIndex();
switch(index)
case 0: System.out.println("12/12/2004 being selected"); break;
case 1: System.out.println("13/12/2004 being selected"); break;
case 2: System.out.println("14/12/2004 being selected"); break;
private void jComboBox6_actionPerformed(ActionEvent e)
int index = jComboBox6.getSelectedIndex();
switch(index)
case 0: System.out.println("12/11/2004 being selected"); break;
case 1: System.out.println("13/11/2004 being selected"); break;
case 2: System.out.println("14/11/2004 being selected"); break;
public static void main(String[] args)
newssa ssa=new newssa();
//JFrame.setDefaultLookAndFeelDecorated(true);
//JDialog.setDefaultLookAndFeelDecorated(true);
//JFrame frame = new JFrame("SSA Service Scheduler");
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setJMenuBar(ssa.menuBar);
//frame.getContentPane( ).add(ssa.toolBar, BorderLayout.NORTH);
//frame.getContentPane( ).add(ssa.pane, BorderLayout.CENTER);
//frame.pack( );
//frame.setVisible(true);
try
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
catch (Exception ex)
System.out.println("Failed loading L&F: ");
System.out.println(ex);
But as a O/P ,I am getting menu in a seperate windos and the rest of the combobox and jtable in a seperate window.Kindly help me to solve the application.
VERY URGENT PLEASE..
Thanks in advance
with kind regds
Satheesh.KBut did u mean this as the next problem,Which I will come across..Yes, the second setVisible(true) seemed to be producing a smaller frame behind the main frame.
And your JMenuBar is declared twice, but not sure if this will affect the code - haven't read it all. -
i have a JMenu called jMenu2, in its action performed method i would like it to add to the bottom of its list a new JMenuItem while the program is running.
private void jMenu2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jMenu2.add("hi");
}so why can i not see this new menu item? i have tried many refreshing methods from JMenu with no luckI wouldn't expect it to show up immediately if it's being added as a result of clicking another item on the same JMenu, only next time you pull that JMenu down. If that doesn't happen, I would try creating a new JMenuItem("hi"), call setVisible(true) on it, and then add it to the JMenu. You shouldn't need to revalidate or repaint anything.
-
Is it possible to have a JMenuItem within a JMenuItem? For example: Menu would drop down to a JMenuItem with two choices and when you click on one of the two choices another JMenuItem would have more choices available?
Read this section from the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html]How to Use Menus for an example.
-
The problem is a simple one.
Simply I don't know how to have the two together, every time I try to create a contentPane with the buttons in it I always get errors I would love it if some one could show me how to do this.
Thanximport java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class Test extends JFrame {
public Test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = getContentPane();
content.setLayout(new FlowLayout());
JMenuBar jmb = new JMenuBar();
setJMenuBar(jmb);
JMenu jm = new JMenu("Menu");
jmb.add(jm);
for (int i=0; i<5; i++) {
JMenuItem jmi = new JMenuItem("MenuItem-"+i);
jm.add(jmi);
jmi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.out.println(((JMenuItem)ae.getSource()).getText());
JButton jb = new JButton("Button-"+i);
content.add(jb);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.out.println(((JButton)ae.getSource()).getText());
setSize(200,400);
show();
public static void main(String[] args) { new Test(); }
} -
hello, I am using the JMenuBar for my simple program, I got two menus to the left, Figures and colors, and to the right I have a About box, this about box is supposed to show me Info on top of the drawingboard,�I want to be able to hardtype some text into to messagebox/popupbox but I cant find the appropiate method...anyone got an idea?
sincerarly,Use a JOptionPane. Look at the JOptionPane section here:
http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/
colr__ -
Hi frds,
I add the JMenu after that i add JMenuIteam.
In add Three menuiteam..
so which event i will for that..?
if i will click on one menuiteam then open one iteam like that so which event i wil add for that.?
so please give me brief information.
Thank you,Hi frds,
I add the JMenu after that i add JMenuIteam.
In add Three menuiteam..
so which event i will for that..?
if i will click on one menuiteam then open one iteam
like that so which event i wil add for that.?
so please give me brief information.
Thank you,(only 50% sure what your talking about)
try implementing ActionListener and register your menu items using addActionListener -
hi
does anybody know how i can disable menuitems
and enable them on some given events?
would help me very much!
thanxA good way to do this is to use Actions - define an Action object for each menu item (including text and an icon if you want), then just add the actions to the menu, and it will show them with icons automatically.
Then if you do setEnabled(false) on the action, the menu item is greyed out. Also, you can add these Actions to other components such as a JToolBar, which would automatically render them as buttons (these also get greyed out when disabed).
The big benefit is that all your "action" code is in one place, so the menu and the toolbar both call the same code.
For a tutorial, see
http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html -
Custom text for JMenu and JMenuItem
hey guys
right now i can do a :
JMenu menu = new JMenu("File");but how can i customize the text on the Menu a.k.a "file" so may be it looks bigger or a different color ?
ive looked at the API i can see that JMenu takes "Action" object too which describes the look. I know Action is an interface and i havent been able to find a suitable class that implements this interface
so how should i go about changing the text look/feel ?
thank in advanceso may be it looks bigger or a different color ?Read the API. There are many different set??? methods which allow you to change the properties of the component including the font size and color of the text.
i can see that JMenu takes "Action" object too which describes the look.The Action has nothing to do with the look (other then providing the text to display). It has to do with the Action that happens when you select the menu item. -
I have a JMenu attached to a JFrame, along with a panel and a graphic object which extends Canvas. When I click the Menu bar, the menu does not show, it actually falls behind the Canvas. I can see it if I remove the Canvas. How do you make it appear over the Canvas or the items drawn over the Canvas?
Any help will be greatly appreciated.
Pradeep Gupta
[email protected]
Mon 03Nov03 10:31You are mixing heavyweight and lightweight components. Can you subclass JComponent or JPanel instead of Canvas? That would solve your problem.
-
JAWS and JMenuItem - PLEASE HELP
Hi,
I try to access to disabled menu options with JAWS 6.0 but it can read this.
How I can do that JAWS read de disabled menu options, same that appears on Windows Menus?
Thanks,
Mentu
PS: Sorry for my bad english.Well, I modified the code I provided a little ... if you need it just ask ... and fixed all my keyboard problems. As for the mouse reading the wrong thing, I rewrote the mouseListener and set the accessibleName before the selection is made and it works. This is just crazy ... Sun needs to make their widgets more accessible in the next release. I am doing everything that sun specifically says not to do in order to make stuff accessible ... but what else can I do?
-
How can I distinguish a JMenuItem , JPopupMenu and TopLevelMenu?
I used the following methods to get the component who (a menu)gains the focus:
JComponent com = JFrmame.getFocusOwner();
or
MenuSelectionManager menuMgr = MenuSelectionManager.defaultManager();
MenuElement[] menuPath = menuMgr.getSelectedPath();
if(menuPath != 0) {
JComponent comp = Array.get(menuPath, menuPath.length-1); // this is the item with the focus
then I need to use getAction() to get the menuItem's action. But how can I filter JMenuItem and JPopUpMenu from TopLevelMenu (JMenuBar item) ?
Thanks!The instanceof may provide the simplest solution, since JPopupMenu, JMenuBar and JMenuItem don't have any kind of ancestor relationship with each other.if (comp instanceof JPopupMenu) {
// Popup
} else if (comp instanceof JMenuItem) {
// Item
} else if (comp instanceof JMenuBar) {
// Bar
} else ...?Hope this helps,
-Troy
Maybe you are looking for
-
Time machine didn't back up all originals in iPhoto
I have been trying to restore my iPhoto library (iLife 9) to an alternative Macbook after a Hard Drive failure. I can all the thumbnails to show but only the full res photos for those taken in 2011. If I click on earlier ones I just get a triangle wi
-
Hello All, I was trying to see if there is any way that we can get the choices for a Field from another filed in the same item in a Document Library. To be specific. I have Created a Project Document Set, Which has a Column called "Project Members" w
-
Error while posting billing document - Error belongs to Business area/G/L a
Hi All, When i am posting the billing document i ma getting below error message: Business Area must be entered, Pl check if the Business Area is correct Message no. ZT017 I think it is due to MWST tax condtion type and respective G/L account only. Wh
-
Is data dvd copied from mac with itunes 7 work with windows itunes 7
I have a new laptop pc. My itunes library is on my emac. I want to authorize the laptop and put my itunes library on both computers. If I back up my itunes library on DVD from my mac desktop can I use the DVDs to load my library onto the laptop pc. B
-
Embedding media player in sap gui
Hello, i want to play a media file in the sap gui. i can play a file using the cl_gui_frontend_services=>execute method, but i don't want the media player to pop up. i have also checked out this blog entry /people/thomas.jung3/blog/2005/05/11/using-