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
.