简体   繁体   English

有没有一种方法可以在没有CellEditor的情况下选择JTable中单元格中的所有文本?

[英]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? 简化版:给定一个带有扩展了DefaultTableModel的TableModel的JTable,是否可以在不使用CellEditor的情况下选择具有String类的单元格中的所有文本?

Long Version: I have made a TableModel which extends DefaultTableModel and should only have selectable rows be editable. 长版:我制作了一个TableModel,它扩展了DefaultTableModel,并且只应具有可编辑的可选行。 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. 据了解,DefaultTableModel将用字符串填充的单元格视为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). 选择一个可编辑的单元格后,我希望它设置其文本为文件名之前的文件名,然后选择所有文本(此后,用户执行的命令中每个文件都有可选的String字段,因此如果文件名是关联的可编辑单元格不为空,它使用该值。我们不希望用户每次设置此文件名时都必须重新输入整个文件名,但也希望使他们能够在需要时快速输入该文件名。

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. 该表具有一个MouseListener,它可以查看是否单击了可编辑的单元格,如果单击了它,还可以在单​​元格前填充文件名。

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. 我似乎找不到返回单元格组件的方法,并且添加了在网上找到的CellEditor,但它没有任何改变。

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. 如果可以完全省略CellEditor,事情会容易得多,但是如果我做过一些事情,例如弄乱了这些侦听器处理事件的顺序,我不介意包括它。 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. 我要避免的是需要编写一些巨大的CellEditor或对所有代码进行大修,以增加一点便利。

Try wrapping the selectAll() statement in a SwingUtilities.invokeLater(...) . 尝试将selectAll()语句包装在SwingUtilities.invokeLater(...)

See: how do I simulate "onStartCellEditing" for DefaultCellEditor 请参阅: 如何模拟DefaultCellEditor的“ onStartCellEditing”

for a general example of selecting text in any cell when cell editing is started: 有关在开始单元格编辑时在任何单元格中选择文本的一般示例:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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