• Post Reply Bookmark Topic Watch Topic
  • New Topic

listSelectionListener help needed  RSS feed

 
Kristof Camelbeke
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need some help to update a Jtextfield.
when the value in the Jlist changes I want a change in the Jtextfield. Here is some code :

// this updates the Jlist (which is read from a MS-Access Database)
public void veldenListUpdate(){
DefaultListModel m = ((DefaultListModel) VeldenList.getModel());
m.removeAllElements();
try{
// create a statement handlez
Statement sqlStatement = databaseConnection.createStatement();

// create a sql-string
String sql = "SELECT * FROM VELD";
ResultSet results = sqlStatement.executeQuery(sql);

while (results.next()){
m.addElement(results.getString(1));
-----------------------------------------------------------------
At this point I will have a Jlist(Veldenlist)with names in it.
Now I want the selected name in the Jtextfield :
I know I have to work with a listSelectionListener but I don't really get it.
Sorry for this newbie question... but I can need all the help I can get. Or if you can tell me where I can find some examples I would be pleased.
Thanks.
Kristof
 
Cory Twibell
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kristof Camelbeke:
I need some help to update a Jtextfield.
when the value in the Jlist changes I want a change in the Jtextfield. Here is some code :

// this updates the Jlist (which is read from a MS-Access Database)
public void veldenListUpdate(){
DefaultListModel m = ((DefaultListModel) VeldenList.getModel());
m.removeAllElements();
try{
// create a statement handlez
Statement sqlStatement = databaseConnection.createStatement();

// create a sql-string
String sql = "SELECT * FROM VELD";
ResultSet results = sqlStatement.executeQuery(sql);

while (results.next()){
m.addElement(results.getString(1));


If I understand, you want the selected List Item to be displayed on the text field? If so, just use:
textField.setText((String)list.getSelectedValue() );
 
Kristof Camelbeke
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Cory Twibell:

If I understand, you want the selected List Item to be displayed on the text field? If so, just use:
textField.setText((String)list.getSelectedValue() );

-----------------------------------------------------------------
Yes that's what I wanted and it worked too !! Thanks a million !
Didn't know it was so simple just putting the selected value in the Jtextfield.
I now have some other Jtextfields that have to change when the cursor is moved but they are not on the Jlist but on the Database. So in this case i cannot just use the value of the list because it's not in there.
How should I do this ? Work with an array because the Resultset cannot be used ?
Kristof

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!