Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 1.82 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How do I allow buttons in one class to set the visibility of other panels in other classes?
  2. panelManager.add(typeSelectionView, TYPEVIEW);
  3.        
  4. cl.show(panelManager, newPanel);
  5.        
  6. public class CardLayoutDemo {
  7.  
  8.   private static JFrame createGUI(){
  9.  
  10.     JFrame testFrame = new JFrame(  );
  11.     testFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  12.  
  13.     List<String> layoutConstraints = Arrays.asList( "first", "second", "third");
  14.  
  15.     final JPanel contentsPane = new JPanel(  );
  16.     final CardLayout cardLayout = new CardLayout(  );
  17.     contentsPane.setLayout( cardLayout );
  18.  
  19.     //listener which will be used to switch between the different layouts
  20.     ActionListener listener = new ActionListener() {
  21.       public void actionPerformed( ActionEvent aActionEvent ) {
  22.         String constraint = aActionEvent.getActionCommand();
  23.         cardLayout.show( contentsPane, constraint );
  24.       }
  25.     };
  26.  
  27.     //add components to card layout with specific constraint
  28.     for ( String constraints : layoutConstraints ) {
  29.       contentsPane.add( new JLabel( constraints ), constraints );
  30.     }
  31.  
  32.     //create buttons allowing to switch between the different layouts
  33.     JPanel buttonPanel = new JPanel();
  34.     for ( int i = 0; i < layoutConstraints.size(); i++ ) {
  35.       String constraint = layoutConstraints.get( i );
  36.       JButton button = new JButton( "Layout " + i);
  37.       button.setActionCommand( constraint );
  38.       button.addActionListener( listener );
  39.       buttonPanel.add( button );
  40.     }
  41.  
  42.     testFrame.add( contentsPane, BorderLayout.CENTER );
  43.     testFrame.add( buttonPanel, BorderLayout.SOUTH );
  44.  
  45.     return testFrame;
  46.   }
  47.  
  48.   public static void main( String[] args ) {
  49.     EventQueue.invokeLater( new Runnable() {
  50.       public void run() {
  51.         JFrame frame = createGUI();
  52.         frame.pack();
  53.         frame.setVisible( true );
  54.       }
  55.     } );
  56.   }
  57. }