How should I pass a value from a JFrame to another?
I have this logical problem regarding on how to pass value from one JFrame to another.
What I want to do:
Since all value inputted by the user were automatically saved into a database, I want to capture first the ID and then pass it on to another JFrame were it will show the recently stored data.
So, basically I have a JFrame were the user will fill up all the required information, after hitting the submit button, I want to show another JFrame showing that the addition of an Item were successful while showing the previous data inputted.
Problem:
I don't know how to pass the value I have got(using a database query) from the first JFrame to another JFrame(where the user will see the complete information recently stored).
here's a sample code:
Statement stat = con.createStatement();
ResultSet rs = stat.executeQuery("SELECT cId, LastName, FirstName, "
+ "Address, TelNo, Email from pcontacts WHERE FirstName ='"
+firstname.getText()+"' AND LastName ='"+lastname.getText()+"'");
int cId;
String LastName, FirstName, Address, TelNo, Email;
if(counter == 0){
if(rs.next()) {
//cId = rs.getIn开发者_Go百科t("cId");
LastName = rs.getString("LastName");
FirstName = rs.getString("FirstName");
Address = rs.getString("Address");
TelNo = rs.getString("TelNo");
Email = rs.getString("Email");
fname.setText(FirstName);
lname.setText(LastName);
address.setText(Address);
contact.setText(TelNo);
email.setText(Email);
counter++;
How can i pass the cId to another JFrame?
Please Help. Thanks in Advance :)
PS
hope you understand my explanation.. if you have any other questions regarding my explanation.. please do tell me, I will try my best to explain it further. Thanks again
The prefered way in many cases is to use PropertyChangeListeners, look at tutorial at http://download.oracle.com/javase/tutorial/uiswing/events/propertychangelistener.html
You can create a custom class extending JFrame, with a private member in it, and initialize it in the constructor.
精彩评论