• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

listSelectionListener help needed

 
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
 
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

 
Evil is afoot. But this tiny ad is just an ad:
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!