gpt4 book ai didi

java - 是否可以在 Java Netbeans 上制作渲染的 jTextArea "format editable"? (使用 Enter 和 Tab 键)

转载 作者:行者123 更新时间:2023-12-01 22:41:11 25 4
gpt4 key购买 nike

我在 jTable 单元格内渲染 jTextArea,渲染时,jTextArea 单元格使用“Enter”键作为行的接受和更改,而“Tab”键则转到下一个单元格(右侧的单元格)。我需要的是将它用作普通的 jTextArea,其中“Enter”是换行符,“Tab”是表格。我认为需要设置一些属性,但我不知道哪些属性。

这是我正在使用的代码:

    //Renderer

public class TableCellLongTextRenderer extends JTextArea implements TableCellRenderer{

@Override

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
this.setText((String)value);
this.setWrapStyleWord(true);
this.setLineWrap(true);


//set the JTextArea to the width of the table column
setSize(table.getColumnModel().getColumn(column).getWidth(),getPreferredSize().height);
if (table.getRowHeight(row) != getPreferredSize().height) {
//set the height of the table row to the calculated height of the JTextArea
table.setRowHeight(row, getPreferredSize().height);
}
return this;
}
}

提前致谢。

最佳答案

经过一番搜索,我终于得到了它,只需从另一个答案复制一些代码,并在我的示例中使用它,对于需要带有可编辑单元格的表格的任何人来说,这是最终代码。

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

只需复制这两个类,然后在表中使用它,如下所示:

    '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

关于java - 是否可以在 Java Netbeans 上制作渲染的 jTextArea "format editable"? (使用 Enter 和 Tab 键),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26102260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com