How to select all text in JTable cell when editing

后端 未结 3 1617
遇见更好的自我
遇见更好的自我 2021-01-02 02:12

I would like to have the editor in my editable JTables select all text in the cell when starting to edit. I have tried a couple of things that all revolve around calling JT

3条回答
  •  渐次进展
    2021-01-02 02:38

    The Table Select All Editor should work for you. It is the preferred solution so you don't have to keep creating custom editors. That is the columns containing integers should only accept integers. With you current code

    Your code does work partially. If you start editing using the F2 key, then the text is selected. However, when you use the mouse and double click on the cell then the second mouse event is passed to the editor so the caret can be positioned where you clicked and this removes the selection. A solution for this is:

    final JTextComponent jtc = (JTextComponent)c;
    jtc.requestFocus();
    //jtc.selectAll();
    SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            jtc.selectAll();
        }
    });
    

提交回复
热议问题