I have created java swing application. In that application, there is a textfield.I have attached key listner for that text field also. Using that, enter key is identified and popup another JPanel. There is a JTable which is loaded data from DB and there is a JButton. The selected row from that table is loaded to that previous textfield.
There is a very rare issue with that implementation. when we have that JPanel, mistakenly the user pressed the enter button twice in very very short period at once. Then after we load that panel ther is a unusal behavior. by default the JButton is pressed without any key pressed.
According to my investigation, the problem can be solved, if we have a method to cler keylistner actions once it finished.
Are ther any methods?
Related
Hy everyone,
I was just learning some new things with Java GUI, and I got stuck with a problem. This is what it looks like:
I want a button that adds the data from the second JPanel in to the first one.But not only once. I want it to go to the first JPanel, everytime it is pressed.
So, the button should create a new JTextField in the first JPanel, below their respective categories "name" and "age" everytime it is pressed. It means that I have to modify the "Y" field everytime, so all the new JTextFields created by the button "add data" dont get pilled up.
I don't know how to make the button "add data" Works, the other things, I know how to do. I know how to make the button creates a JTextField, with the data I want to store, only once (using getText() and setText() ), but not how to create new JTetFields with diferente "y" field everytime it is pressed.
Thank you for the help.
When you click on the button to add people you should not display a new JPanel. Instead you should display a modal JDialog. A JDialog is just like a frame but generally is only displayed for a specific function and then closed.
On this dialog you will probably want two buttons: 1) Save Data and 2) Close. This will allow you to enter information for multiple people before closing the dialog.
To display the "person" information you should probably be using a JTable. A table allows you to display data in a row/column format. See the section from the Swing tutorial on How to Use Tables for basic information to get you stated. Note, in this cause you will want to use a DefaultTableModel for your table and then you can just use the addRow(...) method to save the "person" information.
When you create the class for the dialog to get the "person" information you will want to pass the DefaultTableModel to this class as a parameter. Then when you click the "Save Data" button you get the information from the two text fields and update the TableModel. The table will be updated automatically.
So your first step is to create the main frame with the JTable with the two columns. Then you create a simple "Add Person" button. This button will then add hard coded data to the table each time it is clicked by using the addRow(...) method. Once you get this working then you change the logic of the button to display the dialog and then you can enter the "person" information on the dialog and save it.
I am making an accounting software using Netbeans and MySQL.
I need to reload the page on a button click.
As in, the page takes entry for a product. When Add button is clicked, the entry should go into the database and the JFrame that takes the entry should be reloaded to take next entry.
The entry gets entered into the database but the reloading fails and the project needs to be rerun in order to upload next entry.
The entry system takes an input from a combo box and 10 other text boxes. On clicking the add button, the entries get added to the databas and all the fields are reset. Indeed. But the problem is, now the fields arent editable anymore.
revalidate() and repaint() aren't working.
What other options are there ?
Clear the JTextField text elements after clicking the button and change the selected indexes for the JComboBox elements. For instance, in your button's ActionListener:
tfTextField.setText("");
cbComboBox.setSelectedIndex(-1);
this.dispose();
JFrame f = new Add();
f.setVisible(true);
This did it.
The form gets disposed and then it loads again.
I have an application in which there is a textbox and a button. the textbox has focusListener(for incorrect values) and the button has actionListener(for saving the value into a file).
An error message window pops up when a wrong value is entered in the textfield as soon as it loses focus. Now I have 2 senarios.
when the focus is lost from tab out and if a wrong value is entered in the field then the pop up window shows up correctly.
But when i enter a wrong value in the field and without tab out I click the button then the actionListener is activated before the focusListener(Focus Lost) and it saves the wrong value in the text file and then shows the error message.
How should I stop the incorrect value to be saved into file by running the focuslistener first?
ActionListener running before focus listener
Focus, FocusListener, Focus Subsystem is asyncronous, you can sheduling those events, but can caused another side effects, I'd suggest don't do that
delay required events in EDT by using invokeLater,
I have a JFrame which contains most everything in the application. It has a KeyListener attached, and it also has several buttons and textfield on it. The problem it, when a button is clicked or a textfield is selected, it gets focus and shortcuts don't work. Of course, one can tab out of them, but to do this you must tab through EVERYTHING (each button, each textfield) before giving the window focus again.
Is there a sensible way to only require one tab to return focus to the frame from the textfield, and no tabs to return focus to the frame from a button click?
A WindowListener doesn't seem like the best way to do this, but if it's the only way I suppose I can forge forward there.
Thanks in advance!
It has a KeyboardListener attached
I have never heard of the KeyboardListener class so I can only guess what you are trying to do.
My guess is that you should NOT be using a listener of any kind.
Instead you should be using Key Bindings.
If you only need this for the textfield, you can add a keyListener to the textfield and when the user presses tab use yourJFrame.requestFocus(). Otherwise refer you may want to use a window manager or a key map.
I have a Java app that I'll call App. App will occasionally display a JFrame that we'll call myFrame. App will also display a JTextArea that is contained in either a JDialog or a JFrame (I'm not sure which, but I can find out if that's necessary to answer this question). Let's call this JTextArea "myTextArea".
Now, the following sequence of events happens:
We display myFrame. It has the focus and you can give it input.
We call myFrame.setVisible(false)
We display myTextArea.
We call myTextArea.requestFocus().
myTextArea has the focus (the cursor is blinking with in it), but all the keystrokes that are input are sent to myFrame!
Note that myTextArea is not contained in myFrame.
What is going on here? Has anyone heard of a non-visible JFrame receiving keystrokes? Not only receiving keystrokes but stealing them from some other component that has the focus?
I found what’s basically causing the problem. MyFrame has a class MyKeyEventHandler that implements KeyEventDispatcher. The method dispatchKeyEvent(KeyEvent e) is always returning false even for key strokes that are intended for myTextArea. Therefore the key strokes do not reach myTextArea.
It is not about toggling the visibility. The JFrame is initialized first and still has focus. You are only making it invisible, not taking away the focus from it.
Moreover, your JTextBox needs to have a parent container. Possibly
myFrame.add(myTextArea);
should work. To shift the focus to the JTextArea, use :
myTextArea.requestFocus();