Reputation: 19
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.
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}); }
Upvotes: 0
Views: 773
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.
Create your JFrame
form
Drag and drop a JPanel
to the frame. Expand it the size for the frame.
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.
Highlight/select the table in the design view and go to the Properties window to the right of the design view.
Click the ... located to the right of the model property.
In the dialog create your model i.e. set the number of rows and columns and name your column headers. Click OK when done.
Drag and drop two JTextFeild
s for the Film and Director columns
Drag and drop a JButton
. Create the actionPerformed
for that button by right clicking it and select Event -> action -> actionPerformed
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});
}
If you just wanted to add row without deleting all the other rows, get rid of
model.getDataVector().removeAllElements();
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