Frage

How would I use a JTextField across two classes. For example getting an integer value from one class and displaying it in a textField in another class. from jFrame to JFrame

War es hilfreich?

Lösung

Have then nested so you have the class which gets the integer value inside the one which actually displays it E.g:

//Note: this is only a rough representation of the concept
//      what I am trying to get across, it wouldn't really
//      work like this

class Displayer{
    ValueGetter valueGetter;
    JTextField textField;

    public Displayer(){
        valueGetter = new ValueGetter();
    }

    public void display(){

        int value = valueGetter.workOutValue();

        textField.setText(value);
    }
}

Andere Tipps

Your question is:

How would I use a JTextField across two classes. For example getting an integer value from one class and displaying it in a textField in another class.

And I'm going to suggest that you think of this in a different light. Better to consider the general question:

How does one object share its data, its state, with other objects?

One common and useful answer is to give the first class, here the class that holds the JTextField, getter methods, and if need be setter methods, that allow other classes to have the ability to retrieve the state of the JTextField (or even change it if need be). For example, you could give your GUI class a method something like:

public String getTextFieldText() {
  return textField.getText();
}

Then you must make sure that the class that needs this information has a valid instance to the GUI class, i.e. an instance to the displayed GUI, and then have it call this method when it needs the latest information from the GUI.


Edit You've edited your question and have added,

from jFrame to JFrame

This worries me as it suggests that your program uses more than one JFrame. If so, reconsider your program design since it would be unusual for it to need more than one JFrame. If it needs to display another window, usually this is done by displaying a modal or non-modal JDialog. If it needs to change "views" this is often done using a CardLayout to swap JPanels.


Edit 2
For an example of what I'm describing, please see my code from this answer. This code passes information from a modal JDialog into the JFrame that launched it and uses exactly the techniques that I have mentioned above:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class DialogEg {
   private static void createAndShowGUI() {
      MainPanelGen mainPanelGen = new MainPanelGen();

      JFrame frame = new JFrame("DialogEg");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanelGen.getMainPanel());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

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

}

class MainPanelGen {
   private JPanel mainPanel = new JPanel();
   private JTextField field = new JTextField(10);
   private JButton btn = new JButton(new BtnActn());
   private JDialog dialog;
   private DialogPanel dialogPanel = new DialogPanel();

   public MainPanelGen() {
      mainPanel.add(field);
      mainPanel.add(btn);

      field.setEditable(false);
      field.setFocusable(false);
   }

   public JPanel getMainPanel() {
      return mainPanel;
   }

   private class BtnActn extends AbstractAction {
      BtnActn() {
         super("Button");
      }

      @Override
      public void actionPerformed(ActionEvent arg0) {
         if (dialog == null) {
            Window win = SwingUtilities.getWindowAncestor(mainPanel);
            if (win != null) {
               dialog = new JDialog(win, "My Dialog",
                     Dialog.ModalityType.APPLICATION_MODAL);
               dialog.getContentPane().add(dialogPanel);
               dialog.pack();
               dialog.setLocationRelativeTo(null);
            }
         }
         dialog.setVisible(true); // here the modal dialog takes over
         System.out.println   (dialogPanel.getFieldText());
         field.setText(dialogPanel.getFieldText());
      }
   }
}

class DialogPanel extends JPanel {
   private JTextField field = new JTextField(10);
   private JButton exitBtn = new JButton(new ExitBtnAxn("Exit"));

   public DialogPanel() {
      add(field);
      add(exitBtn);
   }

   public String getFieldText() {
      return field.getText();
   }

   private class ExitBtnAxn extends AbstractAction {

      public ExitBtnAxn(String name) {
         super(name);
      }

      @Override
      public void actionPerformed(ActionEvent arg0) {
         Window win = SwingUtilities.getWindowAncestor(DialogPanel.this);
         if (win != null) {
            win.dispose();
         }

      }

   }

}

The JPanel displayed in the modal JDialog has a JTextField field called (appropriately) field:

class DialogPanel extends JPanel {
   private JTextField field = new JTextField(10);

And has a getter method to allow other classes to check the state of this field:

public String getFieldText() {
  return field.getText();
}

The main GUI class, called MainPanelGen carries an instance of the DialogPanel class called dialogPanel:

class MainPanelGen {
   // .... etc
   private DialogPanel dialogPanel = new DialogPanel();

In an ActionListener in the main GUI, a modal JDialog is created "lazily", that is, the dialog is created if a JDialog variable is null, and then the instance is assigned to this variable. The DailogPanel object is placed in the modal dialog and displayed:

  public void actionPerformed(ActionEvent arg0) {
     if (dialog == null) {
        Window win = SwingUtilities.getWindowAncestor(mainPanel);
        if (win != null) {
           dialog = new JDialog(win, "My Dialog",
                 Dialog.ModalityType.APPLICATION_MODAL);
           dialog.getContentPane().add(dialogPanel);
           dialog.pack();
           dialog.setLocationRelativeTo(null);
        }
     }

Since the dialog is modal, the main GUI's code flow freezes when the dialog is displayed, and does not restart until the dialog is no longer visible. Thus we know that all the code after calling setVisible(true) on the dialog is in suspended animation and won't restart until the dialog has been dealt with. We then query the DialogPanel for the textfield's text and us it:

     dialog.setVisible(true); // here the modal dialog takes over
     System.out.println   (dialogPanel.getFieldText());
     field.setText(dialogPanel.getFieldText());

Even if you could do this, you shouldn't. Instead, have two JTextFields that both share the Integer value

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top