-1

I want to add JButtons in JPanel using FlowLayout or any other layout and moving between the JButtons using Up and Down key..Having problem in moving up and down because i dont know the rows and columns of JButton.

public class abc{
List<ControlCenterButton>  buttons=getButtons();     
JPanel buttonPanel= new JPanel();
buttonPanel.setLayout(new FlowLayout)
for (int i = 0; i < buttons.size(); i++) {
            ControlCenterButton  button = buttons.get(i);
            if (button.getLayarID().equals(id)) {
                flag = true;
                button.addActionListener(this);
               button.setPosition(i);
                button.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mousePressed(MouseEvent mouseEvent) {
                        buttonPanel.requestFocusInWindow();
                    }
                });
                buttonPanel.add(button);
}

   buttonPanel.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent keyEvent) {
                final ControlCenterButton selectedControlCenterButton =       (ControlCenterButton) Session.getSession().getAttribute("controlcenter.selectedbutton");
                int position = selectedControlCenterButton.getPosition();
                int keycode = keyEvent.getKeyCode();
                switch (keycode) {
                    case KeyEvent.VK_RIGHT: {
                        if (position + 1 >= buttons.size()) {
                            ControlCenterButton.selectButton(buttons.get(0));
                        } else
                            ControlCenterButton.selectButton(buttons.get(position + ));
                        break;
                    }
             case KeyEvent.VK_UP: {
                            break;
                        }
                        case KeyEvent.VK_DOWN: {
                        }             
       case KeyEvent.VK_ENTER: {
                        selectedControlCenterButton.requestFocusInWindow();
                        new   ControlCenterButton().openApplication(selectedControlCenterButton);
                        break;
                    }
                }
 }
 public class ControlCenterButton extends JButton implements ActionListener {
 private int position;
 public int getPosition() {
      return position;
}

public void setPosition(int position) {
    this.position = position;
}
}
Nainisha
  • 1
  • 4

1 Answers1

-1
  1. Add a key listener to all buttons that listens on up and down key presses

  2. Add the x,y coordinates to the button. Ie. you could either extend JButton or have some kind of map that associates the coordinates with the JButton.

  3. When up and down is pressed then calculate where the focus should land, then after you figure out what button it should be. call requestFocusInWindow() on the button.

Oliver Watkins
  • 12,575
  • 33
  • 119
  • 225
  • 2
    *"call grabFocus() on the button."* Better to call [`requestFocusInWindow()`](http://docs.oracle.com/javase/7/docs/api/java/awt/Component.html#requestFocusInWindow%28%29). In fact, quoted from the JavaDocs .. *"This method is intended for use by focus implementations. **Client code should not use this method;** instead, it should use `requestFocusInWindow()`."* – Andrew Thompson Jul 08 '13 at 09:57
  • no - forget all about keyListeners, learn all key bindings :-) – kleopatra Jul 08 '13 at 10:22
  • @Dahaka - I had posted my code..I want to move Up and Down between JButton using Up Arrow and Down Arrow key.. I had used flowlayout so that if my screen size changes JButtons layout dont get effected.. I had tried with GridLAyout but JButtons overlaps with each other if screen size changes.. – Nainisha Jul 08 '13 at 11:54
  • @OliverWatkins :- I had posted my code..I had already added a listener for Up aand down key.. And already called requestFocusInWindow() – Nainisha Jul 08 '13 at 12:00
  • @OliverWatkins :- Thats what i m asking.. How could I associate the coordinates.. Because flowlayout arranges component using Location..Could u please provide some code? – Nainisha Jul 09 '13 at 06:02