Java make Button Invisible

After learning GUI creation through 8 steps, for more command over button event handling, apart this one, another two programs are given – Changing Button Label and Making Button Enabled.

Example on Java make Button Invisible

In the following program, three buttons are created with labels Red, Green and Blue. When Red button is clicked, the Blue button disappears and when Green button is clicked, the button appears back again. That is, Blue button appears and disappears.

Concept-wise program is simple, but emphasis is given to event handling and methods (like setVisible() and setFont() etc.) available for Button and how to use them.

import java.awt.*;
import java.awt.event.*;
public class ButtonInvisible extends Frame implements ActionListener
{
  Button redBut, greenBut, blueBut;
  public ButtonInvisible()
  {
    setLayout(new FlowLayout());    
   
    redBut = new Button("Red");
    greenBut = new Button("Green");
    blueBut = new Button("Blue");

    redBut.addActionListener(this);
    greenBut.addActionListener(this);
                                  // beautification to buttons
    redBut.setFont(new Font("Monospaced", Font.BOLD, 20));
    greenBut.setFont(new Font("SansSerif", Font.ITALIC, 20));
    blueBut.setFont(new Font("DialogInput", Font.BOLD+Font.ITALIC, 20));

    add(redBut); 
    add(greenBut); 
    add(blueBut); 
        
    setTitle("Button Enable Disable");
    setSize(300, 300);
    setVisible(true);
  }
  public void actionPerformed(ActionEvent e)
  {
    Object obj = e.getSource();
    Button btn = (Button) obj;
  
    if(btn == redBut)
        blueBut.setVisible(false);
    else if(btn == greenBut)
        blueBut.setVisible(true);
    }
    public static void main(String args[])
    {
      new ButtonInvisible();
    }
}

Java make Button Invisible
Output screenshot of Java make Button Invisible

Three buttons redBut, greenBut and blueBut are created and their fonts are changed with setFont() method. Observe, the blueBut is not linked with ActionListener as it does not have any events to handle in the code.

The button, user interacted, can be known in two ways – by its label or by its object. You have seen in Changing Button Label to distinguish using label of the button with getActionCommand() method. Now let us distinguish using the button object with getSource() method.

Object obj = e.getSource();
Button btn = (Button) obj;

The getSource() method of ActionEvent class returns the button object, clicked by the user, in the form of Object class. The object obj is explicitly type casted to Button object btn (a super class object, to assign to a subclass object, requires explicit casting). Now the btn object contains the reference of the button clicked by the user which is either redBut or greenBut.

The above two statements can be modified to a single statement by using anonymous object of Object class.

Button btn = (Button) e.getSource();

Programmers prefer the above style only.

blueBut.setVisible(false);

setVisible(false) method makes the button blueBut to disappear and setVisible(true) the button to appear again. Make a note of one point here; the button disappears (not visible) but exists on the frame. It is not removed from the frame. The frame you get do not close when clicked over the close icon on the title bar of the frame. It requires extra code close icon to work.

Leave a Comment

Your email address will not be published.