Reputation: 479
I try to initiate a "JTable", I added all my elements through the form designer and initiated them in the main function of my GUI.
The table is placed inside a "JScrollPanel" and used a "DefaultTableModel" to add the headers and rows.
Whatever I did, I can't make the table to display headers or rows.
What am I missing here?
class Controls extends JPanel{
private JButton compileButton;
private JPanel controls;
private JTabbedPane tabbedPane1;
private JButton insertButton;
private JTable insertedFilesTable;
private JScrollPane insertedFilesViewport;
private JPanel minify;
private JFileChooser insertChooser;
public Controls () {
insertChooser = new JFileChooser();
compileButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
initCompile();
}
});
insertButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
buttonActionPerformed(e);
}
});
}
public void main () {
JFrame frame = new JFrame("Controls");
frame.setLayout(new SpringLayout());
frame.setContentPane(new Controls().controls);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DefaultTableModel model = new DefaultTableModel();
model.addColumn("Files");
model.addColumn("Status");
insertedFilesTable = new JTable(model);
insertedFilesViewport = new JScrollPane(insertedFilesTable);
insertedFilesViewport.setViewportView(insertedFilesTable);
insertedFilesTable.setFillsViewportHeight(true);
String[] data = {"test","test"};
model.addRow(data);
frame.add(insertedFilesViewport);
frame.setSize(500,500);
frame.setVisible(true);
}
private void buttonActionPerformed(ActionEvent evt) {
insertChooser.showSaveDialog(this);
}
}
Upvotes: 0
Views: 66
Reputation: 324118
frame.setLayout(new SpringLayout());
....
frame.add(insertedFilesViewport);
Don't change the layout of the frame to a SpringLayout. There is no reason to do this.
The reason you don't see the scroll pane containing the table is because you didn't use any constraints for the add(...) method. Read the section from the Swing tutorial on How to Use SpringLayout to see how complex the constraints are for adding components.
If you leave the layout as the default BorderLayout
, then the component will be added to the CENTER
of the BorderLayout
by default. The above tutorial also has a section on How to Use BorderLayout
you should read.
Upvotes: 1