user3287269
user3287269

Reputation: 19

Issues with JTable, I can add to the table but can't see all rows

Within Netbeans I have created a table and added columns "film" and "director" to it. This works fine, but I can't see the table properly only a small section of the last row, which, although alerts me that all rows have been added. I still can't see them fully.

Code and screenshots are below.

Code

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {  
DefaultTableModel MovieTable = (DefaultTableModel) MovieTable.getModel();
 MovieTable.getDataVector().removeAllElements();
                   Column1 ="Film1";
                   Column2 ="Director";
                   MovieTable.addRow(new Object[]{Column1, Column2}); } 

Screenshot

Truncated table

If it helps at all this table is placed on top of a panel.

Upvotes: 0

Views: 773

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 208944

Not sure what you're doing wrong because you have not provided relevant code. But even if you did, it may bot even help because of all the messy code generated by the GUI Builder (I assume that's what you're using). So see below.

Simple step by step tutorial, that doesn't replicate your problem. Hope you can gain something from it.

  1. Create your JFrame form

  2. Drag and drop a JPanel to the frame. Expand it the size for the frame.

  3. Drag and Drop a JTable to the panel. There's no need for a JScrollPane as GUI builder will automatically wrap your table in a scroll pane.

  4. Highlight/select the table in the design view and go to the Properties window to the right of the design view.

  5. Click the ... located to the right of the model property.

  6. In the dialog create your model i.e. set the number of rows and columns and name your column headers. Click OK when done.

  7. Drag and drop two JTextFeilds for the Film and Director columns

  8. Drag and drop a JButton. Create the actionPerformed for that button by right clicking it and select Event -> action -> actionPerformed

  9. In the auto-generated actionPerformed do this.

    private void jButton1ActionPerformed(ActionEvent evt) {                                         
        DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
        model.getDataVector().removeAllElements();
    
        String column1 = jTextField1.getText();
        String column2 = jTextField2.getText();
    
        model.addRow(new Object[] {column1, column2});
    }    
    
  10. If you just wanted to add row without deleting all the other rows, get rid of

    model.getDataVector().removeAllElements();
    
  11. Run your program, add some text to the text field and press the button. Everything should work fine is all these steps above were followed. Works for me every time.

Upvotes: 1

Related Questions