0

I'm trying to make the TextField for my JSpinner uneditable in netbeans so the user can only use the up and down button to change the int value in the textfield of the JSpinner, how can I do this? an alternative would be for me to only allow int values to be typed into the box and not double values, right now if the user input is 21,5 then 215 points are added to that certain "Elevhem" in the database.

(the name of my JSpinner is 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");
            }
        }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

0

I implemented something along the lines of a slider using a JTextField, as my case required it remain a textField. It may be useful to you, though you may want something simpler than 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);
    }
}

Another implementation I had was key Bindings on a panel which absorbed most of the mouse listeners/key commands

/**
 * 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
    }
}
DarceVader
  • 98
  • 7