[英]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. 我使用的是Java 6.0 + Swing + JTable。
Normally, JTable renders the header as the first row. 通常,JTable将标头呈现为第一行。 (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. 我不认为这是JTable中的简单标志。
Can you please recommend an approach or existing widget library? 你能推荐一种方法或现有的小部件库吗?
I assume I need to override the paint() methods for JTableHeader and JTable. 我假设我需要覆盖JTableHeader和JTable的paint()方法。
Thanks 谢谢
Oops, never mind you want a transposed TableModel, not just row headers. 哎呀,没关系,你想要一个转置的TableModel,而不仅仅是行标题。
See http://tips4java.wordpress.com/2008/11/18/row-number-table/ 见http://tips4java.wordpress.com/2008/11/18/row-number-table/
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.
我有一个固定的标题数组,以及我的其余数据的二维数组,它通过json填充,虽然不相关,也没有在这里显示。 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: 这是输出:
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. 您可以使用TableModel执行此操作,您应该在getColumnClass中返回一个特定的类,使用Renderer将其绘制为Header Column。 And probably, you will going to make your getValueAt in vertical.
也许,你将使你的getValueAt垂直。
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. 当我发布它所发布的图像不可见时,对于你的情况,你应该使用CellRenderer使它像一个标题,但它仍然在TableModel上。
How about this, you basically override getValueAt(int row, int column). 怎么样,你基本上覆盖了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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.