JTable losing values when focus is lost

211 Views Asked by At

My JTable/ JXTreeTable "forgets" the values just entered and reverts to the default value of "Value x" when cell focus is lost (by clicking another cell, hitting enter, etc).

I have found a few suggestions on SO and nothing has helped. One was to set putClientProperty(...) which didn't change a thing. From: Can a Jtable save data whenever a cell loses focus?

I am at a loss why this is so difficult. Please explain why the entered data wont "stick" and how to fix it.

Here is my code:

public class MyJTreeTable extends JXTreeTable{
public MyJTreeTable()
{
    setTreeTableModel(new MyTreeStructure(5));
    setRowSelectionAllowed(true);
    setDragEnabled(true);
    setEditable(true);
    setTreeCellRenderer(new DefaultTreeRenderer());
    setSelectionBackground(new Color(176, 197, 227));
    setSelectionForeground(new Color(0, 0, 128));
    BorderHighlighter border = new BorderHighlighter(new ColumnHighlightPredicate(1, 2), BorderFactory.createEmptyBorder(0, 6, 0, 6), false, false);
    addHighlighter(border);
putClientProperty("terminateEditOnFocusLost", true);

   addMouseListener(new java.awt.event.MouseAdapter() 
    {
        @Override
        public void mouseClicked(java.awt.event.MouseEvent evt) 
        {
            int row = rowAtPoint(evt.getPoint());
            int col = columnAtPoint(evt.getPoint());
            if (row >= 0 && col >= 0) 
            {
                editCellAt(row, 1);
                setSurrendersFocusOnKeystroke(true);
                getEditorComponent().requestFocus();

            }
         }
    });
}

    @Override
    public boolean isCellEditable(int row, int column) 
    {
        if (column == 1)
        {
            return true;
        }
        else return false;
    }
}

class MyTreeStructure extends DefaultTreeTableModel 
{

    DefaultMutableTreeTableNode Root = null;
    MyTreeStructure(int i) 
    {
        Root = new DefaultMutableTreeTableNode();

        this.setRoot(Root);
        for (int x = 0; x < 5; x++) 
        {
            DefaultMutableTreeTableNode node = new DefaultMutableTreeTableNode("HI");
            Root.add(node);
            for (int j = 0; j < i; j++) 
            {
                node.add(new DefaultMutableTreeTableNode(j));
            }
        }
    }

    @Override
    public String getColumnName(int column)
    {
        return "Value";
    }

    @Override
    public int getColumnCount() 
    {
        return 2;
    }

    @Override
    public Object getValueAt(Object arg0, int arg1) 
    {
        if (arg1 == 1) 
        {

            return new String("Value " + arg0.toString());
        }
    return arg0;
    }

}
0

There are 0 best solutions below