Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Problem retreving data from another JFrame in Java

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem retreving data from another JFrame in Java

    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!


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Problem retreving data from another JFrame in Java

    Paste the full exception trace here.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  3. #3
    Member
    Join Date
    Feb 2012
    Posts
    58
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Problem retreving data from another JFrame in Java

    Quote Originally Posted by phabion View Post
    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!

    You can solve the problem by using some sort of waiting method for the SecondWindow. When the MainWindow opens the SecondWindow, it also calls the waiting method of the SecondWindow. The waiting method will stop when a button is clicked on the SecondWindow, and control is returned to the MainWindow.
    For waiting, use Thread.sleep() and a boolean flag indicates waiting status. Turn this flat on/off appropriately.

  4. #4
    Forum VIP
    Join Date
    Jun 2011
    Posts
    232
    My Mood
    Stressed
    Thanks
    36
    Thanked 66 Times in 54 Posts
    Blog Entries
    4

    Default Re: Problem retreving data from another JFrame in Java

    You might also want to research the observer design pattern.

Similar Threads

  1. Replies: 2
    Last Post: February 17th, 2012, 01:13 AM
  2. jmenu&jframe problem
    By beni.vd in forum AWT / Java Swing
    Replies: 1
    Last Post: January 2nd, 2011, 11:11 AM
  3. Java Gif image problem in JFrame
    By Zachary Wins in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 23rd, 2010, 08:51 PM
  4. Problem with a JFrame method
    By Lakmini Kuruppu in forum AWT / Java Swing
    Replies: 1
    Last Post: May 20th, 2010, 08:40 AM
  5. Change JFrame components problem
    By bruno88 in forum AWT / Java Swing
    Replies: 0
    Last Post: June 30th, 2009, 01:25 PM

Tags for this Thread