Create JTable in a JPanel and add row
- by DK64
On my program I've dinamically created a JFrame that contains a JPanel called jp. jp also contains a JTable that I would like to fill with some rows.
case KeyEvent.VK_R:
JFrame frame = new JFrame("Snake v2.0 - Rankings");
JPanel jp = new JPanel();
jp.setPreferredSize(new Dimension(300,300));
JTable table = new JTable();
JScrollPane tableContainer = new JScrollPane(table);
jp.add(tableContainer, BorderLayout.CENTER);
DefaultTableModel tm = (DefaultTableModel) table.getModel();
tm.addRow(new Object[] {"#","Player","Score","Date"});
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.getContentPane().add(jp);
frame.pack();
frame.setVisible(true);
break;
This is my code. When I press R on the keyboard, the JFrame with that JPanel inside appears but the table doesnt (picture). What could I do?