2011-12-13 17 views
4

我已經看到了一些這樣做的例子,但我仍然無法理解,無法實現它。如何做cellable上的JTable選擇所有文本

我想要做什麼是對細胞變化(焦點),下一個選擇的單元格將所有的文本選擇,隨時供用戶完全改變它..

關於如何做到這一點任何想法?

// //更新莫名其妙 我設法拿出下面的類,但

實現這個
tblLayers.setDefaultEditor(String.class,新Classes.CellEditor());

不會產生任何結果,「尚未支持」。不投擲..

我應該如何解決這個問題?

import java.awt.Component; 
import java.util.EventObject; 
import javax.swing.JTable; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 
import javax.swing.event.CellEditorListener; 
import javax.swing.table.TableCellEditor; 


public class CellEditor extends JTextField implements TableCellEditor { 


public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { 
    //  final JTextField ec = (JTextField) editorComponent; 
    // 
    //  ec.setText((String) value); 
    // 
    //  // selectAll, so that whatever the user types replaces what we just put there 
    //  ec.selectAll(); 
    // 
    //  SwingUtilities.invokeLater(new Runnable() { 
    // 
    //   public void run() { 
    //    // make the component take the keyboard focus, so the backspace key works 
    //    ec.requestFocus(); 
    // 
    //    SwingUtilities.invokeLater(new Runnable() { 
    // 
    //     public void run() { 
    //      // at this point the user has typed something into the cell and we 
    //      // want the caret to be AFTER that character, so that the next one 
    //      // comes in on the RHS 
    //      ec.setCaretPosition(ec.getText().length()); 
    //     } 
    //    }); 
    //   } 
    //  }); 
    //  return editorComponent; 


    throw new UnsupportedOperationException("Not supported yet."); 
} 

public Object getCellEditorValue() { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 

public boolean isCellEditable(EventObject anEvent) { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 

public boolean shouldSelectCell(EventObject anEvent) { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 

public boolean stopCellEditing() { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 

public void cancelCellEditing() { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 

public void addCellEditorListener(CellEditorListener l) { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 

public void removeCellEditorListener(CellEditorListener l) { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 
} 

回答

5

關於editorComponent,我在哪裏初始化這個變量?

變量editorComponentDefaultCellEditor的字段。

而不是

class CellEditor extends JTextField implements TableCellEditor 

考慮

class CellEditor extends DefaultCellEditor 

然後,你可以做這樣的事情:

@Override 
public Component getTableCellEditorComponent(JTable table, 
     Object value, boolean isSelected, int row, int column) { 
    JTextField ec = (JTextField) editorComponent; 
    if (isSelected) { 
     ec.selectAll(); 
    } 
    return editorComponent; 
} 

附錄:作爲建議由@Edoz並在此完成example所示,您可以有選擇地重新排隊selectAll()當鼠標點擊開始編輯時。

JTable table = new JTable(model) { 

    @Override // Always selectAll() 
    public boolean editCellAt(int row, int column, EventObject e) { 
     boolean result = super.editCellAt(row, column, e); 
     final Component editor = getEditorComponent(); 
     if (editor == null || !(editor instanceof JTextComponent)) { 
      return result; 
     } 
     if (e instanceof MouseEvent) { 
      EventQueue.invokeLater(() -> { 
       ((JTextComponent) editor).selectAll(); 
      }); 
     } else { 
      ((JTextComponent) editor).selectAll(); 
     } 
     return result; 
    } 
}; 
11

我想要做的是在小區變更(焦點),下一個選擇的單元格將所有的文本選擇,隨時供用戶完全改變它..

解決方案取決於您的確切要求。一個JTable有一個渲染器和一個編輯器。

渲染通常只顯示單元格中的文本。如果您希望在開始輸入時替換文本,則需要執行兩項操作:

a)更改渲染器以「選定」狀態顯示文本,以便用戶知道鍵入字符將刪除現有文本 b)更改編輯器以在被調用時選擇所有文本

此方法相對困難,因爲您需要爲表中每個不同數據類型(即字符串,整數)使用自定義渲染器。

或者另一種方法是在獲取焦點時自動將每個單元格置於編輯模式,因此您只需更改編輯器以選擇文本。

這種方法很容易,你可以這樣做:

JTable table = new JTable(data, columnNames) 
{ 
    // Place cell in edit mode when it 'gains focus' 

    public void changeSelection(
     int row, int column, boolean toggle, boolean extend) 
    { 
     super.changeSelection(row, column, toggle, extend); 

     if (editCellAt(row, column)) 
     { 
      Component editor = getEditorComponent(); 
      editor.requestFocusInWindow(); 
      ((JTextComponent)editor).selectAll(); 
     } 
    } 
}; 
+0

可以將這種簡單的方法添加到NetBeans表中嗎?我的項目基本完成,但默認的NetBeans Palette JTable很糟糕。但該項目已經超出預算,所以我寧願不從頭開始重新設計它的一部分。我也看到了你的表格全選編輯器例子[here](http://tips4java.wordpress.com/2008/10/20/table-select-all-editor/),但不知道如何將它應用到一個NetBeans Palette JTable。 – jacknad

+0

@jacknad:camickr的洞察力文章也被引用[這裏](http://stackoverflow.com/a/4611413/230513)和[這裏](http://stackoverflow.com/a/8488735/230513)。龔向前,請考慮限制您使用的GUI編輯器[這裏](http://stackoverflow.com/a/2561540/230513)概述。 – trashgod

+0

不錯的解決方案。嘗試了其他幾個,但它沒有這麼好。我正在使用GlazedLists,上面的解決方案也適用於它。 – Daniel

相關問題