JGoodies Binding bug question about bufferedModels and keyboard input - java

I am using JGoodies Binding on a JTextField like so:
trigger = new Trigger();
PresentationModel<SpectralControlsModel> adapter = new PresentationModel<SpectralControlsModel>(model, trigger);
ValueModel valueModelStartingSampleJTextField = adapter.getBufferedModel("startingSample");
startingSampleJTextField = BasicComponentFactory.createLongField(valueModelStartingSampleJTextField);
setupValueModelListener(valueModelStartingSampleJTextField, startingSampleJTextField);
I have a keyListener on the JTextField that commits data to the model when the "enter" key is pushed.
The problem is when I enter in a new number in the JTextField and hit enter, the model gets the old value, even though I call trigger.triggerCommit(). If I enter a new number and then click something else, lose focus on the JTextField, and then gain focus again, then the new value ends up in the model as expected.
Let me know if I described the problem clearly enough, it is very strange behaviour and I need to get to the bottom of it, thanks.
UPDATE
I went to this Java2s.com JGoodies Example and added this code:
firstNameTextField.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
trigger.triggerCommit();
}
});
When I hit the enter button with degbugging on, the actionListener fires just like it would if I hit Commit Buffer Button, but nothing is committed when I display the values. I am very puzzled by this. There has to be a way to get JGoodies working with keyboard input. I shouldn't have to click out of the JTextField for the text to get to the Value Model.

I have a keyListener on the JTextField that commits data to the model when the "enter" key is pushed.
Probably not related to your problem, but you should be using an ActionListener to handle the Enter key, not a KeyListener.

Sorry about this question, I didn't explain it very clearly. Here is the answer though:
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 :)

Related

How to validate jTextfield within keypress

im trying to validate a jtextfield from the keypress and would like it to throw an error message in a jLabel. I am trying to do this with in the code below.
private void jTextField2KeyPressed(java.awt.event.KeyEvent evt) {
}
Can't comment, so i'll answer instead. If you are looking to validate the input of the user as he/she type, you might wanna have a look at javax.swing.text.DocumentFilter.
If you are simply looking to update a JLabel when the user types something, you should add a KeyboardListener to the JTextField. You have to be a bit careful with that tho, as swing doesn't guarantee the order in which listeners are called, meaning that the text may not have updated when the KeyboardListener is invoked.

Unable to set focus for keyboard input using WindowBuilder SWING UI

I've written a basic calculator type program using WindowsBuilder in Eclise neon and Java 1.8. It's pretty much complete, with everything working how I want it to. Except keyboard entry.
As a finishing touch I'd like to detect keyTyped events and map them to button presses so users can use the keyboard for entry instead of clicking buttons with the mouse.
I've added 'implements KeyListener' to the program class...
public class CashRegister implements KeyListener {
private JTextField keyb;
I've tried to set a listener to a invisible JTextField called keyb....
private void initialize() {
keyb = new JTextField(20);
keyb.addKeyListener(this);
keyb.setFocusable(true);
keyb.requestFocusInWindow();
And I've added methods to handle the captured keypress...
public void keyTyped (KeyEvent e) {
String out = "";
out = out + e.getKeyChar();
pence1text.setText(out);
}
public void keyPressed (KeyEvent e) {
}
public void keyReleased (KeyEvent e) {
}
So, at this stage all I'm expecting, prove it is working, is the keycharacter I press to appear in the textfield called 'pence1text'. However, it doesn't work, when I press the a key nothing is displayed.
I think it could be a focus problem. Googling around and checking stackoverlow lead me to add the following lines...
keyb.setFocusable(true);
keyb.requestFocusInWindow();
as above, but still no luck.
Does anyone have any ideas what I am doing wrong, or what I can try next?
Thanks
Thanks to user Andrew Thompson for pointing me back to the docs and a re-read.
The problem is that the JTextField is not visable and thus can't be given focus. If I add the listener to a textfield that is visable then the program works correctly. However if the user uses the mouse to click a button it loses focus and breaks the implementation...so I need to rethink the code and keep looking at focuse settings.
As a finishing touch I'd like to detect keyTyped events and map them to button presses so users can use the keyboard for entry instead of clicking buttons with the mouse.
Don't use a KeyListener.
Instead you should be using Key Bindings. Read the section from the Swing tutorial on How to Use Key Bindings for basic information.
Also check out: how to put actionlistenerand actioncommand to multiple jbuttons for a working example that shows how you can apply the key bindings to a calculator.

Prevent Java Swing JTable losing focus when invalid data entered

We currently have a focus problem with a JTable/JTextEditor in java swing. The JTable has a custom cell editor which is a JTextField.
The issue is when a cell is being edited and contains invalid data, and the user clicks on a JButton, the text field will stop editing and the JButton actionPerformed (clicked) is called. The JTable#setValueAt handles validation so if the data in the JTextField is invalid, the underlying TableModel is not updated.
Ideally, we do not want to let the JButton click occur. Focus should remain with the JTable or the JTextField.
Clicking the button will perform a submit action and close the frame the table is in. As the validation in the TableModel#setValueAt does not update the value, it submits the old value.
Can this be done? I am still fairly new to Swing so I am not aware what to check.
Unfortunately, our code is not straight forward. The UI is constructed from XML in such a way that the button knows nothing about anything else on a form (this is code I have inherited).
In .net you could stop a control losing focus by handling a Validating event and setting a cancel flag. Is there a similar mechanism with Java.
Validating the input after editing has concluded, in setValueAt(), may be inconveniently late. The editor itself can preclude navigation for invalid values, as shown in this example that links to the corresponding tutorial section.
For valid values, you can make the table commit when losing focus:
table.putClientProperty("terminateEditOnFocusLost", true);
Can you try using inputverifier on the editor component, i.e. text field?
When the focus is lost from a component, the lost focus method is called (more reference in http://docs.oracle.com/javase/tutorial/uiswing/events/focuslistener.html). Therefore, you may call the validation method when you lose the focus.
If you do not need to be aware of the specific field being edited, you can also perform validation inside your button and prevent the submission if it is not sucessful.
I'd achieved a similar functionality by overriding the stopCellEditing method in my JTable's CellEditor.
#Override
public boolean stopCellEditing() {
String s = (String) getCellEditorValue();
if (s != null) {
if (!testYourValue()) {
Toolkit.getDefaultToolkit().beep();
return false;
}
}
return super.stopCellEditing();
}

Let ActionListener listen for change in JTextField instead of only enter?

So as you may know, if you have a text field and you add an ActionListener to it, it will only listen to the keypress of the enter button. However, I want to let my ActionListener listen to changes in text of the . So basically I've got this:
public static JPanel mainPanel() {
JPanel mainp = new JPanel();
JTextArea areap = new JTextArea("Some text in the textarea");
JTextField fieldp = new JTextField("Edit this");
areap.setEditable(false);
fieldp.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(//change in textfield, for instance a letterpress or space bar)
{
//Do this
}
}
});
mainp.add(areap);
mainp.add(fieldp);
return mainp;
}
Any way I can listen to changes in text (like documented in the actionPerformed event)?
From an answer by #JRL
Use the underlying document:
myTextField.getDocument().addDocumentListener();
Yeah, but what is a document listener and how do you use it? You're not really answering the question.
I have a JTextField in my app's user interface. When the user makes any change to it, I want a nearby JCheckBox to be checked. The purpose is to tell the app to USE the value that was entered. Users often enter a value there but if they don't explicitly tell the app to use it then the app continues to ignore it. Instead of "training" users I'm supposed to follow the principle of least astonishment and automatically check the "Use this value" box.
But how do I listen for a change? Can't you guys just tell me the easy way, instead of "educating me" about document listeners?
Documents are the mechanisms java swing uses to store the text inside of a JTextField. DocumentListeners are objects that implement the DocumentListener interface and thus make it possible for you to list to changes in the document, i.e. changes in the text of the JTextField.
To use the document and documentlistener capabilities, as suggested above extend your class (probably but not necessarily a JFrame) so that it implements the DocumentListener interface. Implement all the methods for the interface (most likely your java ide can do that semi-automatically for you. FYI, the DocumentListener interface has three methods, one for inserting characters (into the text field), one for removing characters, and one for changing attributes. You are going to want to implement the first two as they are called when characters are added (the first one) or deleted (the second one). To get the changed text, you can either ask the document for the text, or more simply call myTextField.getText().
C'est tout!
Phil Troy

2 input fields displaying the same thing

Bit of a strange one.
I want to have a JTextField, in which the user will type a string. While typing, however, I'd like that text to automatically print to another JTextField in real time.
I'm not sure if this is possible because I can't recall seeing any application do it.
Anyone even seen anything like this before?
Actually, now that I open my eyes a bit, I see that stackoverflow does it.
Are there any known ways of implementing in Java?
You might be able to give the fields the same document instance. For the document you could use one of the classes that swing provides or you could extend your own. The document is the model of the text field.
Alternatively you could use listeners to do the updating. There are many things you can listen and it depends on your needs what suits best. You can listen the document, you can listen keyboard and mouse events, you can listen for action events. (Action events happen in this kind of fields when pressing enter or focus is lost.)
The "same document" approach is the way to go.
Here's some sample code in Groovy (translation to Java is left as an exercise to the reader):
import javax.swing.*
import java.awt.FlowLayout
f = new JFrame("foo")
t1 = new JTextField(10)
t2 = new JTextField(10)
t2.document = t1.document
f.contentPane.layout=new FlowLayout()
f.contentPane.add(t1)
f.contentPane.add(t2)
f.pack()
f.show()
Add an ActionListener, as this will respond for any action changing the text (not just key presses, but also mouse-driven cut-paste). Code not tested...
// changing textField1 updates textField2
textField1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
textField2.setText(textField1.getText());
}
});
You could add an action listener for the jTextField's key released action.
eg:
jTextField1.addKeyListener(new java.awt.event.KeyAdapter()
{
public void keyReleased(java.awt.event.KeyEvent evt)
{
jTextField1KeyReleased(evt);
}
});
private void jTextField1KeyReleased(java.awt.event.KeyEvent evt)
{
jTextField2.setText(jTextField1.getText());
}
You could use the KeyListener interface, and on each keyTyped event you copy the text to the "duplicate" field.

Categories