後好了一些搜索,我最後得到它,只是從另一個答案複製了一些代碼,並在我的例子中使用它,最後的代碼是任何需要帶有可編輯單元格的表的人的最終代碼。
class TextAreaCellRenderer implements TableCellRenderer {
private JTextArea textArea = new JTextArea();
public TextAreaCellRenderer() {
super();
textArea.setLineWrap(true);
textArea.setBorder(BorderFactory.createEmptyBorder(1, 5, 1, 5));
}
@Override
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
if (isSelected) {
textArea.setForeground(table.getSelectionForeground());
textArea.setBackground(table.getSelectionBackground());
} else {
textArea.setForeground(table.getForeground());
textArea.setBackground(table.getBackground());
}
textArea.setFont(table.getFont());
textArea.setText((value == null) ? "" : value.toString());
return textArea;
}
}
class TextAreaCellEditor implements TableCellEditor {
private final JScrollPane scroll;
private JTextArea textArea = new JTextArea();
public TextAreaCellEditor() {
scroll = new JScrollPane(textArea);
scroll.setBorder(BorderFactory.createEmptyBorder());
//scroll.setViewportBorder(BorderFactory.createEmptyBorder());
textArea.setLineWrap(true);
textArea.setBorder(BorderFactory.createEmptyBorder(1, 5, 1, 5));
KeyStroke enter = KeyStroke.getKeyStroke(
KeyEvent.VK_ENTER, InputEvent.CTRL_MASK);
textArea.getInputMap(JComponent.WHEN_FOCUSED).put(enter, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
stopCellEditing();
}
});
}
@Override
public Object getCellEditorValue() {
return textArea.getText();
}
@Override
public Component getTableCellEditorComponent(
JTable table, Object value, boolean isSelected, int row, int column) {
System.out.println("2. getTableCellEditorComponent");
textArea.setFont(table.getFont());
textArea.setText((value != null) ? value.toString() : "");
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
textArea.setCaretPosition(textArea.getText().length());
textArea.requestFocusInWindow();
System.out.println("4. invokeLater: getTableCellEditorComponent");
}
});
return scroll;
}
@Override
public boolean isCellEditable(final EventObject e) {
if (e instanceof MouseEvent) {
return ((MouseEvent) e).getClickCount() >= 2;
}
System.out.println("1. isCellEditable");
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
if (e instanceof KeyEvent) {
KeyEvent ke = (KeyEvent) e;
char kc = ke.getKeyChar();
if (Character.isUnicodeIdentifierStart(kc)) {
textArea.setText(textArea.getText() + kc);
System.out.println("3. invokeLater: isCellEditable");
}
}
}
});
return true;
}
//Copid from AbstractCellEditor
protected EventListenerList listenerList = new EventListenerList();
transient protected ChangeEvent changeEvent = null;
@Override
public boolean shouldSelectCell(EventObject e) {
return true;
}
@Override
public boolean stopCellEditing() {
fireEditingStopped();
return true;
}
@Override
public void cancelCellEditing() {
fireEditingCanceled();
}
@Override
public void addCellEditorListener(CellEditorListener l) {
listenerList.add(CellEditorListener.class, l);
}
@Override
public void removeCellEditorListener(CellEditorListener l) {
listenerList.remove(CellEditorListener.class, l);
}
public CellEditorListener[] getCellEditorListeners() {
return listenerList.getListeners(CellEditorListener.class);
}
protected void fireEditingStopped() {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == CellEditorListener.class) {
// Lazily create the event:
if (changeEvent == null) {
changeEvent = new ChangeEvent(this);
}
((CellEditorListener) listeners[i + 1]).editingStopped(changeEvent);
}
}
}
protected void fireEditingCanceled() {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == CellEditorListener.class) {
// Lazily create the event:
if (changeEvent == null) {
changeEvent = new ChangeEvent(this);
}
((CellEditorListener) listeners[i + 1]).editingCanceled(changeEvent);
}
}
}
}
只需要複製這些2班,並在表中使用它,就像這樣:
'jTableName'.getColumnModel().getColumn('number of column you need to set editable').setCellRenderer(new 'principal class name'.TextAreaCellRenderer());
'jTableName'.getColumnModel().getColumn('number of column you need to set editable').setCellEditor(new 'principal class name'.TextAreaCellEditor());
只需更改的註釋部分,這就是它,希望它可以幫助別人喜歡它幫助我,我用這個鏈接作爲基礎。 Custom CellEditor with JScrollPane - start editing issue
您需要使用JTextArea作爲編輯器組件創建自定義單元格編輯器。 – 2014-09-29 14:27:47