Hello everyone!

My idea is like this:
Class MainWindow is a JFrame object, on which there is a JButton object (named button1). When I hit that button, the second window (this window is a JFrame object that is instance of class SecondWindow) will appear with a JTextField object (named textField) inside and a JButton (named button2) object lying underneath. I enter the string “hello” on that textField and hit button2, some variable (that is declared like this boolean isConfirmed = false), that is created inside SecondWindow class is set to be true. Otherwise isConfirmed is set to be false (just by default). From class MainWindow (the object of this class is still running), I must retrieve that value of isConfirmed, so that I can do some stuff according to that value (like show a message confirming that you’ve typed “hello” or not).

And this is my attempt to do the thing:
First is the MainWindow.java

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.FlowLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
 

public class MainWindow extends JFrame {
 
	private JPanel contentPane;	
	private final JButton button1 = new JButton("Hit me");
 
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWindow mainFrame = new MainWindow();
					mainFrame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
 
	public MainWindow() {
		setTitle("Main Window");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 293, 90);
		setResizable(false);
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		contentPane.add(button1);
		
		button1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				boolean test = false;
				
				SecondWindow secondFrame = new SecondWindow();
				while(true)
				{
					test = secondFrame.getValueOfisConfirmed();
					if(test)
						break;
				}
				//do something here for example
				JOptionPane.showMessageDialog(null, "You've typed correctly", "Confirm information", 1);
			}
		});
		
	}
	
}

and here is SecondWindow.java

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
 
public class SecondWindow extends JFrame {
 
	private JPanel contentPane;
	private JTextField textField;
	private final JButton button2 = new JButton("OK");
	private boolean isConfirmed = false;
	
	public SecondWindow() {
		setTitle("Second Window");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(200, 200, 280, 90);
		setResizable(false);
		setVisible(true);
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		textField = new JTextField();
		contentPane.add(textField);
		textField.setColumns(15);
		
		contentPane.add(button2);
		
		button2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String textString = textField.getText();
                                //if the entered string equals "hello" then is confirmed is set to be true
				if( textString.equals("hello"))
				{
					isConfirmed = true;
				};
			}
		});
		
	}
	public boolean getValueOfisConfirmed()
	{
		return isConfirmed;
	}
	
}

And I didn't get what I expected. Afer hitting the Hit me button, the second window appeared but with some error.

I think the point is that the MainWindow object is always running, it doesn’t wait for me to type anything on JTextField on SecondWindow object.

Is there anyway to force MainWindow object to stop and wait until user type some text on JTextField on the second window?

Any help or suggestion would be appreciated!

Are you able to move line 47 where you are doing this to line 16 and just make the frame visible when the button is hit instead?

Could you also inform us of the error message you're receiving?

Thanks :)

This article has been dead for over six months. Start a new discussion instead.