File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Pass data back to Window 1 from Window 2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Pass data back to Window 1 from Window 2" Watch "Pass data back to Window 1 from Window 2" New topic
Author

Pass data back to Window 1 from Window 2

Eamon Williams
Greenhorn

Joined: Feb 21, 2006
Posts: 18
Hi,

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
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
Eamon Williams
Greenhorn

Joined: Feb 21, 2006
Posts: 18
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
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;

JDialog dialog;
// JTextField results;
JButton btn;

/** Creates new form TestGUI */
public TestGUI(final Frame jFrame1) {
initComponents();

jTextField1 = new JTextField(16);
boolean isModal = false;
dialog = new JDialog(jFrame1, "enter data, press enter", isModal);
JButton btn = new JButton("Submit");
final JTextField tf = new JTextField(16);
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
transferInput(tf);
dialog.dispose();
}

});

dialog.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
transferInput(tf);
}
});

JPanel openPanel = new JPanel();
openPanel.add(tf);
openPanel.add(btn);
dialog.getContentPane().add(openPanel);
dialog.setSize(300,160);
dialog.setLocationRelativeTo(jFrame1);
}

/** 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;

jPanel1 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
jPanel3 = new javax.swing.JPanel();
jTextField1 = new javax.swing.JTextField();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem17 = new javax.swing.JMenuItem();
jSeparator1 = new javax.swing.JSeparator();
jMenuItem18 = new javax.swing.JMenuItem();
jSeparator3 = new javax.swing.JSeparator();
jMenuItem19 = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenu4 = new javax.swing.JMenu();
jMenuItem7 = new javax.swing.JMenuItem();
jMenuItem8 = new javax.swing.JMenuItem();
jMenuItem20 = new javax.swing.JMenuItem();
jSeparator2 = new javax.swing.JSeparator();
jMenu10 = new javax.swing.JMenu();
jMenuItem3 = new javax.swing.JMenuItem();
jSeparator4 = new javax.swing.JSeparator();
jMenuItem5 = new javax.swing.JMenuItem();
jMenu3 = new javax.swing.JMenu();
jMenuItem6 = new javax.swing.JMenuItem();
jMenuItem4 = new javax.swing.JMenuItem();
jMenuItem21 = new javax.swing.JMenuItem();
jMenu5 = new javax.swing.JMenu();
jMenuItem9 = new javax.swing.JMenuItem();
jMenu6 = new javax.swing.JMenu();
jMenuItem11 = new javax.swing.JMenuItem();
jMenu7 = new javax.swing.JMenu();
jMenuItem10 = new javax.swing.JMenuItem();
jSeparator5 = new javax.swing.JSeparator();
jMenuItem12 = new javax.swing.JMenuItem();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Tae Kwon Do Tournament Scheduler");
setBackground(new java.awt.Color(255, 255, 255));
getAccessibleContext().setAccessibleName("f");
jPanel1.setLayout(null);

jPanel1.setMinimumSize(new java.awt.Dimension(0, 0));
jPanel1.setPreferredSize(new java.awt.Dimension(500, 350));
jPanel2.setBorder(new javax.swing.border.EtchedBorder());
jPanel1.add(jPanel2);
jPanel2.setBounds(410, 0, 140, 380);

jPanel3.setBorder(new javax.swing.border.EtchedBorder());
jPanel3.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
jPanel3PropertyChange(evt);
}
});

jTextField1.setColumns(16);
jTextField1.setName("");
jPanel3.add(jTextField1);

jPanel1.add(jPanel3);
jPanel3.setBounds(0, 0, 410, 380);

getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

jMenu1.setText("File");
jMenu1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenu1ActionPerformed(evt);
}
});

jMenuItem1.setText("Open..");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});

jMenu1.add(jMenuItem1);

jMenuItem17.setText("Close");
jMenu1.add(jMenuItem17);

jMenu1.add(jSeparator1);

jMenuItem18.setText("Save");
jMenuItem18.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem18ActionPerformed(evt);
}
});

jMenu1.add(jMenuItem18);

jMenu1.add(jSeparator3);

jMenuItem19.setText("Print..");
jMenu1.add(jMenuItem19);

jMenuItem2.setText("Exit");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
}
});

jMenu1.add(jMenuItem2);

jMenuBar1.add(jMenu1);

jMenu2.setText("Edit");
jMenu2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenu2ActionPerformed(evt);
}
});

jMenu4.setText("Withdraw");
jMenuItem7.setText("Competitor");
jMenu4.add(jMenuItem7);

jMenuItem8.setText("Event");
jMenu4.add(jMenuItem8);

jMenuItem20.setText("Official");
jMenu4.add(jMenuItem20);

jMenu2.add(jMenu4);

jMenu2.add(jSeparator2);

jMenu10.setText("Add..");
jMenuItem3.setText("\"\" \"\"");
jMenu10.add(jMenuItem3);

jMenu2.add(jMenu10);

jMenu2.add(jSeparator4);

jMenuItem5.setText("Edit competitor weight");
jMenu2.add(jMenuItem5);

jMenuBar1.add(jMenu2);

jMenu3.setText("View");
jMenuItem6.setText("Competitor Details");
jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem6ActionPerformed(evt);
}
});

jMenu3.add(jMenuItem6);

jMenuItem4.setText("Event Details");
jMenu3.add(jMenuItem4);

jMenuItem21.setText("Official Details");
jMenu3.add(jMenuItem21);

jMenuBar1.add(jMenu3);

jMenu5.setText("Schedule");
jMenuItem9.setText("Schedule Tournament");
jMenu5.add(jMenuItem9);

jMenuBar1.add(jMenu5);

jMenu6.setText("Options");
jMenuItem11.setText("Upload Results");
jMenu6.add(jMenuItem11);

jMenuBar1.add(jMenu6);

jMenu7.setText("Help");
jMenuItem10.setText("Tournament Scheduler Help");
jMenu7.add(jMenuItem10);

jMenu7.add(jSeparator5);

jMenuItem12.setText("About");
jMenu7.add(jMenuItem12);

jMenuBar1.add(jMenu7);

setJMenuBar(jMenuBar1);

pack();
}

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(!dialog.isVisible())
dialog.setVisible(true);
}

private void jPanel3PropertyChange(java.beans.PropertyChangeEvent evt) {
// TODO add your handling code here:
}

private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
/* Open SearchCompetitorWindow */
SearchCompetitorWindow mySearchCompetitorWindow = new SearchCompetitorWindow();

// myNewTournamentUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mySearchCompetitorWindow.setSize(400, 350);
mySearchCompetitorWindow.pack();
mySearchCompetitorWindow.setVisible(true);

/* 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);
}

});
}

// Variables declaration - do not modify
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu10;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenu jMenu4;
private javax.swing.JMenu jMenu5;
private javax.swing.JMenu jMenu6;
private javax.swing.JMenu jMenu7;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem10;
private javax.swing.JMenuItem jMenuItem11;
private javax.swing.JMenuItem jMenuItem12;
private javax.swing.JMenuItem jMenuItem17;
private javax.swing.JMenuItem jMenuItem18;
private javax.swing.JMenuItem jMenuItem19;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem20;
private javax.swing.JMenuItem jMenuItem21;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JMenuItem jMenuItem5;
private javax.swing.JMenuItem jMenuItem6;
private javax.swing.JMenuItem jMenuItem7;
private javax.swing.JMenuItem jMenuItem8;
private javax.swing.JMenuItem jMenuItem9;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JSeparator jSeparator2;
private javax.swing.JSeparator jSeparator3;
private javax.swing.JSeparator jSeparator4;
private javax.swing.JSeparator jSeparator5;
private javax.swing.JTextField jTextField1;
// End of variables declaration

}

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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
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
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.

to fix, comment out the one in the constructor
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pass data back to Window 1 from Window 2
 
Similar Threads
Simple Query
nx:All of URLy Bird 1.1.3 find and search method
JavaScript Objects to set array of request parameters
Multiple sessions paging result sets...
cookie interference with user session