Create JTable in a JPanel and add row

Posted by DK64 on Stack Overflow See other posts from Stack Overflow or by DK64
Published on 2013-06-29T10:11:38Z Indexed on 2013/06/29 10:21 UTC
Read the original article Hit count: 228

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about java

Related posts about jtable