如何使JSpinner中的TextField不可编辑,以便用户只能使用向上和向下按钮?

时间:2018-12-31 18:12:10

标签: java sql swing jspinner

我正在尝试使JSpinner的TextField在netbeans中不可编辑,因此用户只能使用向上和向下按钮来更改JSpinner文本字段中的int值,我该怎么做? 对于我来说,另一种选择是只允许将int值键入到框中,而不允许使用double值,现在,如果用户输入为21.5,则将215点添加到数据库中的该“ Elevhem”。 >

(我的JSpinner的名称是antalPoang)

if (Validering.isPositivtSpinner(antalPoang)) {
            try {
                String elevhemsNamn = (String) namnElevhem.getSelectedItem();
                int points = (int) antalPoang.getValue();
                String query = "UPDATE ELEVHEM SET HUSPOANG = HUSPOANG + " + points + " WHERE ELEVHEMSNAMN ='" + elevhemsNamn +"'";

                if (namnElevhem.getSelectedItem().equals("Gryffindor")) {
                    db.update(query);
                    ImageIcon icon = new ImageIcon("Gryffindor.png");
                    JOptionPane.showMessageDialog(null, "Poängen har lagts till", " ", JOptionPane.INFORMATION_MESSAGE, icon);
                    antalPoang.setValue(0);



                } else if (namnElevhem.getSelectedItem().equals("Ravenclaw")) {
                    db.update(query);
                    ImageIcon icon = new ImageIcon("Ravenclaw.png");
                    JOptionPane.showMessageDialog(null, "Poängen har lagts till", " ", JOptionPane.INFORMATION_MESSAGE, icon);
                    antalPoang.setValue(0);

                } else if (namnElevhem.getSelectedItem().equals("Slytherin")) {
                    db.update(query);
                    ImageIcon icon = new ImageIcon("Slytherin.png");
                    JOptionPane.showMessageDialog(null, "Poängen har lagts till", " ", JOptionPane.INFORMATION_MESSAGE, icon);
                    antalPoang.setValue(0);

                } else if (namnElevhem.getSelectedItem().equals("Hufflepuff")) {
                    db.update(query);
                    ImageIcon icon = new ImageIcon("Hufflepuff.png");
                    JOptionPane.showMessageDialog(null, "Poängen har lagts till", " ", JOptionPane.INFORMATION_MESSAGE, icon);
                    antalPoang.setValue(0);
                }

            } catch (InfException e) {
                JOptionPane.showMessageDialog(null, "Något blev fel");
            }
        }

1 个答案:

答案 0 :(得分:0)

我使用JTextField沿着滑块的方式实现了一些功能,因为我的情况要求它仍然是textField。尽管您可能想要比Object[] obj

更简单的方法,但它可能对您有用。
/**
 * 
 * @param jTextField  field to listen on
 * @param minLimit    minimum value for field
 * @param maxLimit    maximum value for field
 * @param obj         Generic object to return integer value 
 */
public static void variableDragListener(JTextField jTextField, int minLimit, int maxLimit, Object[] obj,
        Consumer<Object> setVarMethod) {

jTextField.addMouseMotionListener(new MouseMotionAdapter() {
    int lastMouseX;

    @Override
    public void mouseDragged(MouseEvent mouse) {
        if (mouse.getX() != lastMouseX) {
            int step = mouse.getX() > lastMouseX ? 1 : -1;

            if (obj instanceof Integer[]) {
                acceptBoundedIntMethod(step, (Integer) obj[0], minLimit, maxLimit, setVarMethod);
            }
            lastMouseX = mouse.getX();
        }
    }
});
}

private static void acceptBoundedIntMethod(int step, Integer variable, int minLimit, int maxLimit,
        Consumer<Object> setVarMethod) {
    if (minLimit <= variable + step && maxLimit >= variable + step) {
        setVarMethod.accept(variable + step);
    }
}

我的另一个实现是面板上的按键绑定,它吸收了大多数鼠标侦听器/键盘命令

/**
 * Modified from
 * https://stackoverflow.com/questions/8482268/java-keylistener-not-called
 * <p>
 * For a JPanel, set Key Bindings to execute consumer method on KeyBinding activation
 *
 * @param actionMap   JPanel actionMap
 * @param inputMap    JPanel inputMap
 * @param keyBindings UserDefined Key->Method binding
 */
public static void setKeyBindings(ActionMap actionMap, InputMap inputMap,
        Map<Integer, Consumer<Object>> keyBindings) {

    for (int keyCode : keyBindings.keySet()) {

        String vkCode = KeyEvent.getKeyText(keyCode);
        inputMap.put(KeyStroke.getKeyStroke(keyCode, 0), vkCode);

        actionMap.put(vkCode, new KeyAction(vkCode, keyBindings.get(keyCode)));
    }
}

/**
 * Modified from
 * https://stackoverflow.com/questions/8482268/java-keylistener-not-called
 * <p>
 * Action to execute consumer method when KeyBinding is activated
 */
public static class KeyAction extends AbstractAction {
    private final Consumer<Object> method;

    public KeyAction(String actionCommand, Consumer<Object> method) {
        this.method = method;
        putValue(ACTION_COMMAND_KEY, actionCommand);
    }

    @Override
    public void actionPerformed(ActionEvent actionEvt) {
        method.accept(new Object());
    }
}

public class GraphicPanel extends JPanel {

    private final Map<Integer, Consumer<Object>> keyBindings = new HashMap<>();

    {
        keyBindings.put(KeyEvent.VK_ENTER, this::submitGamepiece);
    }

    public GraphicPanel() {
        setKeyBindings(getActionMap(), getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW), keyBindings);
    }

    private void submitGamepiece(Object o) {
        ...
        // method stuff here
    }
}