0

I've just started coding in java swing today, so I'm very newbie and sorry if my question is silly. I have searched the net a lot but nothing was found.

My problem is that I can't make a Jfraim invisible by setVisible(false).

The code is very simple. a window with just a button that after being clicked it will show a showMessageDialog "Hello World" and I want the window to be invisible after that.

here is my code:

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

public class Temp extends JFrame{
    private JPanel panel1;
    private JButton button1;

    private Temp() {
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                JOptionPane.showMessageDialog(null, "Hello World");
            }
        });
    }


    public static void main(String[] args) {
        JFrame tempWindow = new JFrame("TempWindow");
        tempWindow.setContentPane(new Temp().panel1);
        tempWindow.setLocationRelativeTo(null); // this line set the window in the center of the screen
        tempWindow.setDefaultCloseOperation(tempWindow.EXIT_ON_CLOSE);
        tempWindow.pack();
        tempWindow.setVisible(true);

    }

}

I don't know what I'm doing wrong. I did everything just like this youtube video but my window won't get invisible after clicking the button.

any help would be appreciated.

Peyman
  • 3,097
  • 5
  • 33
  • 56
  • 1
    The given code will not even run. You never initiate `panel1` and `button1`, hence NPE will be thrown. – George Z. May 23 '19 at 16:48
  • @GeorgeZ. but it is running for me. I use intelliJ GUI form, does this effect? – Peyman May 23 '19 at 16:50
  • 1
    First, don't extend JFrame as you already create one in main (which is probably part of your problem). And where do you create the button? – WJS May 23 '19 at 16:51
  • Try to run as java program, it does not run as @GeorgeZ. said. I will suggest search some sample code and try to run them. – Sambit May 23 '19 at 16:52
  • @WJS I just drag the button in intellij GUI from. it is working for me and will be run. – Peyman May 23 '19 at 16:54
  • @Peymanmohsenikiasari Check again your run configurations in your IDE and make sure you are running this specific piece of code you gave us. – George Z. May 23 '19 at 16:54
  • @GeorgeZ. yes, it is. it will show a window with a button and after clicking that, it will pup up a "hello world" window. the problem is that the first window won't be invisible. – Peyman May 23 '19 at 16:56
  • 1
    See [The use of multiple JFrames, Good / Bad practice?](https://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-or-bad-practice) (Bad). You should be using `JDialogs` or `CardLayout`. – Frakcool May 23 '19 at 17:10

1 Answers1

2

Try this.

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

public class Temp {
   private JPanel  panel1;
   private JButton button1;

   JFrame          tempWindow = new JFrame("TempWindow");

   private Temp() {
      button1 = new JButton("Button");
      tempWindow.add(button1);
      button1.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
            tempWindow.setVisible(false);
            JOptionPane.showMessageDialog(null, "Hello World");
         }
      });
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(() -> new Temp().start());

   }

   public void start() {
      tempWindow.setLocationRelativeTo(null); // this line set the window in the
                                              // center of the screen
      tempWindow.setPreferredSize(new Dimension(500, 500));
      tempWindow.setDefaultCloseOperation(tempWindow.EXIT_ON_CLOSE);
      tempWindow.pack();
      tempWindow.setLocationRelativeTo(null); // centers on screen
      tempWindow.setVisible(true);

   }

}
  • I deleted the panel because it wasn't necessary to demonstrate the solution
  • I also created a button since you didn't
  • It is also considered bad practice to extend JFrame. It is better to extend JPanel and place an instance inside the JFrame instance. But if you're not going to override something, best to favor composition over inheritance.
WJS
  • 36,363
  • 4
  • 24
  • 39