Java 使用类似JSpinner的JTable单元格编辑器
我使用的JSpinner就像表格单元格编辑器,我有一个恼人的问题: 在我点击单元格之前,单元格一直处于不可编辑模式,对于“不可编辑”,我的意思是我无法写入单元格(它没有焦点,因此不接受键盘输入),但我可以使用(键盘的)上下箭头更改值 那么,当选择表格单元格时,我要做什么才能在按下某个键后立即将其聚焦 除了这个问题,我的SpinnerEditor类工作得很好Java 使用类似JSpinner的JTable单元格编辑器,java,swing,focus,tablecelleditor,jspinner,Java,Swing,Focus,Tablecelleditor,Jspinner,我使用的JSpinner就像表格单元格编辑器,我有一个恼人的问题: 在我点击单元格之前,单元格一直处于不可编辑模式,对于“不可编辑”,我的意思是我无法写入单元格(它没有焦点,因此不接受键盘输入),但我可以使用(键盘的)上下箭头更改值 那么,当选择表格单元格时,我要做什么才能在按下某个键后立即将其聚焦 除了这个问题,我的SpinnerEditor类工作得很好 谢谢大家。好的,我现在的解决方案是: spinner.addFocusListener(new FocusListener() {
谢谢大家。好的,我现在的解决方案是:
spinner.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
// editor is ((JSpinner.DefaultEditor)spinner.getEditor()).getTextField();
// necessary to set focus on the text component of jspinner
editor.requestFocus();
// this if you want to select the displayed text of jspinner
SwingUtilities.invokeLater(new Runnable() {
public void run() {
System.out.println("run");
editor.selectAll();
}
});
}
public void focusLost(FocusEvent e) {}
});
这里有一个完整的例子。它不仅仅是将JSpinner放在表中,这样您就可以阅读它并获取所需的内容。我发现Blow的答案不完整,对我来说不起作用,所以这里有一些你可以编译和运行的东西
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class JSpinnerInTables {
static String[] columnNames = {
"Name","Value"
};
static Object[][] data = {
{"one",1.0},
{"two",2.0}
};
public static void main( String[] args ) {
JFrame frame = new JFrame();
JTable table = new JTable(data,columnNames);
//table.setSurrendersFocusOnKeystroke(true);
TableColumnModel tcm = table.getColumnModel();
TableColumn tc = tcm.getColumn(1);
tc.setCellEditor(new SpinnerEditor());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(table);
frame.pack();
frame.setVisible(true);
}
public static class SpinnerEditor extends DefaultCellEditor
{
JSpinner spinner;
JSpinner.DefaultEditor editor;
JTextField textField;
boolean valueSet;
// Initializes the spinner.
public SpinnerEditor() {
super(new JTextField());
spinner = new JSpinner();
editor = ((JSpinner.DefaultEditor)spinner.getEditor());
textField = editor.getTextField();
textField.addFocusListener( new FocusListener() {
public void focusGained( FocusEvent fe ) {
System.err.println("Got focus");
//textField.setSelectionStart(0);
//textField.setSelectionEnd(1);
SwingUtilities.invokeLater( new Runnable() {
public void run() {
if ( valueSet ) {
textField.setCaretPosition(1);
}
}
});
}
public void focusLost( FocusEvent fe ) {
}
});
textField.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent ae ) {
stopCellEditing();
}
});
}
// Prepares the spinner component and returns it.
public Component getTableCellEditorComponent(
JTable table, Object value, boolean isSelected, int row, int column
) {
if ( !valueSet ) {
spinner.setValue(value);
}
SwingUtilities.invokeLater( new Runnable() {
public void run() {
textField.requestFocus();
}
});
return spinner;
}
public boolean isCellEditable( EventObject eo ) {
System.err.println("isCellEditable");
if ( eo instanceof KeyEvent ) {
KeyEvent ke = (KeyEvent)eo;
System.err.println("key event: "+ke.getKeyChar());
textField.setText(String.valueOf(ke.getKeyChar()));
//textField.select(1,1);
//textField.setCaretPosition(1);
//textField.moveCaretPosition(1);
valueSet = true;
} else {
valueSet = false;
}
return true;
}
// Returns the spinners current value.
public Object getCellEditorValue() {
return spinner.getValue();
}
public boolean stopCellEditing() {
System.err.println("Stopping edit");
try {
editor.commitEdit();
spinner.commitEdit();
} catch ( java.text.ParseException e ) {
JOptionPane.showMessageDialog(null,
"Invalid value, discarding.");
}
return super.stopCellEditing();
}
}
}
我也有同样的问题,但你的解决方案对我不起作用。focusGain方法从未被调用。我准确地复制了您的代码,创建了一个名为“editor”的字段,并将其指定给您要使用的值。设置JTable和CellEditor的其余代码是什么样子的?