當我嘗試做一個JSpinner的不可編輯通過鍵盤或鼠標這樣的:如何禁用JSpinner的鍵盤和鼠標輸入?
((DefaultEditor) mySpinner.getEditor()).getTextField().setEditable(false);
mySpinner.setEnabled(false);
它禁止任何鍵盤輸入和粘貼,但我仍然可以點擊上/下按鈕更改數值。
如何禁用上/下按鈕?
當我嘗試做一個JSpinner的不可編輯通過鍵盤或鼠標這樣的:如何禁用JSpinner的鍵盤和鼠標輸入?
((DefaultEditor) mySpinner.getEditor()).getTextField().setEditable(false);
mySpinner.setEnabled(false);
它禁止任何鍵盤輸入和粘貼,但我仍然可以點擊上/下按鈕更改數值。
如何禁用上/下按鈕?
如果微調器使用JSpinner.DefaultEditor或其子類,則以下代碼有效(禁用鍵盤導航,微調按鈕不起作用,但可以選擇並複製微調器中顯示的值)。
JSpinner component = ...;
component.setEnabled(false);
if (component.getEditor() instanceof JSpinner.DefaultEditor) {
JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) component.getEditor();
editor.getTextField().setEnabled(true);
editor.getTextField().setEditable(false);
}
如果微調與其他的東西,然後JTextComponent的自定義編輯器,那麼它很可能還是可以使用同樣的方法(禁用微調,重新啓用微調編輯器使用的實際分量,標誌是組件只讀使用其API)。
// Disabling mouse input without desabling the JSpinner itself
JSpinner spinner = ...;
// set the minimum and maximum values to the current value,
// thus preventing changes to the spinner's current value
SpinnerNumberModel snm = (SpinnerNumberModel) spinner.getModel();
snm.setMinimum((Integer)spinner.getValue());
snm.setMaximum((Integer)spinner.getValue());