gpt4 book ai didi

java - 使光标在 JTable 内的 JTextField 中可见,无需左键单击

转载 作者:太空宇宙 更新时间:2023-11-04 14:24:30 31 4
gpt4 key购买 nike

我正在尝试在 JTable 中编辑 JTextField,而无需左键单击单元格。看来,如果不左键单击单元格,就不会获得焦点,并且在 JTextField 中看不到光标。右键单击单元格应使 JTextField 可编辑,并且光标应可见并闪烁,准备在 JTextField 中键入。

我发现了很多有类似问题的人的例子,但他们的差异足够大,以至于我无法将任何解决方案应用于我的代码(也许缺乏经验和脑力?)。

下面的代码是真实代码的脏副本,我目前无法访问。如果我在 JTable 中显示 JTextField 的实现不正确,请告诉我。

任何帮助或建议将不胜感激。谢谢

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.LinkedList;
import java.util.List;
import javax.swing.AbstractCellEditor;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;


public class EditableJTextFieldInJTable implements MouseListener{

protected List<JTextField> textFields;
protected JTable table;

public EditableJTextFieldInJTable(){

textFields = new LinkedList<JTextField>();

JTextField tf1 = new JTextField("number 1");
tf1.setBackground(Color.yellow);
tf1.setEditable(false);
textFields.add(tf1);

JTextField tf2 = new JTextField("number 2");
tf2.setEditable(false);
tf2.setBackground(Color.green);
textFields.add(tf2);

TableModel tableModel = new TableModel();
table = new JTable(tableModel);
table.addMouseListener(this);
table.setDefaultRenderer(Object.class, new CellRenderer());
table.setDefaultEditor(Object.class, new CellEditor());
table.setTableHeader(null);

JFrame frame = new JFrame("Table Cell Edit Test");
frame.setPreferredSize(new Dimension(800, 480));
frame.getContentPane().add(new JScrollPane(table));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);

}


private class TableModel extends AbstractTableModel{

@Override
public int getColumnCount(){
return 1;
}

@Override
public int getRowCount(){
return textFields.size();
}

@Override
public Object getValueAt(int row, int col){
return textFields.get(row);
}

@Override
public boolean isCellEditable(int row, int col){
return textFields.get(row).isEditable();
}

}


public class CellRenderer implements TableCellRenderer{

public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int col){
return textFields.get(row);
}

}


public class CellEditor extends AbstractCellEditor implements TableCellEditor{

public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int col){
JTextField textField = textFields.get(row);
return textField;
}

@Override
public Object getCellEditorValue(){
return null;
}

}


@Override
public void mouseClicked(MouseEvent arg0){
if(arg0.getSource().equals(table)){
int row = table.rowAtPoint(table.getMousePosition());
System.out.println("row: " + row + " clicked.");
JTextField textField = textFields.get(row);
if(arg0.getButton() == MouseEvent.BUTTON3 && textField != null){
textField.setEditable(true);
System.out.println("row: " + row + " set editable. Caret should now be visible in cell.");
}
}

}


@Override
public void mouseEntered(MouseEvent arg0) {
// Empty method.
}


@Override
public void mouseExited(MouseEvent arg0) {
// Empty method.
}


@Override
public void mousePressed(MouseEvent arg0) {
// Empty method.
}


@Override
public void mouseReleased(MouseEvent arg0) {
// Empty method.
}

public static void main(String[] args){
new EditableJTextFieldInJTable();
}

}

最佳答案

所以经过坚持,我似乎找到了答案。首先,我必须让表格知道我要编辑单元格,然后请求 TextField 获得焦点。

@Override
public void mouseClicked(MouseEvent arg0){
if(arg0.getSource().equals(table)){
int row = table.rowAtPoint(table.getMousePosition());
System.out.println("row: " + row + " clicked.");
final JTextField textField = textFields.get(row);
if(arg0.getButton() == MouseEvent.BUTTON3 && textField != null){
textField.setEditable(true);
table.editCellAt(row, 0);
textField.requestFocus(); // 'textField' is the object within the cell.
textField.setCaretPosition(textField.getText().length());
System.out.println("row: " + row + " set editable. Caret should now be visible in cell.");
}
}
}

关于java - 使光标在 JTable 内的 JTextField 中可见,无需左键单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26817693/

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