Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have 2 frames each with 1 textfield, I want to capture in the first frame textfield of one a string and pass it to another frame in the textfield, but save the data in a global variable type String.

(Sorry for my english)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
128 views
Welcome To Ask or Share your Answers For Others

1 Answer

Java doesn't have "global" variables, and you will not want to use this. Rather you will want to structure your code somewhat along the M-V-C model (model-view-control), and hold the key text String in your model. Then you can have any GUI window you wish listen to the model for changes to this variable, and they can then react accordingly.

If all you want to do is to have your two JTextFields share the exact same text, another thing you can do is have them share the same model -- Document for JTextFields.

On a side, note, you will rarely want your GUI to have more than one JFrame displaying at the same time. If you need a second dependent window, go for a dialog such as a JDialog.


Edit
For example

import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.*;
import javax.swing.event.SwingPropertyChangeSupport;

public class SharedData {
   private static void createAndShowGui() {
      SimpleModel model = new SimpleModel();
      MyPanel1 panel1 = new MyPanel1(model);
      MyPanel2 panel2 = new MyPanel2(model);

      JFrame frame = new JFrame("SharedData");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(panel1);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);

      JDialog dialog = new JDialog(frame, "Dialog", false);
      dialog.getContentPane().add(panel2);
      dialog.pack();
      dialog.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class MyPanel2 extends JPanel {
   private JTextField field = new JTextField(10);
   private UpdateAction updateAction = new UpdateAction("Update");
   private JButton updateDataBtn = new JButton(updateAction);
   private SimpleModel model;

   public MyPanel2(SimpleModel model) {
      this.model = model;
      field.addActionListener(updateAction);

      add(field);
      add(updateDataBtn);
   }

   private class UpdateAction extends AbstractAction {
      public UpdateAction(String name) {
         super(name);
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         if (model != null) {
            model.setMyText(field.getText());
         }
      }
   }

}

class MyPanel1 extends JPanel {
   private JTextField field = new JTextField(10);
   private SimpleModel model;

   public MyPanel1(SimpleModel model) {
      field.setFocusable(false);
      this.model = model;
      add(field);

      model.addPropertyChangeListener(new ModelListener());
   }

   private class ModelListener implements PropertyChangeListener {
      @Override
      public void propertyChange(PropertyChangeEvent evt) {
         if (SimpleModel.MY_TEXT.equals(evt.getPropertyName())) {
            field.setText(model.getMyText());
         }
      }
   }

}

class SimpleModel {
   public static final String MY_TEXT = "my text";
   private SwingPropertyChangeSupport support = new SwingPropertyChangeSupport(this);
   private String myText = "";

   public String getMyText() {
      return myText;
   }

   public void setMyText(String myText) {
      String oldValue = this.myText;
      String newValue = myText;
      this.myText = myText;
      support.firePropertyChange(MY_TEXT, oldValue, newValue);
   }

   public void addPropertyChangeListener(PropertyChangeListener listener) {
      support.addPropertyChangeListener(listener);
   }

   public void removePropertyChangeListener(PropertyChangeListener listener) {
      support.removePropertyChangeListener(listener);
   }

}

Edit 2
If you want both JTextFields to share the same data as the user types it in, then have them share models (Documents for JTextFields). For example

import javax.swing.*;

public class SharedData2 {
   private static void createAndShowGui() {
      JTextField field1 = new JTextField(10);
      JTextField field2 = new JTextField(10);

      // ******** key below
      field2.setDocument(field1.getDocument());

      JPanel panel1 = new JPanel();
      JPanel panel2 = new JPanel();

      panel1.add(field1);
      panel2.add(field2);

      JFrame frame = new JFrame("SharedData2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(panel1);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);

      JDialog dialog = new JDialog(frame, "Dialog", false);
      dialog.add(panel2);
      dialog.pack();
      int x = frame.getLocation().x + 200;
      int y = frame.getLocation().y + 200;

      dialog.setLocation(x, y);
      dialog.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...