I'm making a simple calculator, so far I did a text field where I can type numbers and it listens if key was typed from keyboard.
private void resultKeyTyped(java.awt.event.KeyEvent evt) { }
What I want is to click on let's say '1' with mouse and send a key event to this method, so it would be like I clicked it on keyboard. Tried doing keypress with robot but it says 'void type is no good here' or something like that. I wanted to just run that resultKeyTyped method from withing mouse click listener, like this:
private void jButton1MouseClicked(java.awt.event.MouseEvent evt)
{
resultKeyTyped(KeyEvent.VK_1);
}
To call the resultKeyTyped, you have to pass a KeyEvent. You can just create a KeyEvent using appropriate constructor:
KeyEvent event = new KeyEvent(textField, 0, System.currentTimeMillis(), 0, KeyEvent.VK_1);
resultKeyTypes(event);
Although from your description (eg simple calculator), it sounds like you may wish to choose a different approach:
Add an ActionListener to the JButton
Within the implementation of the ActionListener, change the JTextField text by using the setText method
The best way to do this (assuming that you are clicking on a Button instead of something else) in my opinion would be this:
Button button1 = new Button("1");
button1.addActionListener(new ActionListener() {
int thisKey = KeyEvent.VK_1;
#Override
public void actionPerformed(ActionEvent e) {
resultKeyTyped(thisKey);
}
});
Now, the one thing that you need to change is that resultKeyTyped needs to take an int as a parameter instead of a keyevent. From what I understand, all that you care about is which key was pressed, not how long it was pressed or anything like that. So, wherever you call resultKeyTyped, pass it KeyEvent.getKey()
Hopefully this helped!
P.S. if you really want a keyevent, you can use the keyevent constructor, but since you were using a robot anyways, I am pretty sure that you only care about the key
Related
I am working with swings. This is the code I used to make a frame for logout which would return a int value and using that value other operation could be performed in the previous function. But I am getting an error. How should I solve it?
public int logout()
{
int s=0;
JFrame main=new JFrame("Logout");
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
main.setSize(d.width/2,d.height/3);
main.setLayout(new FlowLayout());
JButton yes=new JButton("Yes");
main.add(yes); main.setVisible(true);
yes.addActionListener(new ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent e)
{
s=1;
main.setVisible(false);
}
});
return s;
}
What do you expect your program to do?
The code you wrote, tries to do:
Create a "logout" frame with a "yes" button.
Return from the logout() method with the value of the s variable.
When later the user presses the "yes" button, you prepared an action listener to then set the s variable to 1 - but that can't work, as you already returned from the logout() method, and the variable s no longer even exists.
So, be happy that the compiler told you about a problem.
I suppose you want to wait for the user's logout approvement. Then use JOptionPane methods (the Java tutorials will help you how to do that correctly).
And by the way: get into the habit of formatting and indenting your code properly. It wasn't fun reading your code.
Use the JOptionPane class to create a confirm dialog by using one of the showConfirmDialog() method to show a confirm dialog. Please read the Java tutorial How to Make Dialogs on how to create such dialogs. The source code should look something like this:
int choice = JOptionPane.showConfirmDialog(your_main_frame,
"Do you want to logout?",
"Logout?",
JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION) {
// "Yes" clicked
} else {
// "No" clicked
}
I'm building a Gui using net beans (Java) and my question is how to get the string in the text field to the variable without pressing the Enter key?
I wrote this code:
private void idTextBoxActionPerformed(java.awt.event.ActionEvent evt) {
this.Id = evt.getActionCommand();
}
The problem is that if I just enter the text and move to the next text field, the data doesn't go into the Id variable, if I press Enter everything OK.
Pressing Enter invokes an ActionEvent from that textfield which you listen for in your actionPerformed method and that is why your code only works in that scenario.
You could use a FocusListener to acheive what you want. You will want to listen for the focusLost event, which is when you move away from the textfield.
class foo implements FocusListener {
JTextField textField = new JTextField("A TextField");
textField.addFocusListener(this);
public void focusGained(FocusEvent e) {
// Do whatever you want
}
public void focusLost(FocusEvent e) {
// Save the text in the field to your id variable
}
}
EDIT
The following tutorial shows how to use a formatted textfield. You can ignore the formatting bit and focus on the propertyChangeListner aspect of it.
The idea is the same as my first example but using a different type of listener.
You could use the action event or focus event to track the changes at component level. But if its the text changes that you are interested in, then you should consider using a DocumentListener. Read the tutorial here
im sitting on this for 4 hours now, and once again I end up on Stackoverflow because I just cant solve this (simple) problem.
I want to fire a method when I click a button, Google gives an Example like this:
// Listen for mouse events on the Add button.
addStockButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
addStock();
}
});
But this creates a new Instance(?..How can they even create an instance of Clickhandler, since its an Interface) everytime the button is clicked. How can I solve this that all buttons share a Clickhandler and the Handler askes the Button which button he is, so he can fire the method attached to that button.
Any Ideas? If you this is to vage information and you require more code please let me know.
Thanks in advance,
Daniel
Java creates a new instance of an anonymous class that implements ClickHandler. Which it can do because you provide an implementation for the onClick function specified by the interface.
This class is however not created when you click on the button but at the moment you call addClickhandler. If you need the handler for multiple events do something like:
ClickHandler handler = new ClickHandler() {
public void onClick(ClickEvent event) {
addStock();
}
};
addStockButton.addClickHandler(handler);
someOtherButton.addClickHandler(handler);
Within the handler you can identify from where the event is coming using event.getSource().
If you have access to your button variables you could simply check the pointer
if (addStockButton == event.getSource()) ...
Or you can cast the result of getSource to the appropriate type and access the properties/methods of the object.
Eelke has already answered your question. I just add that if you would use GWT's UiBinder feature, you could achieve what you want like this:
#UiField
Button addStockButton;
#UiField
Button removeStockButton;
#UiHandler({ "addStockButton", "removeStockButton" })
void handleClickEvents(ClickEvent event)
{
if (event.getSource() == addStockButton)
{
addStock();
}
else if (event.getSource() == removeStockButton)
{
removeStock();
}
}
Its an anonymous instance of the interface, this is like declaring a new class that implements that interface.
I would have to ask why you would want to do this, you would need to make the ClickHandler contain a reference to its parent. You would also need to make the buttons identifiable so you can select the right one in the body of the ClickHandler. Is your need to only have a single instance really that bad that you can't have multiple anonymous instances ?
I have this code:
this.trigger = new Trigger();
this.presentationModel = new PresentationModel(this.personBean, this.trigger);
final ValueModel firstNameAdapter = presentationModel.getBufferedModel("firstName");
final JTextField firstNameTextField = BasicComponentFactory.createTextField(firstNameAdapter);
and
firstNameTextField.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
trigger.triggerCommit();
}
});
So when I push the enter button on the JTextField, I expect the value in my ValueModel class to be the same as the value in my JTextField. This doesn't happen unless I click outside the JTextField, then back inside the JTextField, and then push enter. If I just type in the text and hit enter, the ValueModel does not get the updated value. I am stuck on this problem, can anybody help?
BTW, I used this link for figuring out JGoodies in the first place: JGoodies Tutorial
I hope I am understanding your question correctly.
You need to get the text in the text field and set it in the ValueModel.
firstNameTextField.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
//this get the text from the text field
String firstName = firstNameTextField.getText();
//now write your code to set the firstname into the ValueModel
trigger.triggerCommit();
}
});
I looked through the JGoodies API (should have done this sooner) and found an unexpected static call, Bindings.commitImmediately()
If I call this method before my call to trigger.triggerCommit(), everything works as expected :)
Create a text field that commits on each key typed instead of when focus is lost:
BasicComponentFactory.createTextField(firstNameAdapter, false);
Also, you should consider architecting your program to not use buffered models. I find that they make things more complicated and tricky, and think I saw Karsten Lentzsch recommending not to use them as well in a mailing list.
The most useful way for me to learn JGoodies was to look at the tutorial code for the JGoodies binding and validation libraries.
I'm writing a Java Swing application using the Metal look-and-feel. Every time there is a JButton in my application the user uses the Tab key to move the focus to the button and then hits the Enter key. Nothing happens! If he hits the Space key the button events are fired. How do I assign the Enter key to trigger the same events as the Space key? Thank you for your help.
I found the following:
http://tips4java.wordpress.com/2008/10/25/enter-key-and-button/
Where Rob Camick writes that when using JDK5 and later you simply add...
UIManager.put("Button.defaultButtonFollowsFocus", Boolean.TRUE);
...to the application to solve the problem. This did the trick for me! And I can't imagine anything simpler. However, when using older versions of Java you will have to do something like Richard and Peter describe in their answers to this question.
Here is complete example. Richard was close, but you also need to map pressed ENTER to action, not just released. To make it work for ALL buttons, I have put this mapping to default input map for buttons. Add imports, and it should be runnable.
public class Main implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Main());
}
#Override
public void run() {
setupEnterActionForAllButtons();
JFrame frame = new JFrame("Button test");
frame.getContentPane().add(createButton(), BorderLayout.NORTH);
frame.getContentPane().add(createButton(), BorderLayout.SOUTH);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private void setupEnterActionForAllButtons() {
InputMap im = (InputMap) UIManager.getDefaults().get("Button.focusInputMap");
Object pressedAction = im.get(KeyStroke.getKeyStroke("pressed SPACE"));
Object releasedAction = im.get(KeyStroke.getKeyStroke("released SPACE"));
im.put(KeyStroke.getKeyStroke("pressed ENTER"), pressedAction);
im.put(KeyStroke.getKeyStroke("released ENTER"), releasedAction);
}
private JButton createButton() {
JButton b = new JButton("press enter");
b.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Pressed");
}
});
return b;
}
}
Actually, this is a look and feel issue. It is (and should be) up to the look and feel as to which key triggers the focused button.
The "default button" work-around works since the L&F you're using uses enter for the default button.
Peter's workaround explicitly changes the L&F default "focus action" key - which is somewhat more convincing if you ask me.
I would add that I don't think many users would want to tab to the button then hit enter (most won't even notice the focus indicator) - they want the default action to be the "right" one and work wherever they press enter. This can only be done with input maps as Richard suggests.
I would certainly suggest getting a very clear picture of what your users actually want and expect (preferably with reference to other apps they use) before changing anything globally.
You do it by assigning an input / action map for the Enter key. Something like the following:
// save the command mapping for space
Object spaceMap = button.getInputMap.get(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true));
// add a mapping from enter to the same command.
button.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true),spaceMap);
You can also set the "default button" to the button most recently focussed.
I did this on an application, and all methods of doing this are a nightmare to maintain and debug. The fact is, this is clearly not what the designers of Swing intended to happen.
Extension to above answers to do same with radio , checkboxes.
Called this before creating components
void setupEnterAction(String componentName){
String keyName = componentName + ".focusInputMap";
InputMap im = (InputMap) UIManager.getDefaults().get(keyName);
Object pressedAction = im.get(KeyStroke.getKeyStroke("pressed SPACE"));
Object releasedAction = im.get(KeyStroke.getKeyStroke("released SPACE"));
im.put(KeyStroke.getKeyStroke("pressed ENTER"), pressedAction);
im.put(KeyStroke.getKeyStroke("released ENTER"), releasedAction);
}
public void setEnterEvent(){
setupEnterAction("Button");
setupEnterAction("RadioButton");
setupEnterAction("CheckBox");
}