Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to Return multiple String in JTextField  RSS feed

 
Chris Coura
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i have a program which does this. Any string entered by the user is stored as a String in an arraylist. When the user presses the up arrow, the most recently entered command is shown in the JTextField. Further presses of the up arrow will display other commands in the JTextField,ordered by the most recently entered until there are no more commands to display or the user stops.

Storing the the string in the arrayalist works fine the only issue is with the Jtextfield only one command is shown in it when i press the up arrow even if i have typed multiple commands Can any body help below is the code
for when the up arrow is pressed.

 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are describing sounds like it should be a ComboBox control.
 
Les Morgan
Rancher
Posts: 752
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris,

The problem you will always have with your desired result is to resize your control. Tim's suggestion will get your a dropdown, but only 1 line will display until you activate the dropdown. You will need to use a layout in your control that will display your data in a list, after you add your new line, you will need to resize your control, then repaint your container to reflect the new size.

Les

Chris Coura wrote:Hi, i have a program which does this. Any string entered by the user is stored as a String in an arraylist. When the user presses the up arrow, the most recently entered command is shown in the JTextField. Further presses of the up arrow will display other commands in the JTextField,ordered by the most recently entered until there are no more commands to display or the user stops.

Storing the the string in the arrayalist works fine the only issue is with the Jtextfield only one command is shown in it when i press the up arrow even if i have typed multiple commands Can any body help below is the code
for when the up arrow is pressed.

public void keyPressed(KeyEvent e)
{


if (e.getKeyCode() == KeyEvent.VK_UP) {


int value;
String code ;

for( value = 0 ; value < history.storehistory.size() ; value++)
{
field.setText(history.storehistory.get(value));
}


}
 
Campbell Ritchie
Sheriff
Posts: 53750
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Always use the code button; I edited your post using it and doesn't it look better now
But your indentation is inconsistent; please look at some suggestions, e.g. here, because you can confuse yourself severely with incorrect indentation. Also write fewer empty lines.
 
Fred Kleinschmidt
Bartender
Posts: 507
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot do what you want with a JTextField, which by design displays only one line. You need to use a different component. Others have suggested a JComboBox, but you might also consider a JTextArea inside a JScrollPane.
 
Dave Tolls
Ranch Hand
Posts: 2504
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This code will loop through the entire history list and set the text in the field to each of them, before ending up on the last one.
Each time you hit the UP it will do this.
So each time you will only ever actually see the last entry in the list.

What you need to do is somehow track the number of times UP has been clicked, in a variable external to this handler, then change it on each click.
Something like:


You'll also need somewhere to reset that history index, possibly when a command or whatever the field represents is executed (or whatever).
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quite apart from all the other suggestions, Swing was designed to be used with Key Bindings which are more flexible and reliable than the KeyListener inherited form AWT.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:You cannot do what you want with a JTextField, which by design displays only one line.

Actually, you can. The problem description requires only one line to be displayed at a time.

Fred Kleinschmidt wrote:Others have suggested a JComboBox, but you might also consider a JTextArea inside a JScrollPane.

Or a JList in a JScrollPane.

However, a JTextField with key bindings for the up and down arrow keys (probably in the WHEN_FOCUSED map), in conjunction with a List<String> and an int index as instance fields, would do the job just fine.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After thinking about it some more, I believe the best fit would be a JSpinner with a SpinnerListModel.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!