2

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 'JTable.prepareRenderer' method.

I hope what I need to do has been made clear!

jzd
  • 23,473
  • 9
  • 54
  • 76
oli.burgess
  • 87
  • 1
  • 5
  • 12
  • Actually this is not very clear. An SSCCE would really help explain your problem better. – jzd Mar 15 '11 at 11:57
  • Read the Javadoc of the `TableModel` interface; you may want to write your own implementation. And don't forget to fire the appropriate events when rows are added/removed/modified. – Laurent Pireyn Mar 15 '11 at 12:01
  • why did you reinvent the wheel? The DefaultTableModel does what you want without any custom code. – camickr Mar 15 '11 at 15:26

2 Answers2

10

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);

            }

        });
    }
} 
Gursel Koca
  • 20,940
  • 2
  • 24
  • 34
  • Thank you. I didn't use your precise implementation but simply left the rows array as undefined within an abstracttablemodel, and upon clicking the okay button the rows array is instantiated to the passed array, and beyond that the array is enlarged by one with each addition. Thanks for giving me the brainwave! – oli.burgess Mar 15 '11 at 13:43
  • I think there is a problem with adding a new line: `tableModel.addRow(new Object[]{txtField1.getText(),txtField1.getText()});`. The same text field is used twice. – lovasoa Nov 08 '14 at 09:38
2

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.

jzd
  • 23,473
  • 9
  • 54
  • 76
  • The reason I made a table with 10 empty rows was that there seemed to be no way of making a table without specifying a row element. do you know if there is – oli.burgess Mar 15 '11 at 11:58
  • 1
    @Oli, you can create your table model that has any amount of rows or without rows. If you use a Default Table Model you can pass it an empty array for the data. – jzd Mar 15 '11 at 12:00