How to refresh a JPanel after user inputs something? - java

Say a game allows you to choose a number of players, and then asks for each player's name based on that number.
The first user inputs their name in a JTextField, and clicks a JButton (which stores their name in a list).
After the button is clicked, the JPanel erases the user's name from the JTextField so that the second person may enter their name, and so on.
How would I do this? Would I use a loop?

How would I do this? Would I use a loop?
Nope. Using a loop is a construct that you'd use in a linear console-driven programming environment to get repeated input from the user. Instead Swing is an event-driven library, and in this situation you have to think differently since here you'd use state-dependent interaction with the user.
So say you wanted to get 5 records of information from the user, you'd use a counter, and each time the submit button is pressed, you'd get the input from the GUI components, create an object with them add them to whatever collection is holding the information, perhaps an ArrayList or a JTable's model, and then when the maximum number is reached by the counter (the state variable that you'd be monitoring), you'd stop getting input, and perhaps even change the GUI view entirely to reflect this change in state, something that you could use a CardLayout to help with.

Related

how do I re-use an action listener multiple times on a JTextField in java?

My program is supposed to get information about a person (first and last names, address, phone) so that it can add the person into an address book.
I made a JLabel which gives instructions on what to enter right below. Below the JLabel is a JTextField which has an ActionListener listening to what is being entered. My method has about 8 ActionListener's with 8 actionPerformed methods. I am running into trouble it is not working. I can't figure out any other way.
Best to create a form, perhaps with GridBagLayout or MigLayout that holds displays JLabel/JTextField pairs, so that the user can enter all the data on a single simple form, much like most software you use.
If you absolutely must use a single JTextField, then you should use a single ActionListener, but change how it responds based on the state of the GUI. That is, perhaps use an int counter variable, that you increment each time data is entered, and base what the listener does with the data based on the value held by the counter.
You can create an instance of the listener and reuse it across the class, like:
...
OnChangeListener listener = new OnChangeListener() {
//All the code here
};
...
textField1.addOnChangeListener(listener);
textField2.addOnChangeListener(listener);
textField3.addOnChangeListener(listener);
...

Hitting enter to move a to next text field, like tab key?

I am creating a simple form for a class and I want the user to be able to press enter after filling out one JTextField. It moves on to the next available JTextField (not all of them are editable at a given time), similar to using tab.
When it gets to the last editable JTextField it should submit the form. Is that possible or is tab and the usual enter function (as a submit my only option)?
I'll post the code if necessary, but if you can answer the question without it, that'd be great too.
Like Durandal mentioned, there are multiple ways to go about it.
You could use JTextField's addActionListener method, addAction method, addKeyListener method, etc... They're all fairly similar - you're telling the text field to do something when something happens. (Although exactly how you do it differs by method)
Regardless, each time the action will need to know which text field is next in line. Then you can call JTextField#requestFocus() to transfer input focus to it.
You can also do this with setFocusTraversalKeys and setNextFocusableComponent

In Java, how do you hide a JList after making a selection?

I'm making a simple rock, paper scissor game in Java, and I have two JLists where Player 1 and Player 2 can make their selection. The thing is, I want the JList to be hidden when a selection is made (for obvious reasons, otherwise there'd be no point with the game ^_^), but I havent found a way to do this and still keep the selected value. Can anyone point me in the right direction?
Thanks!
JList extends JComponent, which has an extensive list of methods you could use to varying degrees. For example, if you just want to disable the JList from user interaction, there is setEnabled which will "grey" out that component. If you want to hide the JList completely, see setVisible.
Whichever route you take, just drop one of these methods in where your user makes their selection, and you should be good to go. In order to store the user's selection, you'll want to save that to a variable that goes beyond that particular JList so you can later compare it with the opponent's value. I'd recommend giving this a read.
jlist1.setVisible(false) will make it invisible

Creating a number of JTextField s that is defined by user input in way to to read text from all of them in a push of a button

I used to make my UI by making declaring new JTextField in a for cycle while attaching an action listener by anon class to each JTextField, which means that you have to press enter fire an event which will read the text of the field and put it into an array here is the code
Getting data from JTextField that is one of several local variables without a few minor changes . Now I have to modify it to so that i press a button like Apply in order to have values written into an array. While I found two ways to do this wonder what is the optimal way to do this .
The horrible way . Create an array that acts as temporary storage ,
replace ActionListeners with DocumentListeners that will place
the values into this temp array . And a button that will on press
iterate through the the temp array placing its values into the
target array.
A better way which I found while searching , create a JTextField
array as public and simply have a button that will on press iterate
through the JTextField array and place its values into the target
array.
Adder is an example that maintains a List<JFormattedTextField> to enforce formatting. It uses a PropertyChangeListener & FocusListener to update on navigation events, such as the default key binding to Tab and Shift-Tab.

How do I fire an action when the user leaves a JTextBox?

I've got a JTextField, and I'd like the system to do some processing on what the user typed whenever the user leaves the text field. The ActionListener that you can add to just the JTextField only fires when the user presses enter, however.
I'd like the processing routine to run whenever the user leaves the text box by any means - tabs, clicks out of it, presses enter, etc. (The processing in question is to save the text the user typed to the appropriate data object, nothing fancy.)
My google-fu has failed on this one: I'm confident that it's possible, I just can't see how.
Add a FocusListener.
It's worth noting that this is a relatively low-level listener. On JComboBox it wont work unless you find the text field (and perhaps button) that the particular PL&F inserts. Swing is a bit odd that way (amongst many other ways).
Although for my money, non-cosmetic changes that happen when focus leaves a field give poor user experience. Much better to do any relevant changes on every change with a listener on the text field's document.
If you want to edit the text as it is typed then you should use an DocumentFilter.
If you want to validate the text as a complete entity then you can use an InputVerifier.

Categories

Resources