0

so I have a frame 1 and frame 2 in the frame 1 by having 4 and has 1 JButton JTextField at 4 JTextField a user to input an integer value .. after the user input, the user presses a JButton and JFrame will feature 2 ..

and in the second frame I have 1 JTextArea which will print out a value that a user input

so how to send values ​​from frame 1 to frame 2?

actually in this project I've given constructor in which I made into a class and in Jframe1 "coba.java" I make new objeck with this code:

coba ar = new coba(); 

in a Jframe1 I have a method in which DDA has a code:

double X0 = Double.parseDouble (x0.getText ()); 
double X1 = Double.parseDouble (x1.getText ()); 
double Y0 = Double.parseDouble (y0.getText ()); 
double Y1 = Double.parseDouble (y1.getText ()); 
int no = 1; 
ar.X0 = X0; 
ar.X1 = X1; 
ar.Y0 = Y0; 
ar.Y1 = Y1;
  • This is a basic prinicple of references and parameters. Consider providing an actual [runnable example that demonstrates your problem](https://stackoverflow.com/help/mcve) would involve less guess work and better responses – MadProgrammer Jun 10 '14 at 05:11

2 Answers2

0

You can make a thread that can check some static variables for changes and update the frames accordingly. For example somewhere after showing the two frames:

    new Thread(new Runnable(){
            public void run(){
                    try{
                            while(true){
                                    if(coba.HASNEWVALUE){
                                            updateFrame(); // some function that does the updating and communicating
                                            coba.HASNEWVALUE = false;
                                    }

                            }
                    }
                    catch(Exception e){
                            e.printStackTrace();
                    }
            }

    }).start();

Whenever you want to pass a new value, set the appropriate values and set coba.HASNEWVALUE to true, this way your frame will fetch the required update automatically through the updateFrame() function everytime coba.HASNEWVALUE is TRUE.

  • Why? Apart from breaking the single thread rules of Swing, why not simply use an observer pattern and simply notify interested parties of the change...just like Swing does already... – MadProgrammer Jun 10 '14 at 05:20
0

Basically, Frame 1 will need a reference to Frame 2 either directly or indirectly via some kind of observer implementation...

This way, all you need to do is provide some kind of method in Frame 2 that will allow you to pass the information you need to it so it can update the text area in question.

Personally, I prefer to use interfaces for this, as it prevents the caller from messing with things it shouldn't

Oh, and you might also want to have a read through The Use of Multiple JFrames: Good or Bad Practice?

For example...

The NotePad interface prevents SecretaryPane from making changes to the underlying implementation, because the only method it actually knows about is the addNote method

Notes

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class CrossCalling {

    public static void main(String[] args) {
        new CrossCalling();
    }

    public CrossCalling() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                NotePadPane notePadPane = new NotePadPane();

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new SecretaryPane(notePadPane));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                JFrame noteFrame = new JFrame("Testing");
                noteFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                noteFrame.setLayout(new BorderLayout());
                noteFrame.add(notePadPane);
                noteFrame.pack();
                noteFrame.setLocation(frame.getX(), frame.getY() + frame.getHeight());
                noteFrame.setVisible(true);
            }
        });
    }

    public interface NotePad {

        public void addNote(String note);

    }

    public class SecretaryPane extends JPanel {

        private NotePad notePad;

        public SecretaryPane(NotePad pad) {
            this.notePad = pad;
            setLayout(new GridBagLayout());
            JButton btn = new JButton("Make note");
            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    pad.addNote(DateFormat.getTimeInstance().format(new Date()));
                }
            });
            add(btn);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

    public class NotePadPane extends JPanel implements NotePad {

        private JTextArea ta;

        public NotePadPane() {
            setLayout(new BorderLayout());
            ta = new JTextArea(10, 20);
            add(new JScrollPane(ta));
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        public void addNote(String note) {

            ta.append(note + "\n");

        }
    }

}
Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366