Java Code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JTabbedSwitch extends JApplet implements ActionListener
{
JTabbedPane pane = new JTabbedPane( );
JButton prev2 = new JButton( "Back" ), prev3 = new JButton( "Back" );
JButton next1 = new JButton( "Next" ), next2 = new JButton( "Next" );
public void init( )
{ // add listeners
prev2.addActionListener( this );
prev3.addActionListener( this );
next1.addActionListener( this );
next2.addActionListener( this );
// first pane : no previous tab, has next tab
JPanel panel1 = new JPanel( new FlowLayout() );
JLabel label1 = new JLabel( "Here is some fine text " );
panel1.add(label1);
panel1.add( next1 );
pane.add( "one", panel1 );
// second pane : has previous tab, has next tab
JPanel panel2 = new JPanel( new FlowLayout( ) );
JLabel label2 = new JLabel( "Whhoooooeeeee!" );
panel2.add( prev2 );
panel2.add(label2);
panel2.add( next2 );
pane.add( "two", panel2 );
// third pane : has previous tab, no next tab
JPanel panel3 = new JPanel( new FlowLayout( ) );
JLabel label3 = new JLabel( "yeaoswers!" );
panel3.add( prev3 );
panel3.add(label3);
pane.add( "three", panel3 );
add( pane ); // add to applet
}
/** handle events on the buttons, switching the current tab */
public void actionPerformed( ActionEvent ae )
{
Object obj = ae.getSource( ); // get the object that caused the event
// determine which tab index is currently selected in JTabbedPane
int tabindex = pane.getSelectedIndex( );
// check which object caused the event - either previous button
if ( obj == prev2 || obj == prev3 )
pane.setSelectedIndex( tabindex-1 ); // go back one tab
// check if the event was caused by a next button
else if ( obj == next1 || obj == next2 )
pane.setSelectedIndex( tabindex+1 ); // go forward one tab
}
}
©2006 by E.S.Boese. All Rights Reserved