JTable and TableModel

Hi,

I need some help with creating a Table. I have a TableModel that reads from a database and is used to populate a JTable. I want to store some information in the Table, but i need to have a field that is not displayed.

The field holds some information based on which the rows are colored black, red or blue. So, assuming my fields are id, username, password and status, i dont want to display the status column, but based on its value, i want to change the color of the fields.

also, the status value can change and needs to be stored. when the user right clicks on a row, based on the status he will have a different menu pop up.

is there any way i can do this (have the extra column but not display it)?

[746 byte] By [rapte] at [2007-9-26 1:59:03]
# 1

Yes,

Just dont include the value you don't want to see in the getValueAt() setValueAt()...

functions. In other words don't include it in your model.

// example doesn't use getters and setters but please use them

class Employee {

String fname;

String lname;

long ssoc;

}

Use a Vector to store Employees

String columnNames = {"FIRST NAME", "LAST NAME"};

Vector empVector

AbstractTableModel model = new AbstractTableModel() {

public int getColumnCount() {

return columnNames.length;

}

public int getRowCount() {

return empVector.size();

}

public String getColumnName(int col) {

return columnNames[col];

}

public Object getValueAt(int row, int col) {

Employee e = (Employee)empVector.elementAt(row);

switch (col) {

case 0:

return e.fname;

case 1:

return e.lname;

//optionally if you want to use the model to access the hidden field - this way the table will never ask for column 2 since columnNames is only 2 elements long

case 2:

return e.ssoc

default:

return null;

}

}

public Class getColumnClass(int col) {

return String.class;

}

public boolean isCellEditable(int row, int col) {

return false;

}

public void setValueAt(Object value, int row, int col) {

}

};

Now alll you have to do is get the ssoc form the empVector given the row selected or however you want to AS LONG AS empVector is a class variable (i.e. you have access to it in other methods)

Or you can use the option in the getValueAt function

hope this helps

lordkilgour at 2007-6-29 3:17:41 > top of Java-index,Archived Forums,Java Programming...