Question

So I'm working on a research assignment with a gui and I have 3 boxes on the left panel "Input", "Processing", and "Display", if "Input" is selected using CardLayout then the first panel is shown on the right, if "Display" is selected then the last panel is displayed, but I can't get "Processing" to display in this part:

public void actionPerformed( ActionEvent event )   {
    // show first card
    if ( event.getSource() == controls[ 0 ] )    
        cardManager.first( deck ); 

    else if ( event.getSource() == controls[ 1 ] )    
        cardManager.show( card2Panel(), "c2");  
    // show previous card
    else if ( event.getSource() == controls[ 2 ] )
        cardManager.last( deck );

The code in it's enitre form:

import java.awt.*;

import java.awt.event.; import javax.swing.;

public class BookCentre extends JFrame implements ActionListener {

private CardLayout cardManager;
private JPanel deck;
private JButton controls[];
private String names[] = { "Input", "Processing", "Display"};

public BookCentre(){
    super( "CardLayout" );
    Container container = getContentPane();
    deck = new JPanel();
    cardManager = new CardLayout(); 
    deck.setLayout( cardManager );  
    deck.add( card1Panel(), "c1" );
    deck.add( card2Panel(), "c2" );
    deck.add( card3Panel(), "c3" );
    JPanel buttons = new JPanel();
    buttons.setLayout( new GridLayout( 2, 2 ) );
    controls = new JButton[ names.length ];
    for ( int count = 0; count < controls.length; count++ ) {
        controls[ count ] = new JButton( names[ count ] );
        controls[ count ].addActionListener( this );
        buttons.add( controls[ count ] );
        container.add( buttons, BorderLayout.WEST );
        container.add( deck, BorderLayout.EAST );
        setSize( 450, 200 );
        setVisible( true );}

}

public JPanel card1Panel(){ 
JLabel label1 = new JLabel( "card one",  SwingConstants.CENTER );
JPanel card1 = new JPanel();
card1.add( label1 ); 
return card1;

}

public JPanel card2Panel(){
    JLabel label2 = new JLabel( "card two", SwingConstants.CENTER );
    JPanel card2 = new JPanel();
    card2.setBackground( Color.yellow );
    card2.add( label2 );
    return card2;

}

public JPanel card3Panel(){
    JLabel label3 = new JLabel( "card three" );
    JPanel card3 = new JPanel();
    card3.setLayout( new BorderLayout() );  
    card3.add( new JButton( "North" ), BorderLayout.NORTH);
    card3.add( new JButton( "West" ), BorderLayout.WEST );
    card3.add( new JButton( "East" ), BorderLayout.EAST );
    card3.add( new JButton( "South" ), BorderLayout.SOUTH);
    card3.add( label3, BorderLayout.CENTER );
    return card3;

}

public void actionPerformed( ActionEvent event )   {
    // show first card
    if ( event.getSource() == controls[ 0 ] )    
        cardManager.first( deck ); 

    else if ( event.getSource() == controls[ 1 ] )    
        cardManager.show( card2Panel(), "c2");  
    // show previous card
    else if ( event.getSource() == controls[ 2 ] )
        cardManager.last( deck );           

}

public static void main( String args[] )   {
    BookCentre cardDeckDemo = new BookCentre();
    cardDeckDemo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );

} }

in public void ActionPerformed I was able to use cardManager.first, next, previous, last. I only want the "Processing" panel to be displayed though (so card2), not the user seeing first, last or cycling through all panels.

Was it helpful?

Solution

Check out the Java Trail for CardLayout. The cardManager.show() call in the ActionListener should be

cardManager.show( deck, "c2" );

as the first parameter is the parent container with a CardLayout, not the component that you want to display.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top