Hide buttons in Java Swing


 

Hide buttons in Java Swing

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

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");
      }
    });
  }
}    

Ads