Could you please help me with something I am trying to acheive. I am using Java 6 and Eclipse 3.5.0.

I am new to programming generally and have created two java files. Driver loads the UserForm and will go on to control a number of other files eventually. UserForm is just a really basic form to allow data entry which I will tidy up later.

As it stands, when Driver is run, UserForm is called and run, the user can input two numbers for e.g "1" and "5", which (and this is the bit I am struggling with) I then need to send to the Driver script when OK is clicked.

Can anyone help please?

PS I could also do with some way to shut down the UserForm without closing the whole program, ideally after UserForm has been closed, it would relinquish control back to Driver, but I am not sure how to do this either. - any thoughts?

Thanks a lot.
phil.

public class Driver
{

	
	
	public static void main(String[] args)
	{
	
	//brings in UserForm class and sets it up to be displayed by Driver 
	UserFormNew userformnew = new UserFormNew();
	userformnew.setVisible(true);	
	}
}

And

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


public class UserFormNew extends JFrame
{
	
private JTextField textFieldHigh;
private JTextField textFieldLow;
public String strLow;
public String strHigh;
public int intrecordnolow;
public int intrecordnohigh;

	//This actually builds form using the methods I have put in private voids
	public UserFormNew()
	{				
		createTextFields();
		createOKButton();
		setSize(300,400);
		setTitle("test");
		setDefaultCloseOperation(EXIT_ON_CLOSE);	
	}
	
	private void createTextFields()
	{
		textFieldHigh = new JTextField(10);
		add(textFieldHigh, BorderLayout.NORTH);
		textFieldLow = new JTextField(10);
		add(textFieldLow, BorderLayout.SOUTH);	
	}
	
	
	public void createOKButton()
	{
		JButton OkBtn = new JButton("OK");
		add(OkBtn, BorderLayout.EAST);
		
		class ClsOKBtn implements ActionListener
		{
			public void actionPerformed(ActionEvent event)
			{
				//do something when action performed
				strLow = textFieldLow.getText();
				strHigh = textFieldHigh.getText();	
				System.out.print(strHigh);
				System.out.print(strLow);				
				return;
			}
			
		}
		//Need to add listener to ok button object here
		ActionListener listener = new ClsOKBtn();
		OkBtn.addActionListener(listener);
	}
			
			
	}
VernonDozier commented: Code tags on first post. +23

I don't know if it's a matter of "sending" data from one object to another (by the way, you don't HAVE a Driver object at the moment. Everything's in main, which is static). I'd imagine that it's a matter of having the two objects know how to find each other and access each other's data members, or at least have the Driver object know about the UserFormNew object, which it will, since it created it in the first place. So you need to set up public getstrLow () and getstrHigh () methods in UserFormNew since you have them as private.

You have another problem as far as "returning control to Driver". The problem is that Driver will create the UserFormNew object and then keep right on going to the next line. It won't wait for any buttons to be clicked because the UserFormNew object extends JFrame and thus isn't modal. So the Driver code will just keep on executing. If you want the code in Driver to stop temporarily, make the UserFormNew modal or find some other way to make Driver wait. I know of no way to make a JFrame modal. There may be a way, but you also may want to change from a JFrame to a JDialog, in which case making it modal is quite easy.

If you do that, the code in Driver will pause when it creates the JDialog object and pick up where it left off when that JDialog object ceases to be visible.

hey, ok vernon provided you with an almost complete answer. I dont know if you understand yet that methods that are private cannot be accessed outside the class. It is also bad programing to make everything public. Therefore you should employ 'getters' and 'setters'. Setters make a variable something and getters essentially get the value of the variable. So you need to make public getstrLow () and getstrHigh () methods in UserFormNew (Like vernon said). These are known as getters. Getters are public and setters are private.

Can I just say that I have posted on a few forums now and this is the only one I have found that gave a good qualified answer. Over the next week or so I will take these two answers and try and use them to cobble something together, thanks a millions for the pointers both I will repost with success/failure as soon as I can in the future..
phil.

Please see above reply - applies to you to.
ta
phil

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.