Java Tips Weblog

  • Blog Stats

    • 1,883,029 hits
  • Categories

  • Archives

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() ) );

frame.add(cards, BorderLayout.CENTER);
frame.add(south, BorderLayout.SOUTH);

The initial display would then look like the image below. Note how the previous button is initially disabled automatically.

Get The Code

RXCardLayout.java

See Also

Card Layout Focus

Related Reading

How to Use Card Layout
How to Use Actions

Advertisements

2 Responses to “Card Layout Actions”

  1. André Uhres said

    Hint: introducing a “name” parameter to the method getNextAction (and getPreviousAction as well) would make the method more generally usable. The relevant code within the body of the method should then be changed to:
    nextAction = new CardAction(name, true);
    nextAction.putValue(Action.MNEMONIC_KEY, (int)name.charAt(0));

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: