简体   繁体   中英

Is there a way to select all text in a cell in a JTable without a CellEditor?

Short Version: Given a JTable with a TableModel that extends DefaultTableModel, can I select all the text in a cell with the class String without the use of a CellEditor?

Long Version: I have made a TableModel which extends DefaultTableModel and should only have selectable rows be editable. Here's the relevant parts:

class MyTableModel extends DefaultTableModel {

    private List<String> columnNames;
    private List<List<String>> strings;

    /** editable stores the columns which we're allowed to change */
    private List<Integer> editable;

    public MyTableModel (List<String> columnNames, List<List<String>> 
            strings, List<Integer> editable) {
        this.columnNames = columnNames;
        this.strings = strings;
        this.editable = editable;
    }

    public Object getValueAt(int row, int col) {
        return strings.get(col).get(row);
        return null;
    }

    public void setValueAt(Object value, int row, int col) {
        strings.get(col).set(row, (String) value);
        fireTableCellUpdated(row, col);
    }

    public boolean isCellEditable(int row, int col) {
        return canEdit(col);
    }

    /**
     * This method looks like it should be lumped into isCellEditable()
     * But there's unique behavior I plan on having later for the first
     * column and this will let my code down the road be much cleaner.
     **/ 
    public boolean canEdit(int col) {
        return col != 0 && editable.indexOf(col) != -1;
    }
}

As far as understand, the DefaultTableModel treats cells populated with Strings as JTextFields. Our use of this table is to populate it with two columns. An uneditable column with filenames (as Strings), followed by an editable empty column of Strings. When an editable cell is selected, I'd like it to set its text the text of the filename before it, then select all the text (later the user executes a command that has an optional String field for each file, so if a filename's associated editable cell isn't empty, it uses that value. We don't want the user to have to retype the entire filename every time they set this, but also want to give them the ability to quickly do so if they want).

The table has a MouseListener which sees if an editable cell was clicked on, and if it was, it also fills in the cell with the file's name before it.

table.addMouseListener(new MouseAdapter() {
    public void mouseReleased(MouseEvent e) {       
        JTable table = ((JTable) e.getSource());
        MyTableModel model = (MyTableModel)table.getModel();
        int row = table.rowAtPoint(e.getPoint());
        int col = table.columnAtPoint(e.getPoint());

        if(tableModel.canEdit(col))
            tableModel.setValueAt(
                tableModel.getValueAt(row, col - 1), row, col);
    }
});

This works fine, but I cannot get the text to be selected. I can't seem to find a method which returns the component of a cell, and I've added a CellEditor I found online, but it doesn't change anything.

class MyCellEditor extends DefaultCellEditor {

    public MyCellEditor(JTextField textField) {
        super(textField);

        textField.addFocusListener(new FocusAdapter()  {
            public void focusGained(FocusEvent e ) {
                textField.selectAll();
            }
        });
    }
}

Added to the table with this:

for(int i = 0; i < table.getRowCount(); i++)
    table.setCellEditor(new MyCellEditor(new
              JTextField((String)table.getValueAt(i, 2))));

Things would be much easier if there were a way to omit the CellEditor altogether, but if there's something I've done like mess up the order these listeners process events, I don't mind including it. What I want to avoid is needing to write some huge CellEditor or massively overhaul any code just to add a tiny bit of convenience.

Try wrapping the selectAll() statement in a SwingUtilities.invokeLater(...) .

See: how do I simulate "onStartCellEditing" for DefaultCellEditor

for a general example of selecting text in any cell when cell editing is started:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM