KeyListener on JMenuBar - java

We often see in applications, a menu at the top (File, Edit, View...) composed of JMenuItems that can be reached by clicking on them or by typing the underlined rule of this JMenuItem.
I can't reproduce this last behavior whether I put my KeyListner on the JMenuBar or the JMenu. How to listen to the keyboard after clicking on a JMenu?
mnNewMenu_1.addKeyListener(new KeyAdapter() {
#Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_O)
{
mnNewMenu_7.setArmed(true);
}
}
});

Related

JButton doClick() performs the last clicked button

This is the block of code that I am using to check for key presses. If I press the 'esc' key then the JFrame closes. But, if I press the 'space' bar the listener performs the last JButton pressed, NOT the specific button I am telling it to click. The doClick() also does not run unless a JButton was previously clicked.
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent ke) {
if(ke.getKeyCode() == KeyEvent.VK_ESCAPE) {
SaveScripts.saveData(player);
dispose();
}
if(ke.getKeyCode() == KeyEvent.VK_SPACE) {
center.buttonMenuAttack.doClick();
}
}
});
Edit 1: Ok after some more testing, the issue seems to be that the listener breaks when anything in the frame is clicked.
Program Launches
Listener is active and working.
Any component of the frame is clicked, the listener breaks
Edit 2: I ended up going with camickr's solution, its much easier to set up and I haven't had any issues with using it.
InputMap events = getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap actions = getRootPane().getActionMap();
events.put(KeyStroke.getKeyStroke("SPACE"), "click");
actions.put("click", new AbstractAction() {
public void actionPerformed(ActionEvent event) {
center.bAttack.doClick();
}
});
events.put(KeyStroke.getKeyStroke("ESCAPE"), "click");
actions.put("click", new AbstractAction() {
public void actionPerformed(ActionEvent event) {
manage.bDataExit.doClick();
}
});
I figured out the issue. The Listener stopped working because when a button is clicked, it becomes the focused. I made a class that extends JButton so that all my buttons have the same behavior and added the following line of code to its constructors;
setRequestFocusEnabled(false);
This way, after a button is clicked the JFrame remains focused, allowing the Listener to behave as intended.

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();
}
});

Netbeans JPopupMenu Issues

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

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);

KeyListener not reacting

Im trying to use a KeyListener to do something when the button m is pressed, it needs to add an image to a JPanel, however it doesn't do anything :/
I use :
public void keyPressed(KeyEvent e) {
if(e.getKeyChar()==('m')){
panel.add(mario);
Thread marioS = new AePlayWave("sm64itsamemario.wav");
marioS.start();
}
}
});
Edit:
The answer is to set the focus to the panel before trying to invoke the listener :)
so I added a mouselistener that sets the focus to the panel on click :
panel.addMouseListener( new MouseListener(){
#Override
public void mouseClicked(MouseEvent e) {
panel.requestFocusInWindow();
});

Categories

Resources