我在JTable
中放置JPanel
,但在显示时,我会看到表格内容,但不会看到列名称。
public class Neww extends JPanel
{
Connection conn = null;
Statement st;
DefaultTableModel model = new DefaultTableModel(new Object[][] {
{ " ", " " },
{ " ", " " },
{ " ", " " },
{ " ", " " },
{ " ", " " },
{ " ", " " }
}, new Object[] {
"ItemName",
"No of items"
});
JTable table = new JTable(model);
TableColumn ItemName = table.getColumnModel().getColumn(0);
JComboBox comboBox = new JComboBox();
Neww()
{
this.setLayout(new FlowLayout(FlowLayout.CENTER));
this.add(table);
comboBox.addItem("Spoon");
comboBox.addItem("Plate");
comboBox.addItem("Mixer");
comboBox.addItem("Glass");
ItemName.setCellEditor(new DefaultCellEditor(comboBox));
}
}
答案 0 :(得分:3)
有两种方式
(正确的方式)必须将JTable
添加到JScrollPane
,然后JTableHeader
可见
从JTableHeader
获取JTable
(将JPanel
LayoutManager
更改为BorderLayout
)并将NORTH area
放入JPanel
}}
答案 1 :(得分:2)
1)将JTable
换成JScrollPane
。像这样:
JTable table=...;
JPanel container = new JPanel();
JScrollPane jsp=new JScrollPane(table);
container.add(jsp);
2)使用getTableHeader()
并在需要的地方添加它(通常位于北方):
...
JTableHeader header = table.getTableHeader();
JPanel container = new JPanel(new BorderLayout());
// Add header at NORTH position
container.add(header, BorderLayout.NORTH);
//Add table below header
container.add(table, BorderLayout.CENTER);
答案 2 :(得分:1)