I'm very new to Java - please can somebody point me in the direction of a good tutorial or sample code that will show me how to pass information from a "popup" window back to the main window of the application. The first window will display results, the second window (generated from the first window) has a form for the user to fill in and search a database. I need the second window to post back the search results to the first window, on user close.
Sorry I don't have any code worth pasting here....it's all over the place!
Please let me know if there's any other info I can provide (ahem).
Thanks for any help in advance
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
here's some of the ways you can do this,
1) a class variable to hold the returned data, and creating window #2 as an inner class so it has access to the class variable. 2) passing a reference of window #1 to window #2, again 'exposing' the class variable to #2. 3) something similar to a JOptionPane, and its return value.
here's a crude example of (3)
Craig Wood
Ranch Hand
Joined: Jan 14, 2004
Posts: 1535
posted
0
Eamon Williams
Greenhorn
Joined: Feb 21, 2006
Posts: 18
posted
0
Thanks for the help, this looks like what I need - I'll try it out asap.
Cheers.
Eamon Williams
Greenhorn
Joined: Feb 21, 2006
Posts: 18
posted
0
Hi,
Could somebody please have a look at my code? I have tried the solution above but haven't got it to work succesfully yet. Still getting to grips with the form editor of netbeans aswell )
Any help or pointers would be gratefully appreciated.
Thanks
_______________________________________
/* * TestGUI.java * * Created on 29 September 2005, 19:43 */ import javax.swing.*; import java.util.*; import java.io.*; import java.awt.*; import java.awt.event.*; /** * * @author */ public class TestGUI extends javax.swing.JFrame{
/* Declare vectors to store tournament event information */ public Vector events; public Vector competitors; public Vector officials;
/* Declare variables to count items in vectors */ public int eventsCount; public int competitorsCount; public int officialsCounts;
/* declare variables to store tournament details */ public String tournamentName; public String tournamentDate; public String tournamentLocation; public int numRings; public int durationHours;
/** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ private void initComponents() { java.awt.GridBagConstraints gridBagConstraints;
/* On exit, add.element to vectors with information */ // mySearchCompetitorWindow.competitorName;
}
private void jMenuItem18ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: }
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: System.exit(0); }
private void jMenu2ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: }
/* This method transfers input from dialog box */ private void transferInput(JTextField tf){ jTextField1.setText(tf.getText()); tf.setText(""); }
private void jMenu1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: }
/** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { JFrame jFrame1 = new JFrame(); new TestGUI(jFrame1).setVisible(true); }
1) when posting code, click the 'code' button and these two tags will appear [ CODE] [ /CODE] paste your code in between the two - it preserves the formatting, making it easier to read
2) the code you posted requires a support class SearchCompetitorWindow() i.e. we cannot run the program, and it is extremely doubtful someone will read through 360 lines of code.
3) most of the code you posted is totally unrelated to your problem, 1 menuItem is related, but the other 20 (or is it 30) menuItems, and their respective actions, have nothing to do with your problem.
4) nothing in the code you posted remotely resembles the examples given, - have to assume this code is in the missing SearchCompetitorWindow() code
5) "but haven't got it to work succesfully yet" really does not tell us what problem you have
the best way to get help is to create a sample program. Just enough to demonstrate the problem, where we can copy/paste/compile/run and observe the behavior.
In this case a frame with a button and a label/s. Add the actionListener to the button to create SearchCompetitorWindow() - modify this code as well so that it sets the text of the label/s in the frame (if that is what you are trying to do). Explain what the code is doing and what it is supposed to do. (remember to post the modified SearchCompetitorWindow() code, as well)
Quite often, when putting together a sample program, the problem is self-detected.
Eamon Williams
Greenhorn
Joined: Feb 21, 2006
Posts: 18
posted
0
Hi,
Thanks for the tips. Hope this is better. I have created a new form, of which I have tried to implement the 2nd solution in the thread (posted from member #64659). I think the problem lies in where I am defining the jFrame and where I am passing it - the text from the dialog box is not transferring back to the main window.
Thanks once again.
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
your problem is a duplicate declaration of jTextField1
from the constructor
and in initComponents()
the one in initComponents() is the one added to the panel i.e. the one visible on the screen
the one in the constructor is the one passed to transferInput(), which sets the text of that textfield, not the one on the screen.