Netbeans JPopupMenu Issues - java

I made a popup menu in Netbeans and I want to make a menu item that sends me to another GUI when I press it, but I don't know how.
I have to do something like a restaurant menu and when someone presses the button from the menu to send them to a specific type of food.
I made it only to popup when I right click it.
private void formMousePressed(java.awt.event.MouseEvent evt) {
if(evt.isPopupTrigger()){
jPopupMenu1.show(evt.getComponent(),evt.getX(),evt.getY());
}
}
private void formMouseReleased(java.awt.event.MouseEvent evt) {
if(evt.isPopupTrigger()){
jPopupMenu1.show(evt.getComponent(),evt.getX(),evt.getY());
}
}

"I want to make a menu item that sends me to another GUI when I press it, but I don't know how."
I'm, not sure how the drag and drop works with GUI Builder for JPopupMenu. I tried to to drap and drop it, but it won't show the display, so I couldn't just drag and drop JMenuItems to it. So I had to hand code it.
I dragged a JPopupMenu to the frame (jPopupmenu1);
In the constructor, I added a JMenuItem to it.
I added an ActionListener to the JMenuItem
Just show instantiate the second GUI , and maybe dispose of the second, depending on your preference
public NewJFrame() {
initComponents();
JMenuItem item1 = new JMenuItem("Open GUI2");
jPopupMenu1.add(item1);
item1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
new GUI2();
}
});
}
....
private void formMousePressed(java.awt.event.MouseEvent evt) {
if (evt.isPopupTrigger()) {
jPopupMenu1.show(evt.getComponent(), evt.getX(), evt.getY());
}
}
private void formMouseReleased(java.awt.event.MouseEvent evt) {
if (evt.isPopupTrigger()) {
jPopupMenu1.show(evt.getComponent(), evt.getX(), evt.getY());
}
}
It works fine for me

Related

How to "close" a JMenu in an Event Handler - Java

I have a simple MouseAdapter that I add to a JMenu. I want the menu to show its contents when moused over, react when clicked on, and hide its contents when the mouse leaves it. Here's where I add the listener:
public final void addGuiReaction(Runnable clickReaction) {
JMenu THIS = this;
this.addMouseListener(new MouseAdapter() {
final Runnable reaction = clickReaction;
final JMenu source = THIS;
#Override
public void mouseClicked(MouseEvent arg0) {reaction.run();}
#Override
public void mouseEntered(MouseEvent arg0) {source.doClick();} // Opens the menu
#Override
public void mouseExited(MouseEvent arg0) {/* Need to "close" the menu */}
});
}
This works just fine for both the reaction and the open when moused over functionality, but I can't figure out how to close it. Tried both setSelected(false) and setPopupMenuVisible(false) but the issue with both is that the menu doesn't open again the next time I mouse over it.
Anyone knows a nice way of closing the menu?
Answering to "How to close the JMenu..."
In place of this:
/* Need to "close" the menu */
You can put this:
source.dispatchEvent(new KeyEvent(source, KeyEvent.KEY_PRESSED, 0, 0, KeyEvent.VK_ESCAPE));
Hence simulate the ESC key press which will trigger the respective Action from the ActionMap of the JMenuBar.
However this will not fully solve your problem as while the keyboard arrow keys will work, you will not be able to use the mouse to select any of the items of the JMenu as trying to enter any of them will trigger the JMenu mouseExited event. You may need to attach a proper mouse event listener to the menu items as well.

How to use jbutton to make an jlabel to appear and disappear right away after releasing the click in JAVA

As the title says, I would like to know the syntax for that
I am making a first person boxing game when you click the button it punches the enemy but after releasing it, the image of the punch disappears right away.
the concept of the game is how many click the user can make in a certain period of time
I don't have a code to show because its blank inside the button
Start with this:
JButton btnPunch = new JButton("");
btnPunch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnPunch.setVisible(btnPunch.isVisible() ? false : true);
}
});
If you want an action to be done On Release use:
addMouseListener(new
MouseAdapter() {
public void mouseReleased(MouseEvent e)
{ // TODO: add your code here
} });

