Guest User

Swing button-label on click change

a guest
Mar 28th, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.95 KB | None | 0 0
  1.  
  2.  
  3. class MyButton extends JButton
  4. {
  5.     private JComponent comp_;
  6.  
  7.     public setComponent(JComponent comp)
  8.     {
  9.         comp_ = comp;
  10.     }
  11. }
  12.  
  13. class MyLabel extends JLabel
  14. {
  15.     private JComponent comp_;
  16.  
  17.     public setComponent(JComponent comp)
  18.     {
  19.         comp_ = comp;
  20.     }
  21. }
  22.  
  23. //===============================
  24.  
  25. MyLabel lbl = new MyLabel();
  26. MyButton btn = new MyButton();
  27. lbl.setComponent(btn);
  28. btn.setComponent(lbl);
  29. lbl.setVisible(false);
  30. btn.setVisible(true);
  31.  
  32. lbl.addActionListener(new ActionListener()
  33.     {
  34.         @Override
  35.         public void actionPerformed(ActionEvent e) {
  36.             comp_.setVisible(!comp_.isVisible());
  37.             setVisible(!isVisible());
  38.         }
  39.     });
  40. btn.addActionListener(new ActionListener()
  41.     {
  42.         @Override
  43.         public void actionPerformed(ActionEvent e) {
  44.             comp_.setVisible(!comp_.isVisible());
  45.             setVisible(!isVisible());
  46.         }
  47.     });
Advertisement
Add Comment
Please, Sign In to add comment