Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Which event to use when selecting a row in a Table

Status
Not open for further replies.

Salo

Programmer
May 25, 2001
6
SE
Which event should be appropriate when selecting a row in a Table. More precisely I want to display the info about the row in textfields when clicking on a row. Anyone got a clue?
 
Hi Salo,

This is one way you could do it:

import java.awt.*;
import java.awt.event.*;

public class ListTest extends Frame implements ItemListener
{
public ListTest()
{
List list = new list();
list.addItemListener(this); // class implements ItemListener

// ... do some GUI stuff.....
}

// This method handles the itemevents when you select item from list
public void itemStateChanged(ItemEvent e)
{
ItemSelectable is = e.getItemSelectable();
String rowValue = is.getSelectedObjects()[0].toString();
// rowValue contains the value of the selected row
}

}

Check the documentation related to java.awt.List, java.awt.ItemSelectable and java.awt.event.ItemEvent
from
I hope that helped you.

-Vepo
 
Hi Salo,

I think you will have to be more precise in what Table you are talking about? If you are talking about JTable, there is an example that comes along with the jdk package. It can be found in the folder c:\jdk1.3\demo\jfc\TableExample\ .

If I don't remember wrongly, the program allows you to click (or double click) on a particular row of data retrieved from the database and displays information on that row for you to edit.

Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Hi!

I didn't find any help in the examples that comes with the JDK. :(

I wondered if it's possible to do it like this:

Code:
public class Table extends JTable implements ItemSelectable
{
...
}

//Salo
 
Hi Salo,

Sorry for the late reply. I am not so sure whether implementing ItemSelectable will work or not (because I always can't seem to get ItemSelectable to work for other cases, not JTable). However, I know implementing MouseListener will work.

Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
I solved the problem! :)

In the frame-class I did like this:


Code:
public class Main_Frame extends JFrame implements ListSelectionListener
{
  JTable table;
...
  ListSelectionModel lsm = table.getSelectionModel();
...
  //Somewhere in the main code/constructor
  table.setSelectionModel(lsm);
...
  //Method from the interface
  public valueChanged (ListSelectionEvent e)
  {
    if (e.getSource() == table.getSelectionModel())
    ...
  }
...
}

Thanks for your efforts to help me!

Regards,
Salo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top