Access to Java Swing TextField from other class

10,761

Solution 1

You may not actually want a JTextField. It sounds like you're waiting for a line of input from the user, which should really be a JOptionPane. How to do this is described here:

http://download.oracle.com/javase/tutorial/uiswing/components/dialog.html#input

Basically, JOptionPane.showInputDialog() will cause a window to pop up that contains a text field and OK/Cancel buttons, and if you press enter it will take your input. This eliminates the need for another class.

You'd put it in your inputData() method:

inputData()
{
    String input = JOptionPane.showInputDialog(...);
    //input is whatever the user inputted
}

If that's not what you're looking for and you want a text field that stays open, perhaps what you really want is a "Submit" button next to your JTextField that allows the user to decide when to submit the text. In that case, you could have:

class B extends JFrame
{
    private A myA;

    private JTextField input;

    private JButton submitButton;

    public B()
    {
        submitButton.addActionListener(new SubmitListener());
    }

    private class SubmitListener
    {
        //this method is called every time the submitButton is clicked
        public void actionPerformed(ActionEvent ae)
        {
            myA.sendInput(inputField.getText());
            //A will need a method sendInput(String)
        }
    }
}

Solution 2

TextField? Since it's a Swing project, I hope you mean a JTextField, right? And don't add a KeyListener to it, but rather an ActionListener since these are triggered when the user presses Enter. One way to solve your problem is to give the GUI class (here named B) a public method that will allow outside classes to add an ActionListener to the JTextField. Perhaps you can call it addActionListenerToInput(ActionListener listener). Then Class A can add the listener to B, and the actionPerformed code will be called when enter is pressed.

e.g.,

class A {
   B b = new B();
   public A() {
       //inputData();
      b.addActionListenerToInput(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            inputActionPerformed(e);
         }
      });
   }

   private void inputActionPerformed(ActionEvent e) {
      JTextField input = (JTextField) e.getSource();
      String text = input.getText();
      input.setText("");

      // do what you want with the text String here
   }
}

//-------------------------------

class B extends JFrame{
   private JTextField input;

   public B() {
   }

   public void addActionListenerToInput(ActionListener listener) {
      input.addActionListener(listener);
   }

}
Share:
10,761
Martynas
Author by

Martynas

Updated on June 04, 2022

Comments

  • Martynas
    Martynas almost 2 years

    I have a problem with Java Swing text input. I have a method inputData() in class A and when I call it, the method should wait while user fill TextField input in class B and press ENTER. Finally, the method inputData() should have the text that user wrote. How could I solve it?

    class A {
        B b = new B();
        public A() {
            inputData();
        }
    
        public char[] inputData() {
            // there I would like to get text 
            // from TextField from class B
        }
    }
    
    //-------------------------------
    
    class B extends JFrame{
        private JTexField input;
    
        public B() {
        }
    
        private void inputKeyPressed(KeyEvent e) {                                   
            if (e.getKeyCode() == 10) {  // pressed ENTER
                input.getText()
                input.setText(null);
            }
        } 
    }