IntelliJ Java IDE
The moose likes Swing / AWT / SWT / JFace and the fly likes listSelectionListener help needed Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT / JFace
Reply Bookmark "listSelectionListener help needed" Watch "listSelectionListener help needed" New topic
Author

listSelectionListener help needed

Kristof Camelbeke
Ranch Hand

Joined: Nov 28, 2001
Posts: 97
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

Joined: Dec 06, 2001
Posts: 3
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

Joined: Nov 28, 2001
Posts: 97
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

 
 
subject: listSelectionListener help needed
 
Threads others viewed
Regarding JList and DefaultListModel.
How can I make JList update when ListModel changed?
I need help creating a custom ListModel
selecting topics to get help information
JList maximum capacity???
IntelliJ Java IDE

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture