我有以下代码来实例化一个JTable:表格显示了正确的行数和列数,但是列的顶部没有标题。
public Panel1()
{
int nmbrRows;
setLayout(null);
setBackground(Color.magenta);
Vector colHdrs;
//create column headers
colHdrs = new Vector(10);
colHdrs.addElement(new String("Ticker"));
// more statements like the above to establish all col. titles
nmbrRows = 25;
DefaultTableModel tblModel = new DefaultTableModel(nmbrRows, colHdrs.size());
tblModel.setColumnIdentifiers(colHdrs);
scrTbl = new JTable(tblModel);
scrTbl.setBounds(25, 50, 950, 600);
scrTbl.setBackground(Color.gray);
scrTbl.setRowHeight(23);
add(scrTbl);
//rest of constructor
...
}
将它与其他制表代码进行比较,我没有看到任何缺失的步骤,但肯定缺少一些东西。
发布于 2010-02-24 03:04:08
将您的JTable
放入JScrollPane
中。试试这个:
add(new JScrollPane(scrTbl));
发布于 2015-06-30 20:01:55
正如在前面的回答中所说的,“正常”的方法是将其添加到JScrollPane中,但有时您不希望它滚动(不要问我何时:)。然后,您可以自己添加TableHeader。如下所示:
JPanel tablePanel = new JPanel(new BorderLayout());
JTable table = new JTable();
tablePanel.add(table, BorderLayout.CENTER);
tablePanel.add(table.getTableHeader(), BorderLayout.NORTH);
发布于 2017-06-17 18:34:50
public table2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 485, 218);
setTitle("jtable");
getContentPane().setLayout(null);
String data[][] = { { "Row1/1", "Row1/2", "Row1/3" },
{ "Row2/1", "Row2/2", "Row2/3" },
{ "Row3/1", "Row3/2", "Row3/3" },
{ "Row4/1", "Row4/2", "Row4/3" }, };
String header[] = { "Column 1", "Column 2", "Column 3" };
// Table
JTable table = new JTable(data,header);
// ScrollPane
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(36, 37, 407, 79);
getContentPane().add(scrollPane);
}
}
试试这个!!
https://stackoverflow.com/questions/2320812
复制相似问题