0

I have to develop some easy Java app with JCheckBoxes and JButtons. In order to finish, I have to control whenever I press the number keys (0 - 9) so that the JChechBox changes its status (notSelected <> selected).

I attach my code here. I have the window done, but when I press the keys, nothing happens...

HELP!!!

public class Window extends JFrame {

    public static JCheckBox check1;
    private static JCheckBox check2;
    private static JCheckBox check3;
    private static JCheckBox check4;
    private static JCheckBox check5;
    private static JCheckBox check6;
    private static JCheckBox check7;
    private static JCheckBox check8;
    private static JCheckBox check9;
    private static JCheckBox check10;

    public static void main( String[] args ){
        JFrame frame = new JFrame( "Window");
        frame.setSize( 600, 600 );
        frame.setVisible( true );
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

        JButton open = new JButton("OPEN"); 
        open.setLocation( 330, 340 );
        open.setSize( 85, 30 );
        frame.add( open ); 

        JButton save = new JButton("SAVE"); 
        save.setLocation( 480, 340 );
        save.setSize( 85, 30 );
        frame.add( save); 

        check1 = new JCheckBox("option1");
        check1.setLocation( 25, 60 );
        check1.setSize( 146, 30 );
        frame.add( check1 );


        check2 = new JCheckBox("option2"); 
        check2.setLocation( 25, 90 );
        check2.setSize( 146, 30 );
        frame.add( check2 );

        check3 = new JCheckBox("option3");
        check3.setLocation( 25, 120 );
        check3.setSize( 146, 30 );
        frame.add( check3 );

        check4 = new JCheckBox("option4");
        check4.setLocation( 25, 150 );
        check4.setSize( 146, 30 );
        frame.add( check4 );        

        check5 = new JCheckBox("option5");
        check5.setLocation( 25, 180 );
        check5.setSize( 146, 30 );
        frame.add( check5 );

        check6 = new JCheckBox("option6");
        check6.setLocation( 25, 210);
        check6.setSize( 146, 30 );
        frame.add( check6 );        

        check7 = new JCheckBox("option7");
        check7.setLocation( 25, 240 );
        check7.setSize( 146, 30 );
        frame.add( check7 );

        check8 = new JCheckBox("option8");
        check8.setLocation( 25, 270 );
        check8.setSize( 146, 30 );
        frame.add( check8 );

        check9 = new JCheckBox("option9");
        check9.setLocation( 25, 300 );
        check9.setSize( 146, 30 );
        frame.add( check9 );

        check10 = new JCheckBox("option10");
        check10.setLocation( 25, 330);
        check10.setSize( 146, 30 );
        frame.add( check10 );

        TextArea area = new TextArea();
        area.setLocation( 320, 150 );
        area.setSize(250, 180);
        frame.add( area );

        frame.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void keyReleased(KeyEvent arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void keyPressed(KeyEvent arg0) {
                int keyCode = arg0.getKeyCode();

                switch (keyCode) {
                case KeyEvent.VK_1:
                    check1.setSelected(true);
                    break;
                case KeyEvent.VK_2:
                    check2.setSelected(true);
                    break;
                case KeyEvent.VK_3:
                    check3.setSelected(true);
                    break;
                case KeyEvent.VK_4:
                    check4.setSelected(true);
                    break;
                case KeyEvent.VK_5:
                    check5.setSelected(true);
                    break;
                case KeyEvent.VK_6:
                    check6.setSelected(true);
                    break;
                case KeyEvent.VK_7:
                    check7.setSelected(true);
                    break;
                case KeyEvent.VK_8:
                    check8.setSelected(true);
                    break;
                case KeyEvent.VK_9:
                    check9.setSelected(true);
                    break;
                case KeyEvent.VK_0:
                    check10.setSelected(true);
                    break;
                }               
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Helena
  • 1
  • Did you search the forum first before asking this question. For example did you look at the threads found under the `Related` heading on the right side of the page? – camickr Dec 11 '14 at 20:30
  • 1) For Swing, we typically use [key bindings](https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) rather than the lower level `KeyListener`. 2) Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Dec 11 '14 at 22:33

2 Answers2

0

Easiest way would be to use JCheckBox.setMneumonic, though then the user would have to press alt-1, alt-2 etc. If your requirement is strict, I would recommend using the KeyBoardFocusManager and implementing a KeyEventDispatcher that would direct the key presses to click the checkboxes

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • `Easiest way....to click the checkboxes` - equals KeyBindigs in the case that we want to translate Key_Events to Swing – mKorbel Dec 11 '14 at 19:51
0

JavaDocs says:

To fire keyboard events, a component must have the keyboard focus.

In your situation keyboard focus is on TextArea.So, you must add KeyListener to Textarea

area.addKeyListener(new KeyListener(){
       //Your code
}

You can set focus on JFrame with frame.setFocusable(true) and it should work as soon as you'll change focus on TextArea.

Tkachuk_Evgen
  • 1,334
  • 11
  • 17