0

I make two JFrame. "one.java" for send value to "two.java". when i clicked "Send to two" button on the "one.java" it getting the "one" Text Field values and send to "two.java" and set it correctly. That process working well!!!

As well it is working for other way. I mean first run the "two.java" and click "Send to one" then it set that values on "one.java" You can see it on netbeans.

But in my case one.java is main one. two.java is child one. So i want to when open two.java by pressing Send to two button, send back text field values from two.java to one.java pressing Send to one.

Hope you got my point. I am not good at for explain very well. Exactly i want to pass text field value from child Jframe to main Jframe when child one opened by main jframe. At that time two Jframes are running. User can see two jframes.

Hope you got it. Please kindly fix my problem. If you can complete it do it for me. Now i am 3 days trying to do this. But finally i writing for you.

Here I post my code.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package testing;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
 *
 * @author Code
 */
public class one extends javax.swing.JFrame implements PropertyChangeListener{

    /**
     * Creates new form one
     */
    public one() {
        initComponents();
    }

    one(two aThis) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
@Override
    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals("jButton1ActionPerformed")){
            System.out.println("button in frame1 was pressed");
            this.two.setText((String)evt.getNewValue());
        }
        if (evt.getPropertyName().equals("jTextField1KeyReleased")){
            System.out.println("user typed in frame1 jTextField");
            this.two.setText((String)evt.getNewValue());
        }
    }
    /**
     * 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        one = new javax.swing.JTextField();
        two = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        one.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                oneKeyReleased(evt);
            }
        });

        jButton1.setText("Send to two");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("from two : ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(51, 51, 51)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(two, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(one, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(31, 31, 31)
                        .addComponent(jButton1)))
                .addContainerGap(74, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(159, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(one, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(76, 76, 76)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(two, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(21, 21, 21))
        );

        pack();
    }// </editor-fold>                        

    private void oneKeyReleased(java.awt.event.KeyEvent evt) {                                
        // TODO add your handling code here:
        firePropertyChange("jTextField1KeyReleased", "", one.getText());
    }                               

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        two two2 = new two();
        this.addPropertyChangeListener((PropertyChangeListener) two2);
        two2.setVisible(true);
        firePropertyChange("jButton1ActionPerformed", "",  one.getText());
    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(one.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(one.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(one.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(one.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new one().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField one;
    private javax.swing.JTextField two;
    // End of variables declaration                   
}

Two.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package testing;
import java.awt.Frame;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
/**
 *
 * @author Code
 */
public class two extends javax.swing.JFrame implements PropertyChangeListener{

    /**
     * Creates new form two
     */
    public two() {
        initComponents();
    }
 @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals("jButton1ActionPerformed")){
            System.out.println("button in frame1 was pressed");
            this.one.setText((String)evt.getNewValue());
        }
        if (evt.getPropertyName().equals("jTextField1KeyReleased")){
            System.out.println("user typed in frame1 jTextField");
            this.one.setText((String)evt.getNewValue());
        }
    }
    /**
     * 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        one = new javax.swing.JTextField();
        two = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        two.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                twoKeyReleased(evt);
            }
        });

        jButton1.setText("Send to one");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("From one  : ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(51, 51, 51)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(one, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(two, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1)))
                .addContainerGap(88, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(160, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(one, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(85, 85, 85)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(two, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

    private void twoKeyReleased(java.awt.event.KeyEvent evt) {                                
        // TODO add your handling code here:
        firePropertyChange("jTextField1KeyReleased", "", two.getText());
    }                               

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:

        one one1 = new one();
        this.addPropertyChangeListener((PropertyChangeListener) one1);
        one1.setVisible(true);
        firePropertyChange("jButton1ActionPerformed", "",  two.getText());
    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(two.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(two.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(two.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(two.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new two().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField one;
    private javax.swing.JTextField two;
    // End of variables declaration                   
}
Frakcool
  • 10,915
  • 9
  • 50
  • 89
Oldmax
  • 111
  • 1
  • 2
  • 9
  • This question has been asked before, you may use an Observer pattern or even MVC. – user3437460 Jan 06 '16 at 19:50
  • Hope you got it. Please kindly fix my problem. If you can complete it for me, it will be a big help. Now i am 3 days trying to do this. But finally i am writing for you. Please look this. when i click button to setText from two, i can not use "one one1 = new one();" and "one1.setVisible(true);" . the reason it is one.java is running now. So my way is not working at that time. I need to fix it. – Oldmax Jan 06 '16 at 19:51
  • From your codes, it looks like you used the drag and drop feature from Netbeans. I always find it tedious to edit auto generated codes. You can take a look on MVC. It is a simple pattern. – user3437460 Jan 06 '16 at 19:53
  • anyone here to fix my problem? – Oldmax Jan 06 '16 at 19:58
  • anyone here to fix my problem? Finally i can simply say i need to know how to get value from 2ndFrame to 1stFrame? The 2ndFrame is opened by 1stFrame. 1stFrame is the main one. At that time two Frames are visible to user. When user click button on 2ndFrame pass values to 1stFrame and exit 2ndFrame. Using my code i can send values to 1stFrame to 2ndFrame. As well as but can not do it 2ndFrame to 1stFrame both are opened. Hope clear now. I am new one to java. – Oldmax Jan 06 '16 at 20:04
  • 2
    [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) and for a better idea of a solution maybe have a look at [How to Make Dialogs](http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html) – MadProgrammer Jan 06 '16 at 21:57
  • You should also have a look at previous questions on the subject, as this gets asked way to often – MadProgrammer Jan 06 '16 at 21:58
  • This related [example](http://stackoverflow.com/a/10523401/230513) that uses a modeless dialog. – trashgod Jan 07 '16 at 00:01
  • Thank you for your supports!!! i am trying to implement Dialogs! – Oldmax Jan 07 '16 at 11:53
  • Thank you – MadProgrammer! Yes Dialogs solved my problem. Thank you again. – Oldmax Jan 11 '16 at 11:50

0 Answers0