How do I create a Java Swing JTable with header as first column, not as first row?
I am using Java 6.0 + Swing + JTable.
Normally, JTable renders the header as the first row. (Left image)
However, I would like to render the header as the first column. (Right image)
I don't think this is a simple flag in JTable.
Can you please recommend an approach or 开发者_Go百科existing widget library?
I assume I need to override the paint() methods for JTableHeader and JTable.
Thanks
Oops, never mind you want a transposed TableModel, not just row headers.
See http://tips4java.wordpress.com/2008/11/18/row-number-table/
You can do that using an TableModel, you should return an specific Class in getColumnClass, use a Renderer to paint its like an Header Column. And probably, you will going to make your getValueAt in vertical.
This is not simple, but is possible.
When I posted it the images your posted was not visible, to your case, you should use a CellRenderer to make it like a Header, but its still on TableModel.
How about this, you basically override getValueAt(int row, int column). Here is a test class:
package so.pivot;
import javax.swing.JFrame;
import javax.swing.JTable;
public class Test extends JFrame{
public static void main(String[] args){
Test test = new Test();
String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
Object[][] data = {
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)}
};
JTable table = new JTable(data, columnNames){
@Override
public Object getValueAt(int row, int column){
return super.getValueAt(column, row);
}
};
test.add(table);
test.pack();
test.setVisible(true);
}
}
I share the same issue. Here's my implementation. I have an array for headers which is fixed, and a 2d array for the rest of my data, which is populated via json although not relevant nor shown here. Do note this supports dynamic populating on a column level:
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class Test {
public static void main(String[] argv) throws Exception {
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);
model.addColumn("Header");
String[] headers = { //If you wish to separate the headers array from columns array
"Header 1",
"Header 2",
"Header 3"
};
String [][]columnData = new String[][]{
{
"Column 1.1",
"Column 1.2",
"Column 1.3"
},{
"Column 2.1",
"Column 2.2",
"Column 2.3"
},{
"Column 3.1",
"Column 3.2",
"Column 3.3"
}
};
for(int col=1;col<(columnData.length+1);col++){
model.addColumn("Col"+col);
}
for(int i=0;i<headers.length;i++){
model.addRow(new Object[] { headers[i] });
}
for(int r=0;r<columnData.length;r++){
for(int c=0;c<columnData[0].length;c++){
model.setValueAt(columnData[r][c],c,r+1);
}
}
JFrame f = new JFrame();
f.setSize(500, 300);
f.add(new JScrollPane(table));
f.setVisible(true);
}
}
Here's the ouput:
精彩评论