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

做~自己de王妃 提交于 2019-12-25 04:22:52

问题


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

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


回答1:


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.



来源:https://stackoverflow.com/questions/21654957/issues-with-jtable-i-can-add-to-the-table-but-cant-see-all-rows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!