- i have a table with employee attendance records for example :
- The column headers will be dates of a month in asc order 1/11,2/11,3/11 upto 30/11
- row 0 --> employee name - employee id
- row 1 --> start time
- row 2 --> end time
- row 3 --> break in
- row 4 --> break out
row 5 --> employee name - employee id
...If i type the employee name in the search box, the row with the employee name
- appears in the above case it is row 0. But i need to display the four consecutive
rows also which is the details for that employee.
Is there any way we can do this?.

- 147
- 1
- 2
- 10
-
*"is there any way we can do this."* Can you use the shift key at the start of sentences and add a full-stop to questions? Then I might be able to read that.. – Andrew Thompson Nov 20 '12 at 10:20
-
That does not sound like a regular table. Did you consider using the `JXTreeTable` of the SwingX project which might better suited for this. Even a `JList` sounds better with a custom renderer which returns a multiline component – Robin Nov 20 '12 at 10:29
-
This is a regular JTable. For every employee i want to display the all the rows that belongs to that employee – AnithaRaj Nov 20 '12 at 14:57
2 Answers
If I have understood correctly. If you search for an employee with employee name (say) then display the rows related to that employee has to happen.
AFAIK you can display the rows only if you maintain some link among them. Because each Employee data in the table row is just a row like the other rows. So create a Employee class
with the attributes and when ever user queries with the EmpName
, retrieve all the details of that employee.
But as far as design is considered, I think you should prefer giving each row for each employee instead of giving all employee names in one row or all start time in one row. Because it is confusing to see details without knowing what exactly they represent.
This is just my understanding and suggestions. Correct me if I am wrong.

- 8,736
- 10
- 54
- 81
-
+1 for a single employee per row; a second table can show the details for the selected employee, as suggested [here](http://stackoverflow.com/a/13480154/230513). – trashgod Nov 21 '12 at 07:48
-
not all employee names are in one row. the column 0 will have the static strings like i have mentioned before value(0,0) will be employeename value(0,1) will be start time value(0,2) will be end time and so on until val(0,13) then again val(0,14) will have second employee name until valu(0,26) and it goes on. col 1 will jan1 col2 will be jan2 and so on based on the date chosen. – AnithaRaj Nov 22 '12 at 14:00
Here's an outline of how to proceed:
Create two instances of
JTable
,employee
anddetail
.Let
employee
useSINGLE_SELECTION
as itsListSelectionModel
viasetSelectionMode()
.Let the
TableModel
ofdetail
contain aListSelectionListener
that receives events fromemployee
, as shown in this related example.To display the selected
employee
data indetail
, theTableModel
ofdetail
can extendAbstractTableModel
andfireTableDataChanged()
.
For example,
employee.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
// update the detail table model based on the selected employee
fireTableDataChanged();
}
}
});
-
hi your answer gave me an idea of finding the solution. Instead of creating two instances of table, i created two tablemodel. initially the table was set to tablemodel1 which holds all the records . When search button is clicked i got the row which matches the search text. Then retrieved the rows in a loop as all the employee has 13 records each. the tablemodel2 holds nly the employee details that was searched. after the loop exits, i set the tablemodel2 for the table. now i added a another button clear search which will again assign the tablemodel1 which holds all the records – AnithaRaj Nov 22 '12 at 14:05