有关为什么以下代码会显示列标题的任何想法?我曾尝试使用和不使用scollpane。
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 600);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollpane = new JScrollPane(table);
contentPane.add(scrollpane, "wrap, span");
tableModel = new DefaultTableModel(new Object[]{"Name","Instrument Type","Channel","Number of Channels"},0);
table = new JTable();
table.setBounds(6, 6, 697, 172);
tableModel = new DefaultTableModel(new Object[]{"Fixture Name","Fixture Type","Fixture Channel","Number of Channels"},0);
table.setModel(tableModel);
contentPane.add(table);
答案 0 :(得分:3)
您的代码段要么丢失了,要么示例错误。
JScrollPane scrollpane = new JScrollPane(table);
这意味着你已经创建了表格,在这种情况下,你是双重创建它,或者它将生成NullPointerException
您的代码应该更像......
table = new JTable();
tableModel = new DefaultTableModel(new Object[]{"Fixture Name","Fixture Type","Fixture Channel","Number of Channels"},0);
table.setModel(tableModel);
contentPane.add(new JScrollPane(table));
答案 1 :(得分:1)
tableModel = new DefaultTableModel(new Object[]{"Fixture Name","Fixture Type","Fixture Channel","Number of Channels"},0);
table.setModel(tableModel);
contentPane.add(table); // This is the place that is creating problem for you.
添加时只需将JTable添加到JScrollPane并将滚动窗格添加到容器中,如下所示,
**contentPane.add(new JScrollPane(table));**
请参阅@Dan帖子why to use a JScrollPane.