Creating a Java Table Model which is initially empty but allows dynamic addition of rows by user
I am making an application whose main function is encompassed in a JTable. I need to make an accompanying table model with an addRow method, the user enters details in the window and upon clicking a JButton the data is brought in to an array which is passed to the addRow() method for addition to the table. It appears to be impossible to make an empty table however, in the case that I make a two dimensional array for rows with 10 rows, when rendering the table the compiler flags up a NullPointerException within the 'JTa开发者_如何转开发ble.prepareRenderer' method.
I hope what I need to do has been made clear!
Here is sample code for you. You can easily modify it according to your requirements.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
public class FrmTable extends JFrame{
private JTable table;
private JButton btnAdd;
private DefaultTableModel tableModel;
private JTextField txtField1;
private JTextField txtField2;
private FrmTable() {
createGUI();
}
private void createGUI() {
setLayout(new BorderLayout());
JScrollPane pane = new JScrollPane();
table = new JTable();
pane.setViewportView(table);
JPanel eastPanel = new JPanel();
btnAdd = new JButton("Add");
eastPanel.add(btnAdd);
JPanel northPanel = new JPanel();
txtField1 = new JTextField();
txtField2 = new JTextField();
JLabel lblField1 = new JLabel("Column1 ");
JLabel lblField2 = new JLabel("Column2 ");
northPanel.add(lblField1);
northPanel.add(txtField1);
northPanel.add(lblField2);
northPanel.add(txtField2);
txtField1.setPreferredSize(lblField1.getPreferredSize());
txtField2.setPreferredSize(lblField2.getPreferredSize());
add(northPanel, BorderLayout.NORTH);
add(eastPanel, BorderLayout.EAST);
add(pane,BorderLayout.CENTER);
tableModel = new DefaultTableModel(new Object[]{"column1","column2"},0);
table.setModel(tableModel);
btnAdd.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
int count = tableModel.getRowCount()+1;
tableModel.addRow(new Object[]{txtField1.getText(),txtField1.getText()});
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
FrmTable frm = new FrmTable();
frm.setLocationByPlatform(true);
frm.pack();
frm.setDefaultCloseOperation(EXIT_ON_CLOSE);
frm.setVisible(true);
}
});
}
}
A table with 10 rows is not an empty table. That is a table with 10 rows of nulls. Make sure your rows have the correct number of columns. Follow the stack trace and find the source of the NullPointer, if it is in a CustomerRenderer then make sure you are handling nulls correctly.
Not sure what data you are including in your table, but maybe your 10 rows should be defaulted with empty Strings instead of nulls.
精彩评论