How to display a JTable in a JPanel with Java?
How to display a JTable in a JPanel 开发者_Python百科with Java?
Imports and table model left as an exercise to the user of this code. Also, the panel layout is arbitrarily chosen for simplicity.
public class JTableDisplay {
public JTableDisplay() {
JFrame frame = new JFrame("JTable Test Display");
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JTable table = new JTable();
JScrollPane tableContainer = new JScrollPane(table);
panel.add(tableContainer, BorderLayout.CENTER);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new JTableDisplay();
}
}
The scroll pane is fairly important to note. Without it, your table won't have a header or scroll if the content becomes larger than the display.
JTable table = new JTable();
JScrollPane spTable = new JScrollPane(table);
JPanel panel = new JPanel();
panel.add(spTable);
There is a comphrensive guide about how to layout swing components, you should consider Pyrolistical link..
精彩评论