【发布时间】:2021-12-20 03:14:49
【问题描述】:
我在JTable 中有一个JSpinner。当我通过键盘输入数值手动编辑该字段时,微调器不会采用输入最后一位数字的值。例如,如果微调器中有一个 1,我在前面输入一个 2(这将是 21),我只取 1,如果我在前面输入一个 3(它会是 321)我只采取 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();
}
}
【问题讨论】:
-
自定义微调器编辑器的用途是什么?另外,请考虑在您的问题中创建和发布minimal reproducible example。
标签: java swing events jspinner