How do I bind a function key say(F11 etc) to a JInternalFrame, so it can activate a particular action.
I tried with the following
this.getInputMap().put(KeyStroke.getKeyStroke("F11"), new AbstractAction() {
public void actionPerformed(ActionEvent e) {
System.out.println("Called");
}
});
but it never gets called?
Or please suggest how to activate a button inside a JInternalFrame using function key.
Thanks
Azlam
Well, focus is never on the internal frame itself, focus is on a component on the internal frame.
So you should probably be adding the binding by using
internalFrame.getRootPane()....
You may also need to use
"WHEN_ANCESTOR_OF_FOCUSED_COMPONENT"
input map.
The blog entry on Key Bindings explains this in more detail.
JInternalFrame has troubles with KeyListeners and KeyBindings. To solve this problem try using JDialog instead of JInternalFrame.
Related
I have written a calculator in NetBeans and it functions perfectly. However, I have to actually click the buttons to insert numbers and am trying to remedy that with a KeyListener. I have all my numbers and function buttons set inside a JPanel named buttons. I have my display label in a JPanel named display.
I set my class to implement KeyListener and it inserted the KeyPressed, -Typed, and -Released methods; however I stuck from there. I'm not sure how to make my buttons actually listen for the KeyPressed event, and when it hears the event - activate the button. Also, my buttons are named by their number (e.g. the Zero Button is named zero, One button is one, etc.).
I've read that you actually have to implement a KeyListener somewhere by using: something.addKeyListener(something);
but I cannot seem to figure this out.
Can I get some help here? I'm new to Java and this is my first solo project. And let me know if I didn't provide enough information.
EDIT: Most of my code is NetBeans Generated and I cannot edit the initialization of the components which seems to be my problem I think?
My class declaration:
public class Calculator extends javax.swing.JFrame implements KeyListener {
//Creates new form Calculator
public Calculator() {
initComponents();
}
One of my buttonPressed actions (all identical with changes for actual number):
private void zeroActionPerformed(java.awt.event.ActionEvent evt) {
if (display.getText().length() >= 16)
{
JOptionPane.showMessageDialog(null, "Cannot Handle > 16 digits");
return;
}
else if (display.getText().equals("0"))
{
return;
}
display.setText(display.getText().concat("0"));
Main method supplied by NetBeans:
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new Calculator().setVisible(true);
}
});
}
The initComponents() netbeans generated is absolutely massive (about 500 lines of code) and I cannot edit any of it. Let me know if I can supply any more helpful information.
Could there be an issue of Focus, and if so how can I resolve the issue?
Yes there is probably an issue with focus. That is why you should NOT be using a KeyListener.
Swing was designed to be used with Key Bindings. That is you create an Action that does what you want. Then this Action can be added to your JButton. It can also be bound to a KeyStroke. So you have nice reusable code.
Read the Swing tutorial on How to Use Key Bindings for more information. Key Bindings don't have the focus issue that you currently have.
I'm not sure I completely understand your question, and some code would help, but I'll take a crack, since it sounds like a problem I used to have a lot.
It sounds like the reason that your key presses aren't being recognized is that the focus is on one of the buttons. If you add keylisteners to the buttons, then you shouldn't have any problem.
In netbeans you can add keylisteners through the design screen really easily.
Here's a picture showing you how to add a keyPressed listener to a button in a jPanel.
private void jButton1KeyPressed(java.awt.event.KeyEvent evt) {
//Check which key is pressed
//do whatever you need to do with the keypressed information
}
It is nice to be able to write out the listeners yourself, but if you are just learning, then it is also nice to get as much help as possible.
This might not be the best solution, since you would have to add the listener for each of your buttons.
I'm making a calculator using Swing. So far I have created a GUI that consists of a JFrame with BorderLayout, and in its center I put a JPanel that has a JLabel (representing the screen of the calculator) and some JButtons (representing the keys).
I want the calculator to be able to receive input directly from the keyboard, so I included the method addKeyListener in a class that extends JFrame and I put as an argument a reference to an object of a class that implements KeyListener.
When I run the application it accepts keyboard input until I click on one of the JButtons. After that using the keyboard doesn't work anymore.
I suspect the problem is about focus, but it is not a problem that I could fix by clicking anywhere on the application. I added the following code:
setFocusable(true);
to the JFrame but it did not help. I have read that using Key Bindings maybe a better option than using a KeyListener, but I am not really sure about this approach.
Swing components are lightweight and use KeyBindings, where KeyListeners are for AWT components which are heavyweight. and known to have focus issues when mixed with Swing components. Thus I suggest changing to KeyBindings (but I see you have heard of them). You would use them something like:
final JButton b=..;
//method to add keybindings to a JComponent ie JButton,JPanel,JTextField etc
private void addKeyBindings(JComponent jc) {
//not the getInputMap(..) call it could also be JComponent.WHEN_FOCUSED etc
jc.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("D"), "D pressed");
jc.getActionMap().put("D pressed", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent ae) {
//do something when d is pressed
b.doClick();
}
});
}
For general knowledge a very hacky solution would be calling requestFocusInWindow() on the component to which the listeners are attached whenever focus is lost (like after button click etc)
It's another hacky approach, but you could alter the properties of the JButton when you create it so that it can't take the focus in the first place i.e:
myJbutton.setFocusable(false);
This worked for me.
I have a content pane containing a JScrollPane wrapped around a non-editable JTextArea that I'm using to (right now) simply display info about what key is being pressed (was just trying to get this KeyListener to work).
The KeyListener is on the JTextArea and everything works fine when the frame is selected. However, I'd like for the key presses to be registered even if another window is selected. Any way to do this?
Thanks.
I did that in the past but I do not remember exactly how. I think it was similar to that:
KeyEventDispatcher dispatcher = new KeyEventDispatcher()
{
public boolean dispatchKeyEvent(KeyEvent e)
{
System.out.println(e.getKeyChar());
return false;
}
};
DefaultKeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(dispatcher);
Just modify the SysOut with your custom code. If you want to prevent an event from being dispatched, the method should return true instead of false. I also found this question that may adress your problem.
Look into using key bindings if you need to listen for keys when a component that's doing the listening doesn't have focus. If on the other hand, you're trying to listen to keys when another application has focus, then your best bet is to not use Java for this, but rather to use a programming language that allows you to get closer to the OS such as C or C++.
This may be a stupid question, but I have to ask!
I have the following code snippets that are supposed to run their corresponding methods when the user interacts with objects.
For some reason, "foo" is never printed, but "bar" is.
myJSpinner1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
System.out.println("foo"); //"foo" is not printed
}
});
myJSpinner2.addChangeListener(new java.awt.event.ChangeListener() {
public void stateChanged(java.awt.event.ChangeEvent evt) {
System.out.println("bar"); //"bar" is printed
}
});
I get no exceptions or stack trace. What am I missing in the MouseListener one?
Thanks in advance.
EDIT: MouseEntered works perfectly on a JCheckBox implemented in exactly the same way!
JSpinner is a composite component consisting of a text field and 2 buttons. It's possible to add mouse listeners to all of those by iterating over the results of getComponents() and adding a listener to each.
However, in my experience, when something takes that much work, you're probably going about it the wrong way.
Why do you need the mouse-entered information for a JSpinner?
What do you want to do with this event?
Update:
If you're looking to supply information about all of the controls in your panel, you may want to look at using a glasspane to detect the component under the mouse.
A Well-behaved Glasspane by Alexander Potochkin is a good place to start.
This is a guess but I suspect you need to add a MouseListener to the JSpinner's editor (via a call to getEditor()). I imagine that the editor Component occupies all available space within the JSpinner and is therefore intercepting all MouseEvents.
This worked for me.
JSpinner spinner = new JSpinner();
((JSpinner.DefaultEditor)spinner.getEditor()).getTextField().addMouseListener(
new java.awt.event.MouseAdapter() {
public void mouseClicked(final MouseEvent e) {
// add code here
}
});
I needed this in order to evoke a popup key dialog for added usability due to our software requirements.
Aditional to #Rapier answer...
If you change the Spinner using something like
yourOldSpinner = new JSpinner(new SpinnerModel(...))
you will lost your previosly MouseListener...
If you need to change something of SpinnerModel, Don't create a new, change its parameters instead! (if you do it, you will need to reassign the MouseListener again, because it will be lost when you assign a new SpinnerModel).
an example (I'm talking...):
((SpinnerNumberModel)yourOldSpinner.getModel()).setValue(size/3);
((SpinnerNumberModel)yourOldSpinner.getModel()).setMinimum(0);
((SpinnerNumberModel)yourOldSpinner.getModel()).setMaximum(isize/2);
((SpinnerNumberModel)yourOldSpinner.getModel()).setStepSize(1);
I have a JPanel inside a JFrame. I have registered a KeyListener, based on which I want to update the JPanel. The problem I am having is that I cannot get the focus on the JPanel and therefore my KeyListener won't work. I already know that the KeyListener is functional because I registered it with the JFrame and it worked fine. My code goes something like this at the moment:
myFrame.setFocusable(false);
myPanel.setFocusable(true);
myPanel.addKeyListener(myKL);
myFrame.add(myPanel);
Has anyone encountered a problem like this before? Is there something I am missing in regards to this?
P.S.: I do not have any components inside the JPanel I just draw an Image on the background, so I need the focus to be on the JPanel itself and not on something inside it.
Although you're indicating that the panel can be focusable, the panel isn't asking for focus. Try using myPanel.requestFocus();.
Use setFocusable(true) and then requestFocusInWindow(). But the latter must be done after the window containing the panel is made visible, for which you will likely need to register a window listener and do the requestFocusInWindow() in the window activated handler code.
Note: Specifically after the window is visible, not just after calling setVisible(true).
I sometimes face a similar problem. I've noticed that in some cases it is better to make or request focus on a specific control within the panel that is within the frame (e.g., the input box to which you want keyboard input to go), rather than request focus for the pane itself.
Try
panel.setFocusable(true);
panel.setRequestFocusEnabled(true);
// some code here
panel.grabFocus();
Try something like this:
myFrame.addFocusListener(new FocusAdapter() {
/**
* {#inheritDoc}
*/
#Override
public void focusGained(FocusEvent aE) {
myPanel.requestFocusInWindow();
}
});