'Swing window not opening

I am creating a NotePad app in Java Swing but when I am trying to open a popup to set a title, it is not showing up. The class that calls the popup:

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

public class NewFile implements ActionListener{

  public static String title;
  
  
  
  public void actionPerformed(ActionEvent e){

    PopupFileName popup = new PopupFileName();

    
    /*try{
      Thread.sleep(30000);
    }catch (InterruptedException o){
      o.printStackTrace();
    }*/
    JTextArea titl = popup.title;
    title = titl.getText();
    try{
      File writer = new File(title+".txt");
      if(writer.createNewFile()){
        System.out.println("file created");
      }else{
        System.out.println("file exists");
      }
    }catch (IOException i) {
      System.out.println("An error occurred.");
      i.printStackTrace();
    }
  }
}

The popup class that is supposed to open:

import javax.swing.*;

public class PopupFileName{

  static JFrame popup = new JFrame("File Title");
  static JLabel titlel = new JLabel("Title:");
  static public JTextArea title = new JTextArea();
  
  public static void main(String[] args){

    popup.setSize(200,300);
    popup.setVisible(true);
    popup.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    popup.add(titlel);
    popup.add(title);
  }
}

Is there any way I can make it visible and make it able to get the text before it is created?



Solution 1:[1]

Start by taking a look at:

The problem with your ActionListener is, it's trying to present a window and then, immediately, trying to get some result from it. The problem is, the window probably isn't even present on the screen yet.

What you need is some way to "stop" the code execution until after the user responds. This is where a modal dialog comes in.

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            JButton btn = new JButton("Test");
            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String title = PopupFileName.getTitle(TestPane.this);
                    System.out.println(title);
                }
            });
            add(btn);
        }

    }

    public static class PopupFileName extends JPanel {

        private JLabel titlel = new JLabel("Title:");
        private JTextArea title = new JTextArea(20, 40);

        public PopupFileName() {
            setLayout(new BorderLayout());
            add(titlel, BorderLayout.NORTH);
            add(new JScrollPane(title));
        }

        public String getTitle() {
            return title.getText();
        }

        public static String getTitle(Component parent) {
            PopupFileName popupFileName = new PopupFileName();
            int response = JOptionPane.showConfirmDialog(parent, popupFileName, "Title", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
            switch (response) {
                case JOptionPane.OK_OPTION:
                    return popupFileName.getTitle();
                default: return null;
            }
        }
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MadProgrammer