JMenu submenu mouse listener didn't listen; action listener did--why?

I just began using JMenu. To ease into it, I decided to use Netbeans form design tool, which has worked great for all components in this app.
Clicking a top-level menu item works great.
For one menu item, I made a submenu with 3 items, each with a mouse click listener.
Here's relevant code for one of the 3 submenus:
private JMenuItem mnuEditDicAddAllScratch;
mnuEditDicAddAllScratch = new JMenuItem();
private void mnuEditDicAddAllScratchMouseClicked(MouseEvent evt) {
new WordsToAdd(); // never happened
}
mnuEditDicAddAllScratch.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
mnuEditDicAddAllScratchMouseClicked(evt);
}
});
mnuEdit.add(mnuEditDicAddAllScratch);
It didn't work. Clicks ignored.
So I tried an Action listener:
private void mnuEditDicAddAllScratchActionPerformed(ActionEvent evt) {
new WordsToAdd(); // WORKED
}
mnuEditDicAddAllScratch.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mnuEditDicAddAllScratchActionPerformed(evt);
}
});
And it worked.
So a question is, "Why didn't mouse click listener listen?"
Also, "If I should stay away from mouse click events, why or under what circumstances?"
(And, pre-emptive strike: I should stay away from Netbeans form designer.)
You should use the best tool for the job at hand. This means that for JMenuItems and for JButtons, you should use ActionListeners, not MouseListeners (exceptions notwithstanding). For instance if you disable a button, you want the button to not work, right? This works with ActionListeners but not with MouseListeners.
For the best information on this type of stuff, go to the source: Swing Tutorials.
mnuEditDicAddAllScratch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
mnuEditDicAddAllScratchActionPerformed();
}
});

Where do you store the logic for a JPopupMenu?

I'm a little confused on where the action logic (what happens when a user selects a menu item from the popup) should be placed. Currently, I have it stored in a subclass of JPopupMenu, but it doesn't seem to call the event when I click on a menu item.
Subclass code:
public class MyPopupMenu extends JPopupMenu {
JMenuItem item1;
JMenuItem item2;
public MyPopupMenu() {
item1 = new JMenuItem("New Tab");
item2 = new JMenuItem("Close Tab");
add(item1);
add(item2);
}
class myListener extends MouseAdapter {
#Override
public void mouseClicked(MouseEvent ev) {
System.out.println("I've been clicked!");
}
}
}
I attached this to my JTabbedPane by calling the setComponentPopupMenumethod.
myTabPane.setComponentPopupMenu(myPopupMenu);
This compiles OK. And it does show the popup menu as expected, but upon selecting one of the menu options in the popup, all is silent. No message gets displayed. Do I need to put it somewhere else?
you have to add MouseListener to JMenuItems
add proper listener to use Swing Action, ActionListener for JMenuItems, read Oracle tutorial for working code example
Add MouseListner to your menu item
myListener myListener = new myListener();
item1.addMouseListener(myListener);
item2.addMouseListener(myListener);
Seems your approach is bit wrong. Please refer How to Use Menus

Why is a WindowBuilder event not getting called?

I created a Java app in WindowBuilder for Eclipse. I built a menu and on one of the menu items I added the mouseclicked event.
JMenuItem mitemAbout = new JMenuItem("About");
mitemAbout.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
JOptionPane.showInternalMessageDialog( form, "Message", "title", JOptionPane.PLAIN_MESSAGE);
}
});
mitemHelp.add(mitemAbout);
I put a breakpoint on the JOptionPane line and when I click on the menu item in debug mode it doesn't even get to it.
Am I completely missing a step here?
Although JMenuItem components offer the addMouseListener method (inherited from java.awt.Component) MouseEvents are only processed for the MenuElements own functional use, i.e. any external MouseEvents will have no effect.
For JMenuItem components, use an ActionListener rather than a MouseListener to listen for user events:
mitemAbout.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
...
}
});
or use:
mitemAbout.setAction(myAction);

Categories

Resources