# 6
Here is some sample code generated using Netbeans 5.5
FIRST myFrame1
--
/*
* NewJFrame.java
*
* Created on February 19, 2007, 9:39 PM
*/
package printmonitor; //Forget about this line
/**
*
* @author janunezc
*/
public class myFrame1 extends javax.swing.JFrame {
/** Creates new form NewJFrame */
public myFrame1() {
initComponents();
}
/** 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.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Change myFrame2");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel1.setText("Content for textbox in myFrame2");
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.add(jLabel1)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 18, Short.MAX_VALUE)
.add(jButton1)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jLabel1)
.add(jButton1))
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
myFrame2Ref.setTextBoxContent(this.jLabel1.getText());
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new myFrame1().setVisible(true);
}
});
}
public void setmyFrame2Reference(myFrame2 mf2) {
myFrame2Ref = mf2;
}
private myFrame2 myFrame2Ref = null;
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
SECOND myFrame2
--
/*
* NewJFrame2.java
*
* Created on February 19, 2007, 10:22 PM
*/
package printmonitor; //Forget about this
/**
*
* @author janunezc
*/
public class myFrame2 extends javax.swing.JFrame {
/** Creates new form NewJFrame2 */
public myFrame2() {
initComponents();
}
/** 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.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jButton1.setText("jButton1");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextField1.setText("This text will be changed by myFrame1");
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new myFrame2().setVisible(true);
}
});
}
public void setTextBoxContent(String content) {
this.jTextField1.setText(content);
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
THIRD: Main
/*
* Main.java
*
* Created on February 15, 2007, 10:25 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package printmonitor;//forget about this line
/**
*
* @author janunezc
*/
public class Main {
/** Creates a new instance of Main */
public Main() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
myFrame1 mf1 = new myFrame1();
myFrame2 mf2 = new myFrame2();
mf1.setmyFrame2Reference(mf2);
mf2.setVisible(true);
mf1.setVisible(true);
}
}
FOURTH
--
Let me know if it is clear enough now... start with the main class to understand what it does. Basically it creates a frame myFrame1 and another myFrame2
Then runs a method in myFrame1 to let it know what is the object where the text will be transfered to...
Finally it opens both forms...
MyFrame2 implements a method to expose access to external callers to modify content of jTextField1
MyFrame1 calls such method.
Let me know if this was useful or if it just does not make any sense.
JN