[RESOLVED] Menu and popup menu can't use the same menu item?
Hi, I'm trying to do this: "fileMenu" and "popupMenu" use the same menu item, but it didn't show correctly. When I add redMenuItem to popupMenu, redMenuItem disappears in fileMenu.
Please help me.
Thanks.
Code:
package gui2;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
public class AFrame extends JFrame {
private JRadioButtonMenuItem redMenuItem;
private JMenu fileMenu;
private JMenuBar bar;
private JPopupMenu popupMenu;
public AFrame()
{
super("Test");
bar = new JMenuBar();
fileMenu = new JMenu("File");
redMenuItem = new JRadioButtonMenuItem("Red");
popupMenu = new JPopupMenu();
fileMenu.add(redMenuItem);
bar.add(fileMenu);
setJMenuBar(bar);
popupMenu.add(redMenuItem); // if comment out this line,
// redMenuItem will appear in file menu.
// if uncomment out this line,
// redMenuItem will disappear in file menu.
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e) {
checkForPopupTrigger(e);
}
public void mouseReleased(MouseEvent e) {
checkForPopupTrigger(e);
}
private void checkForPopupTrigger(MouseEvent e)
{
if(e.isPopupTrigger())
popupMenu.show(e.getComponent(), e.getX(), e.getY());
}
}
);
}
public static void main(String[] args) {
AFrame app = new AFrame();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setSize(500, 500);
app.setLocation(300, 200);
app.setVisible(true);
}
}
Re: Menu and popup menu can't use the same menu item?
You can't use the same GUI component twice in this way. The best approach is to have two different menu item objects named "Red". If they both do the same thing then assign the same ActionListener (or Action object) to both of them.
Re: Menu and popup menu can't use the same menu item?
Can't really? I have doubt about that because I think it's a reference so it can be used in many places.
I need one more answer. Anyway, thank you ^^.
Re: Menu and popup menu can't use the same menu item?
Quote:
Originally Posted by
Emerald214
Can't really? I have doubt about that because I think it's a reference so it can be used in many places. I need one more answer. Anyway, thank you.
So does it work or not? You asked the question because it didn't work, and you got an answer telling you that you that it won't work and suggesting a working alternative. You want a second opinion?
Why not take a look at the source code, and trace for yourself what happens when you add a menu item to a menu.
Teachers open the door, but you must enter by yourself...
Chinese proverb
Re: Menu and popup menu can't use the same menu item?
Quote:
Originally Posted by Emerald214
Can't really? I have doubt about that because I think it's a reference so it can be used in many places.
I need one more answer. Anyway, thank you ^^.
A polite response would have been to ask why it won't work :rolleyes:
Of course if you had spent a little time thinking about it it would be obvious. How can you have the same component simultaneously displaying in multiple places when components have methods like getLocation(), getParent(), getSize(), hasFocus() etc.
Re: [RESOLVED] Menu and popup menu can't use the same menu item?
I'm very sorry for my poor English. I'm shamed for my response. I wish I could go to the past and do it better. :(
Code:
for(int i = 1; i < 1000; ++i)
JOptionPane.showMessageDialog(null, "Sorry");
Re: [RESOLVED] Menu and popup menu can't use the same menu item?
Code:
for(int i = 1; i < 1000; ++i)
JOptionPane.showMessageDialog(null, "Sorry");
:D
Don't worry we have all written posts that have come across as being more to the point than we intended.