- How do I allow buttons in one class to set the visibility of other panels in other classes?
- panelManager.add(typeSelectionView, TYPEVIEW);
- cl.show(panelManager, newPanel);
- public class CardLayoutDemo {
- private static JFrame createGUI(){
- JFrame testFrame = new JFrame( );
- testFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
- List<String> layoutConstraints = Arrays.asList( "first", "second", "third");
- final JPanel contentsPane = new JPanel( );
- final CardLayout cardLayout = new CardLayout( );
- contentsPane.setLayout( cardLayout );
- //listener which will be used to switch between the different layouts
- ActionListener listener = new ActionListener() {
- public void actionPerformed( ActionEvent aActionEvent ) {
- String constraint = aActionEvent.getActionCommand();
- cardLayout.show( contentsPane, constraint );
- }
- };
- //add components to card layout with specific constraint
- for ( String constraints : layoutConstraints ) {
- contentsPane.add( new JLabel( constraints ), constraints );
- }
- //create buttons allowing to switch between the different layouts
- JPanel buttonPanel = new JPanel();
- for ( int i = 0; i < layoutConstraints.size(); i++ ) {
- String constraint = layoutConstraints.get( i );
- JButton button = new JButton( "Layout " + i);
- button.setActionCommand( constraint );
- button.addActionListener( listener );
- buttonPanel.add( button );
- }
- testFrame.add( contentsPane, BorderLayout.CENTER );
- testFrame.add( buttonPanel, BorderLayout.SOUTH );
- return testFrame;
- }
- public static void main( String[] args ) {
- EventQueue.invokeLater( new Runnable() {
- public void run() {
- JFrame frame = createGUI();
- frame.pack();
- frame.setVisible( true );
- }
- } );
- }
- }