我有一個JSpinner里面的JTable. 當我通過鍵盤輸入數值手動編輯欄位時,微調器不會采用輸入最后一位數字的值。例如,如果微調器中有一個 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();
}
}
uj5u.com熱心網友回復:
. 所有這些操作都在 keyPressed 事件中處理。
這是問題所在。
相反,您應該處理keyTyped事件。
或者更好的是,您應該使用DocumentListener. 即使將文本粘貼到文本欄位中,`DocumentListener 也將起作用。您應該始終設計您的 GUI,以便它可以與鍵盤和滑鼠一起使用。
閱讀 Swing 教程中關于如何撰寫 DocumentListener 的部分以獲取更多資訊和示例。
uj5u.com熱心網友回復:
如果您只想要一個僅接受 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();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/351447.html
上一篇:我可以在JavaJFrame中將多個相同型別的動作事件合并為一個嗎?
下一篇:java畫布隨機顯示不正確的影像
