[英]JSpinner does not take the last digit entered
我在JTable
有一個JSpinner
。 當我通過鍵盤輸入數值手動編輯字段時,微調器不會采用輸入最后一位數字的值。 例如,如果微調器中有一個 1,我在前面輸入一個 2(它將是 21)我只取 1,如果然后我在前面輸入一個 3(它將是 321)我只take 21. 所有這些動作都在keyPressed
事件中處理。 這是整個班級的代碼。 順便說一句,如果在輸入數字后,我按回車鍵,我會很好地得到所有數字。
public class SpinnerEditor extends DefaultCellEditor
{
JSpinner spinner;
JSpinner.DefaultEditor editor;
JTextField textField;
boolean valueSet;
// Initializes the spinner.
public SpinnerEditor(DefaultTableModel modelo,JTable tabla) {
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();
}
});
spinner.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent e) {
try {
editor.commitEdit();
spinner.commitEdit();
} catch (ParseException ex) {
Logger.getLogger(SpinnerEditor.class.getName()).log(Level.SEVERE, null, ex);
}
if(tabla.getSelectedRow()>=0)
{
String precio = tabla.getValueAt(tabla.getSelectedRow(),2).toString();
int Iunidades = Integer.parseInt(spinner.getValue().toString());
double Dprecio=Double.parseDouble(precio);
double Total=Iunidades*Dprecio;
modelo.setValueAt(String.valueOf(Iunidades),tabla.getSelectedRow(), 7);
modelo.setValueAt(String.valueOf(Total),tabla.getSelectedRow(), 9);
}
}
});
((JSpinner.DefaultEditor)spinner.getEditor()).getTextField().addKeyListener(new KeyAdapter() {
//textField.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e)
{
char tecla=e.getKeyChar();
if(tecla>='0' && tecla<='9')
{
try {
editor.commitEdit();
spinner.commitEdit();
} catch (ParseException ex) {
Logger.getLogger(SpinnerEditor.class.getName()).log(Level.SEVERE, null, ex);
}
if(tabla.getSelectedRow()>=0)
{
String precio = tabla.getValueAt(tabla.getSelectedRow(),2).toString();
int Iunidades = Integer.parseInt(spinner.getValue().toString());
double Dprecio=Double.parseDouble(precio);
double Total=Iunidades*Dprecio;
modelo.setValueAt(String.valueOf(Iunidades),tabla.getSelectedRow(), 7);
modelo.setValueAt(String.valueOf(Total),tabla.getSelectedRow(), 9);
System.out.println(spinner.getValue().toString());
}
}
}
});
}
// 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();
}
}
. 所有這些操作都在 keyPressed 事件中處理。
這是問題所在。
相反,您應該處理keyTyped
事件。
或者更好的是,您應該使用DocumentListener
。 即使將文本粘貼到文本字段中,`DocumentListener 也將起作用。 您應該始終設計您的 GUI,以便它可以與鍵盤和鼠標一起使用。
閱讀 Swing 教程中關於如何編寫 DocumentListener 的部分以獲取更多信息和示例。
如果您只想要一個僅接受 int 值並充當表格單元格編輯器的微調器,則不需要所有自定義處理:
public class SpinnerEditor
extends AbstractCellEditor
implements TableCellEditor {
private static final long serialVersionUID = 1;
private final JSpinner spinner;
public SpinnerEditor() {
spinner = new JSpinner(
new SpinnerNumberModel(1, 0, Integer.MAX_VALUE, 1));
}
@Override
public Component getTableCellEditorComponent(JTable table,
Object value,
boolean selected,
int row,
int column) {
spinner.setValue(value);
return spinner;
}
@Override
public Object getCellEditorValue() {
return spinner.getValue();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.