| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| Java Programming Ask your Java programming question and help out others with theirs. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
[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);
}
}
Last edited by Emerald214; October 15th, 2009 at 02:20 AM. |
|
#2
|
|||
|
|||
|
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.
|
|
#3
|
|||
|
|||
|
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 ^^. Last edited by Emerald214; October 15th, 2009 at 06:18 AM. |
|
#4
|
|||
|
|||
|
Re: Menu and popup menu can't use the same menu item?
Quote:
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
__________________
Please use [CODE]...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present. |
|
#5
|
|||
|
|||
|
Re: Menu and popup menu can't use the same menu item?
Quote:
![]() 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. |
|
#6
|
|||
|
|||
|
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"); Last edited by Emerald214; October 28th, 2009 at 04:38 PM. |
|
#7
|
|||
|
|||
|
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"); ![]() Don't worry we have all written posts that have come across as being more to the point than we intended. |
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|