0

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

}
JavaBeast
  • 766
  • 3
  • 11
  • 28
  • 1
    Never used a TreeTableModel but I would guess you need a setValueAt(...) method for the model. – camickr Dec 01 '17 at 05:53
  • 1
    I believe the problem is that you're creating DefaultMutableTreeTableNodes. The documentation says "This implementation is designed mainly for testing. It is NOT recommended to use this implementation." so you should implement your own AbstractMutableTreeTableNode. If you debug into DefaultTreeTableModel.setValueAt you'll see that TreeTableNode.setValueAt is never called because TreeTableNode.getColumnCount is returning the wrong number. Note that getColumnCount is one of the methods you have to implement in AbstractMutableTreeTableNode. – Amber Dec 01 '17 at 16:24
  • 1
    @Amber this fixed the issue! thanks so much... – JavaBeast Dec 01 '17 at 20:49

0 Answers0