簡體   English   中英

有沒有一種方法可以在沒有CellEditor的情況下選擇JTable中單元格中的所有文本?

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

簡化版:給定一個帶有擴展了DefaultTableModel的TableModel的JTable,是否可以在不使用CellEditor的情況下選擇具有String類的單元格中的所有文本?

長版:我制作了一個TableModel,它擴展了DefaultTableModel,並且只應具有可編輯的可選行。 以下是相關部分:

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

據了解,DefaultTableModel將用字符串填充的單元格視為JTextFields。 我們使用此表的地方是用兩列填充它。 帶有文件名的不可編輯列(如字符串),其后是可編輯的空字符串列。 選擇一個可編輯的單元格后,我希望它設置其文本為文件名之前的文件名,然后選擇所有文本(此后,用戶執行的命令中每個文件都有可選的String字段,因此如果文件名是關聯的可編輯單元格不為空,它使用該值。我們不希望用戶每次設置此文件名時都必須重新輸入整個文件名,但也希望使他們能夠在需要時快速輸入該文件名。

該表具有一個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);
    }
});

這工作正常,但我無法選擇文本。 我似乎找不到返回單元格組件的方法,並且添加了在網上找到的CellEditor,但它沒有任何改變。

class MyCellEditor extends DefaultCellEditor {

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

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

與此添加到表中:

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

如果可以完全省略CellEditor,事情會容易得多,但是如果我做過一些事情,例如弄亂了這些偵聽器處理事件的順序,我不介意包括它。 我要避免的是需要編寫一些巨大的CellEditor或對所有代碼進行大修,以增加一點便利。

嘗試將selectAll()語句包裝在SwingUtilities.invokeLater(...)

請參閱: 如何模擬DefaultCellEditor的“ onStartCellEditing”

有關在開始單元格編輯時在任何單元格中選擇文本的一般示例:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM