Java – JTable cell returning null

javajtableswing

I am using JTextField inside JTable cells; I use a TabelModel which has the dynamic data of the JTable. Now, when I click a button, I am reading a cell value.
Problem is the cell which has the present focus doesn't return the updated value. For example, consider this program:

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

public class MyTable extends JFrame {

    DefaultTableModel tmodel = new DefaultTableModel(new Object[][]{
            {"some"}, {"any"}, {"even"}, {"text"}, {"and"}, {""}},
        new Object[]{"Column 1"});

    public MyTable() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JTable table = new JTable(tmodel);
        table.setDefaultRenderer(Object.class, new MyRenderer());
        getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
        getContentPane().setLayout(new GridLayout(2, 2));
        JButton jb = new JButton("click me"); //button to display last cell data
        jb.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                JOptionPane.showMessageDialog(
                    null, table.getModel().getValueAt(5, 0));
            }
        });
        getContentPane().add(jb);
    }

    public static void main(String arg[]) {
        new MyTable().setVisible(true);
    }
}

class MyRenderer implements TableCellRenderer {

    public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int column) {
        JTextField editor = new JTextField();
        if (value != null) {
            editor.setText(value.toString());
        }
        return editor;
    }
}

Here I am leaving the last field empty. When I read it by

table.getModel().getValueAt(5, 0)

I get null. Now I change the value at 5,0, and again click the button, but now again I get null. Now surprisingly, I edit some other cell, again click button; now I get the correct data at cell 5,0! Why is this? Is this a bug? I tried several possibilities!

Best Solution

There are several points to consider within your example:

First you should have a look in Java - Tutorials to learn about the concept of Renderer and Editor within JTables, because you kinda mix things up here. Within your Renderer you use a JTextField which doesn't make sense, because this object is just used to present the data within your tableModel. Normally a JLabel is used for presenting, since you wont ever edit a value within a Renderer.

Moreover your example (eventhough I haven't tried it yet) should work, since a JTable already has a DefaultEditor. Maybe you just forgot to commit the value with pressing enter before you clicked the button to check which value is in?

I really can just recommend for reading this tutorial and search for some examples on how this is handled normally. You should find a lot of working examples.

Related Question