Java – how to select all text in JTable cells during editing

I want the editor in my editable jtables to select all text in the cell at the beginning of editing I've tried a few about from tablecelleditor JTextComponent is called on the component returned by the gettablecelleditorcomponent method Selectall() There's nothing I've tried

In my recent attempt, I modified the simpletabledemo class from the swing tutorial to use a custom tablecelleditor that calls the SelectAll method In the debugger, I can see that the SelectAll () method is called, but the table still enters edit mode without selecting the text in the cell (or maybe clearing the selection before displaying) The code is shown below Can someone tell me what's wrong with me?

import javax.swing.DefaultCellEditor;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.text.JTextComponent;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class SimpleTableDemo extends JPanel {
    private boolean DEBUG = false;

    public SimpleTableDemo() {
        super(new GridLayout(1,0));

        String[] columnNames = {"First Name","Last Name","Sport","# of Years","Vegetarian"};

        Object[][] data = {
     {"Kathy","Smith","SNowboarding",new Integer(5),new Boolean(false)},{"John","Doe","Rowing",new Integer(3),new Boolean(true)},{"Sue","Black","Knitting",new Integer(2),{"Jane","White","Speed reading",new Integer(20),{"Joe","Brown","Pool",new Integer(10),new Boolean(false)}
        };

        final JTable table = new SelectingTable(data,columnNames);
        table.setPreferredScrollableViewportSize(new Dimension(500,70));
        table.setFillsViewportHeight(true);

        if (DEBUG) {
            table.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    printDebugData(table);
                }
            });
        }

        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);

        //Add the scroll pane to this panel.
        add(scrollPane);
    }

    private void printDebugData(JTable table) {
        int numRows = table.getRowCount();
        int numCols = table.getColumnCount();
        javax.swing.table.TableModel model = table.getModel();

        System.out.println("Value of data: ");
        for (int i=0; i 

Solution

The table select all editor should suit you This is the preferred solution, so you don't have to continue creating custom editors That is, columns containing integers can only accept integers With your current code

Part of your code works If you use the F2 key to start editing, the text is selected However, when you use the mouse and double-click a cell, the second mouse event is passed to the editor, so the caret can be placed where you click, which will delete the selection The solution is:

final JTextComponent jtc = (JTextComponent)c;
jtc.requestFocus();
//jtc.selectAll();
SwingUtilities.invokelater(new Runnable()
{
    public void run()
    {
        jtc.selectAll();
    }
});
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>