tbarrawi
asked on
populating a JTable with two columns while having the database id accessible
Hi, new to using JTable, and since in a rush to finish this piece in the next hour, hence the 500points. I have a JTable and I want to populate it with two clumns First Name, Last Name. Data is coming from the database . I want to be able to click on one row and get the database ID of the row so I can use it elsewhere. If I was using a JList, I would create a vector of PairElement class [below] and assign it to the list, but since I need to display two columns, I will use JTable. Any working suggestion plz? To be simple, here is the data to be displayed
columnNames = { "first Name ", "Last Name"};
rows:
FirstName1, LastName1 //database id 100
FirstName2, LastName2 //database id 101
When I click on the first raw, I wanna be able to get the id 100. So what is the common approach to do this in JTable?
public class PairElement
{
protected String m_name;
protected String m_id;
public PairElement(String name, String id)
{
m_name = name;
m_id = id;
}
public void setName(String name) {
m_name = name;
}
public String getName() {
return m_name;
}
public void setID(String id) {
m_id = id;
}
public String getID() {
return m_id;
}
public String toString() {
return m_name;
}
}
columnNames = { "first Name ", "Last Name"};
rows:
FirstName1, LastName1 //database id 100
FirstName2, LastName2 //database id 101
When I click on the first raw, I wanna be able to get the id 100. So what is the common approach to do this in JTable?
public class PairElement
{
protected String m_name;
protected String m_id;
public PairElement(String name, String id)
{
m_name = name;
m_id = id;
}
public void setName(String name) {
m_name = name;
}
public String getName() {
return m_name;
}
public void setID(String id) {
m_id = id;
}
public String getID() {
return m_id;
}
public String toString() {
return m_name;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Pass the result set to that method. The first param to that method is redundant so you can get rid of it
ASKER
CEHJ, but if the record set will return id,first_name,last_name, this means the JTable will have three columns visible. I don't wanna show the id?
Wait a minute, this is ringing a bell! Haven't we been through this before and you found a solution based on a JList?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You'd use the following to create your list which can then be use to create your table model
public List readResultSet(ResultSet rs)
{
List rows = new ArrayList();
while (rs.next())
{
MyObject o = new MyObject(rs.getString(1), rs.getString(1), rs.getString(2));
rows.add(o);
}
return rows;
}
public List readResultSet(ResultSet rs)
{
List rows = new ArrayList();
while (rs.next())
{
MyObject o = new MyObject(rs.getString(1), rs.getString(1), rs.getString(2));
rows.add(o);
}
return rows;
}
> this means the JTable will have three columns visible.
It sure would, the approach I suggested avoids that.
It sure would, the approach I suggested avoids that.
ASKER
CEHJ, good memory :) the first time i had one column only so JList was a good solution
Use the code i gave you, passing the ResultSet got from your query, then do:
table.removeColumn(table.g etColumnMo del().getC olumn(0));
The data will still be there in the table model
table.removeColumn(table.g
The data will still be there in the table model
ASKER
CEHJ, and on row click even I will be able to retrieve the ID eventhough the column was removed??
objects, thanks, I am reading the code to understand it.
objects, thanks, I am reading the code to understand it.
> I am reading the code to understand it.
You'll find it to be a far more flexible approach :)
And no need to go removing columns from your table ;)
You'll find it to be a far more flexible approach :)
And no need to go removing columns from your table ;)
>>CEHJ, and on row click even I will be able to retrieve the ID eventhough the column was removed??
Yes. You get the value from the table model col 0, row click index
Yes. You get the value from the table model col 0, row click index
ASKER
what a competion! what shall I do now.... :)
I know which I'd use ;)
ASKER
lol i am sure you know objects
look guys since I learned two good approaches from your answers, I will split. And i will keep which approach i will use to myself :) Thanks!
look guys since I learned two good approaches from your answers, I will split. And i will keep which approach i will use to myself :) Thanks!
:-)
Thanks mate, let me know if you have any more questions.