Please do note that this is a different question,
I am writing a java program. I have a form with 10 JTextFields and a 'Submit' button.
How do I make the method for 'Submit' button to be called when the user presses the
enter key on ANY of the 10 text fields?
Should I add KeyListeners to all of the 10 or is there a more efficient way since the text fields and the button are inside a JPanel?
No, Create an common event handler like this ,And attach it to all
Below is a Mock code:
KeyAdapter event= new KeyAdapter() {
public void keyReleased(KeyEvent e) {
//do something
}
public void keyTyped(KeyEvent e) {
// TODO: Do something for the keyTyped event
}
public void keyPressed(KeyEvent e) {
// TODO: Do something for the keyPressed event
}
});
txtField1.addKeyListener(event);
txtField2.addKeyListener(event);
-----
may be a loop also :)
Related
How can I create Enter event when I want to click on button, without using class Robot? I have already created a form and I create button btnMainOk.
I've tried to use keyListener but it's not working.
btnMainOk.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
btnMainOk.addKeyListener(new KeyAdapter() {
#Override
public void keyReleased(KeyEvent e) {
super.keyReleased(e);
if (e.getKeyCode()==KeyEvent.VK_ENTER){
System.out.println("Hello");
}
}
});
}
});
The goal is when I click with my mouse device on button, that button should act like I pressed enter key on my keyboard.
currently im trying to code a grid based game.
ive already managed to implement a key listener for general navigation.
but when pressing a certain key, a Jpopup Menu opens.
now i want to implement navigation for the menu aswell with a key listener. Pressing B is supposed to close the menu again.
KeyListener UnitActionMenuKeyListener = new KeyListener() {
#Override
public void keyPressed(KeyEvent evt)
{
if (evt.getKeyCode()==KeyEvent.VK_B)
menu.setVisible(false)
}
#Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
JPopupMenu menu = new JPopupMenu("UnitActionMenu");
JMenuItem bewegenItem = menu.add("test");
Using menu.show and menu.addKeyListener after this.
But no matter what i try, either my general navigation wont work anymore, or the general navigation will work, but my menu wont react to pressing B
so, how do i implement multiple key listeners for multiple elements?
i just want my main window to listen (and react) to some key differently than my menu
(sorry for bad writing, my english isnt that good and im frustrated by failing a simple task for more than 4 hours)
Swing key bindings are generally better than key listeners. Key listeners have issues related to the focus system. That's what the problem you're having sounds like. When the popup is active, it steals the focus. Only the focused component will send out key events.
There's a tutorial for key bindings here: https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html
The API is a little more complicated but they're more robust and you can specify the focus behavior directly.
Here's a quick guide for converting key listeners to key bindings.
For a key listener, you have some code that's like this:
myComponent.addKeyListener(new KeyListener() {
...
#Override
public void keyTyped(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_Z)
performZAction();
}
});
Writing a key binding would be like this:
KeyStroke keystroke =
KeyStroke.getKeyStroke("typed Z");
myComponent.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
.put(keystroke, "zAction");
myComponent.getActionMap()
.put("zAction", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
performZAction();
}
});
In other words, there are three steps:
Creating a javax.swing.KeyStroke object with KeyStroke.getKeyStroke.
Using the input map, bind the keystroke to a name for your action.
Using the action map, bind the name for your action to a javax.swing.Action.
The Action is your listener. You can extend javax.swing.AbstractAction and it's just like writing an ActionListener.
Im trying to get my KeyEvent working. Sadly the keyTyped(KeyEvent e) isnt responding at all. :)
I implemented the KeyEvnet to my class.
I assigned the listener like following:
JTextfield searchBar = new JTextField();
searchBar.addKeyListener( this );
My key event looks like this:
#Override
public void keyTyped(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
System.out.println("pressed");
try {
int browser = getSelectBrowser().getSelectedIndex();
logic.search( searchBar.getText(), searchInfo, browser, isURL );
} catch (InterruptedException ie) {
System.out.println( "Pressed fail" );
}
this.repaint();
}
}
I also tried the Listener in a second test Gui and were it also does not work.
:-)
use "keyPressed" instead of "keyTyped". keyTyped listens only on alphanumeric chars
You wont see this listener called when you press enter, so your println will never be called. Pressing enter on a JTextField will call the attached ActionListener, if there is one.
To get this working, add an ActionListener to the text field and put the code you want to run when enter is pressed in there.
You can read more about it here. Thanks to tak3shi for linking that in the comments.
hello is there a way mouse even that can Hold the mouse and release cause I can't find it on google.
so for example this image..
When the jTextBox is **** when he click the button, he see the words oops...
then after he release the click of mouse the jTextBox will back to **** again
I know this code already but the mouseevent is I only don't know
Yes. You will want to implement the MouseListener interface with a new class and add this new Listener against your button with the following;
button.addMouseListener(new YourMouseListener());
An example custom MouseListener might look like this.
class YourMouseListener implements MouseListener {
#Override
public void mouseClicked(MouseEvent e) {
}
#Override
public void mousePressed(MouseEvent e) {
// Insert code to show password
}
#Override
public void mouseReleased(MouseEvent e) {
// Insert code to hide password again
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
}
I hope this helps.
You'll need a Robot object. This can do things as follows:
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
The Mousebutton is pressed until you do this:
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
This should do what you want.
I am trying to listen tab-in tab-out action for my swing gui that is made by JFrame. I have a JTextField added to the JFrame that will be getting the user clipboard whenever the window is selected so the user may tab between programs, copy some url so when back to my program, this JTextField will be populated by the copied url string.
EDIT:
I have tried this:
frame.addFocusListener(
new FocusListener() {
public void focusGained(FocusEvent e) {
url= getClipboardData();
}
#Override
public void focusLost(FocusEvent arg0) {
// TODO Auto-generated method stub
}
}
);
it doesnt work
A frame doesn't recieve a focus event. A component on the frame gets the focus event.
If you want to know when a frame gets focus then use a WindowListener and handle the windowActivated event.
What you want is a FocusListener not an ActionListener. Check out the java Doc and you'll know how to use it. It's easy.
it looks like you are not setting the clipboard data onto the text field.
frame.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
getJTextField().setText(getClipboardData());
}
public void focusLost(FocusEvent e) {
//ignored
}
});
Something like that will likely solve your problem