0

I need to switch in two ways with Key Bindings. Here is my code, it works one way. Can somebody help me make it work both ways?

public class MyFrame extends JFrame {
    private FirstForm firstForm = new FirstForm();
    private SecondForm secondForm = new SecondForm();

    public MyFrame(){
        setContentPane(firstForm);
        setSize(800,600);
        firstForm.addActionListenerForSomeAction(switcher(secondForm));
        secondForm.addActionListenerForSomeAction(switcher(firstForm));

        keyboardSwitcher(firstForm);
        keyboardSwitcher(secondForm);

    }

    public void keyboardSwitcher(JPanel panel){

        InputMap imap = getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        imap.put(KeyStroke.getKeyStroke("ctrl ENTER"),"switchAction");

        ActionMap amap = getRootPane().getActionMap();
        amap.put("switchAction",switcher(panel));
    }

    public Action switcher(JPanel panel){
        Action action = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setContentPane(panel);
                getContentPane().revalidate();
                getContentPane().repaint();
            }
        };
        return action;
    }
}

FirstForm and SecondForm are my own classes that extend JPanel.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Arseniy
  • 3
  • 2
  • 1
    1) Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Feb 18 '17 at 15:59
  • Thank you. It is my first post, I will use your guidance in future – Arseniy Feb 19 '17 at 11:20
  • *"I will use your guidance in future "* Cool. In that case, I might put more though into your questions ..in future. Of course, you might [edit] the question *now.* – Andrew Thompson Feb 19 '17 at 11:27

1 Answers1

1

Your keyboardSwitcher(...) method is wrong. The second time you call the method you are just replacing the first binding. You can't have two bindings for the same KeyStroke.

You correctly pass a JPanel to the method, but then you never use the JPanel to set the key bindings.

So instead of setting the bindings on the root pane you need to set the bindings on each panel separately.

You would use the following InputMap:

InputMap im = panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);

Also, to swap panels you should be using a CardLayout. This layout manager will allow you to swap between multiple panels added to the CardLayout. Check out the section from the Swing tutorial on How to Use CardLayout for a working example.

Now when you create your Switcher Action you will also need to know the name of the panel you want to switch to.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thank you for your answer. I have a task make 2 form switching via KeyBindings in two ways (ctrl ENTER) I need to use GridBagLayout and RootPane in Input/Action maps, does it posible? – Arseniy Feb 19 '17 at 11:14
  • @Arseniy, I already suggested you can't have multiple bindings for the same KeyStroke. It you want to add the binding to the root pane then you can only have a single binding. I think if you use a CardLayout you can just use the `next(...)` method. to swap to the other panel. The Action will need to be smarter. The Action will need to know which panel is currently visible and then display the other panel. The child panels can use what layout you want. The parent panel uses the CardLayout. Read the tutorial, download the examples and play with the demo code to understand the concept!!! – camickr Feb 19 '17 at 20:58
  • Now I'm understand, Thank you very mutch – Arseniy Feb 20 '17 at 08:36