Card Layout Actions
Posted by Rob Camick on November 1, 2008
One usage for a CardLayout is to create a wizzard type application. That is a simple dialog that uses multiple panels and control is transferred to those panels by using Previous/Next buttons. In an application like this you would like the previous button to be disabled when displaying the first panel and the next button to be disabled when displaying the last panel.
Using the building blocks of the RXCardLayout class described in my last blog entry we will now add support for “next” and “previous” Actions. This was done by adding a few more methods to RXCardLayout:
- getNextAction(name) – this Action will invoke the CardLayout next(…) method. Specify the “name” of the Action. The first character of the name will be set up as the mnemonic for the Action
- getNextAction – convenience method that uses “Next” as the default name.
- getPreviousAction(name) – this Action will invoke the CardLayout previous(…) method. Specify the “name” of the Action. The first character of the name will be set up as the mnemonic for the Action
- getPreviousAction – convenience method that uses “Previous” as the default name.
The Actions can then be used to create a menu items, buttons or both. The state of the Actions will be managed by the RXCardLayout. That is, whenever a new card is displayed the Actions will be enabled/disabled appropriately. This means that your menu items or button will also be changed to reflect the state of its related Action.
Example code to build a wizzard type form would be something like:
RXCardLayout cl = new RXCardLayout();
JPanel cards = new JPanel(cl);
cards.add( card1 );
cards.add( card2 );
JPanel south = new JPanel(new FlowLayout(FlowLayout.RIGHT));
south.add( new JButton(cl.getPreviousAction() ) );
south.add( new JButton(cl.getNextAction() ) );
The initial display would then look like the image below. Note how the previous button is initially disabled automatically.