Updated: Displaying a jTable inside another jTable // JTable cellRenderer
After some users asking for answers in my previous post, I updated the code of the embedded JTable to make it less buggy and more stable.
This is the "new" code:
1// We initialize the Objects where we'll store our data
2// First an array Object which will be our main table
3Object[][] data = null;
4// We create an Array String to hold the name of the main table columns
5//In this example case we are going to store a contact name,
6//his email address(es) and his phone numer(s)
7//We're also going to store the creation date.
8String[] columns = {"Name", "E-Mail","Phone","Creation Date"};
9// First thing is to populate our data object with some example values
10public void populateData() {
11 // Two contacts
12 data = new Object[3][columns.length];
13 data[0][0] = "Peter";
14 String[] emails = {"peter@yahoo.com", "strange@name.com"};
15 data[0][1] = emails;
16 String[] phones = {"555 35 25 65" , "555 35 24 63"};
17 data[0][2] = phones;
18 data[0][3] = new Date();
19 data[1][0] = "Jackson";
20 data[1][1] = "Jack@hotmail.com";
21 String[] phones2 = {"555 35 24 33" , "555 11 88 88", "332 55 25 34"};
22 data[1][2] = phones2;
23 data[1][3] = new Date();
24 data[2][0] = "Robert";
25 data[2][1] = "rob@hotmail.com";
26 data[2][2] = "555 28 95 81";
27 data[2][3] = new Date();
28 // As you can see, we've stored two contacts one with two e-mail
29 // accounts and two phone numbers, and another member with three phone numbers
30}
31// Next we create our table models
32public void createModel(){
33 /* First we create the main model We overide the AbstractTableModel necessary methods*/
34 AbstractTableModel modelo = new AbstractTableModel() {
35 public String getColumnName(int col) {
36 return columns[col];
37 }
38 public Class getColumnClass(int col) {
39 if(getRowCount() <1) {
40 return null;
41 }
42 return data[0][col].getClass();
43 }
44 public int getRowCount() {
45 return data.length;
46 }
47 public int getColumnCount() {
48 return columns.length;
49 }
50 public Object getValueAt(int row, int col) {
51 return data[row][col];
52 }
53 public boolean isCellEditable(int row, int col){
54 return true;
55 }
56 public void setValueAt(Object value, int row, int col) {
57 data[row][col] = value;
58 fireTableCellUpdated(row, col);
59 }
60 };
61 // We apply the model to the main jTable
62 jTableData.setModel(modelo);
63 // We create a cell Renderer to display the data of the multivalue fields
64 TableCellRenderer jTableCellRenderer = new TableCellRenderer() {
65 /* Magic Happens */
66 public Component getTableCellRendererComponent(
67 JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
68 /* If what we're displaying isn't an array of values we return the normal renderer*/
69 if(!value.getClass().isArray()){
70 return table.getDefaultRenderer(value.getClass())
71 .getTableCellRendererComponent( table, value, isSelected, hasFocus,row, column);
72 } else {
73 final Object[] passed = (Object[])value;
74 // We create the table that will hold the multivalue
75 // fields and that will be embedded in the main table
76 JTable embedded = new JTable(new AbstractTableModel() {
77 public int getColumnCount() {
78 return 1;
79 }
80 public int getRowCount() {
81 return passed.length;
82 }
83 public Object getValueAt(int rowIndex, int columnIndex) {
84 return passed[rowIndex];
85 }
86 public boolean isCellEditable(int row, int col){
87 return true;
88 }
89 });
90 if(isSelected){
91 embedded.setBackground(jTableData.getSelectionBackground());
92 embedded.setForeground(jTableData.getSelectionForeground());
93 }
94 if(hasFocus){
95 embedded.setRowSelectionInterval(0,1);
96 }
97 // If this is what you plan to enable mouseClick detection,
98 // in your table, IT WONT WORK. Have a look at TableCellEditor.
99 embedded.addMouseListener(new MouseAdapter() {
100 public void mouseClicked(java.awt.event.MouseEvent evt) {
101 System.out.println("PEPE");
102 }
103 });
104 setPreferredSize(embedded.getPreferredSize());
105 if(getPreferredSize().height != table.getRowHeight(row)) {
106 table.setRowHeight(row, getPreferredSize().height);
107 }
108 return embedded;
109 }
110 }
111 };
112 // Finally we apply the new cellRenderer to the whole table
113 TableColumnModel tcm = jTableData.getColumnModel();
114 for(int it = 0; it < tcm.getColumnCount(); it++){
115 tcm.getColumn(it).setCellRenderer(jTableCellRenderer);
116 // tcm.getColumn(it).setCellEditor(jTableCellEditor);
117 }
118 // Note: if we need to edit the values inside the embedded jtable
119 // we will need to create a TableCellEditor too.
120}

In the above image, you can see the result.
Comments in "Updated: Displaying a jTable inside another jTable // JTable cellRenderer"
Second, you CAN get events to the inner JTable. I keep the JTables around, and if the embedded.MouseListener sees it's been clicked on a cell with a JTable, it does a 'dispatchEvent' on the sub-table handing it the event.
Now, if I could just get highlighting/selection to work properly...
What you say its true, you can make your upper table dispatch events to your subsequent jTables. But although you can achieve what you want (partly) its NOT a recommended way of doing things.
TableCellRenderer doesn't build a component for each cell in your JTable, that would be very memory consuming. So if you want to dispatch events to the inner JTables, you must do as you say "keep them around". The correct/elegant/performance way of doing this is implementing your own TableCellEditor.
Further reference can be found in this link:
Sun Bug Database 4136681
but i am facing one proble ..
if i put JTable inside JTable cell....it's not showing whole table i mean it's not showing inside table's column how to do that....plz help me if possible....
besides that, i was having trouble trying to read that data from a list and asiging it to the String[] or to the Data[][]... any ideas?
Thank you, the code is perfectly working for me.
If I try to copy the table data, the embedded cell values are copied as an Object
i.e. Peter [Ljava.lang.Object;@20785a37 [Ljava.lang.Object;@2d649736 04-abr-2007
How can i copy the data as it is for embedded cells instead of Objects.