Reputation: 15
I know that a JTable is usually put in a JScrollPane, but I don't want to scroll at all, I want the table to grow when a row is added. I have used NetBeans IDE 8.2 to create a swing application that almost does what I want with my tables which I've put in JPanels, data and header in separate panels.
I've attempted to go to basics and hand craft the code I need but it isn't displaying the table at all.
public NewJFrame1() {
initComponents();
JTable table = new JTable();
DefaultTableModel tableModel = new DefaultTableModel();
tableModel.addColumn("Type");
tableModel.addColumn("Folder");
String[] row = {"Datum1","Datum2"};
table.setModel(tableModel);
tableModel.addRow(row);
this.getContentPane().add(table.getTableHeader());
this.getContentPane().add(table);
}
Upvotes: 0
Views: 90
Reputation:
I think you should set the model to the table
table.setModel(tableModel); //add this to your code
Ok, so from what i see ,you are using Netbeans and creating JFrame. If you are inserting your table from the design than there is no need for you to create another table in the constructor.Just simply:
public NewJFrame() {
initComponents();
DefaultTableModel tableModel = new DefaultTableModel();
tableModel.addColumn("Type");
tableModel.addColumn("Folder");
String[] row = {"Datum1","Datum2"};
tableModel.addRow(row);
jTable1.setModel(tableModel);// where jTable1 has been created and instantiated automatically by netbeans when you draged and dropt it to your frame , from the design.
}
Upvotes: 1
Reputation: 32
Radu Soigan is right, the JTable and TableModel are separate objects which need to be linked together using the setModel(tableModel) method as the table doesn't know where the data to display is at. This is also the same for custom table models, always remember to set it to the JTable after setting up the model.
Upvotes: 0