Share on Google+Share on Google+

Hide buttons in Java Swing

Advertisement
In this section, you will learn how to hide buttons using java swing.

Hide buttons in Java Swing

In this section, you will learn how to hide buttons using java swing. For this, we have created three buttons, A, B and C. We have performed an action on C. When you click the button 'C', buttons A and B will get disappeared.

Here is the code:

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

public class HideButton {
 public static void main(String[] args) {
    JFrame f = new JFrame("JFrame");
    final JPanel p1 = new JPanel();
     JButton a = new JButton("A");
  JButton b = new JButton("B");
    p1.add(a);
    p1.add(b);
    f.getContentPane().add(p1);

    final JPanel p2 = (JPanelf.getGlassPane();
    p2.setVisible(true);
    p2.setLayout(new GridBagLayout());
    JButton c = new JButton("C");
    p2.add(c);

    f.setSize(150100);
    f.setVisible(true);
    c.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        p1.setVisible(false);
        p1.repaint();
      }
    });
       a.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
       JOptionPane.showMessageDialog(null,"Click Button B");
      }
    });
  }
}    

Advertisements

Advertisement

Posted on: October 23, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.