0

a basic problem that i can't figure out, tried a lot of things and can't get it to work, i need to be able to get the value/text of the variable String input; so that i can use it again in a different class in order to do an if statement based upon the result

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

        public class pInterface extends JFrame {
    String input;
    private JTextField item1;

    public pInterface() {
        super("PAnnalyser");
        setLayout(new FlowLayout());

        item1 = new JTextField("enter text here", 10);
        add(item1);

        myhandler handler = new myhandler();
        item1.addActionListener(handler);
        System.out.println();

    }

    public class myhandler implements ActionListener {

        // class that is going to handle the events
        public void actionPerformed(ActionEvent event) {

            // set the variable equal to empty
            if (event.getSource() == item1)// find value in box number 1
                input = String.format("%s", event.getActionCommand());

        }

        public String userValue(String input) {
            return input;
        }

    }

}
Bugz
  • 9
  • 4

3 Answers3

3

You could display the window as a modal JDialog, not a JFrame and place the obtained String into a private field that can be accessed via a getter method. Then the calling code can easily obtain the String and use it. Note that there's no need for a separate String field, which you've called "input", since we can easily and simply extract a String directly from the JTextField (in our "getter" method).

For example:

import java.awt.Dialog.ModalityType;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import javax.swing.*;
import javax.swing.text.JTextComponent;

public class TestPInterface {
   @SuppressWarnings("serial")
   private static void createAndShowGui() {
      final JFrame frame = new JFrame("TestPInterface");

      // JDialog to hold our JPanel
      final JDialog pInterestDialog = new JDialog(frame, "PInterest",
            ModalityType.APPLICATION_MODAL);
      final MyPInterface myPInterface = new MyPInterface();
      // add JPanel to dialog
      pInterestDialog.add(myPInterface);
      pInterestDialog.pack();
      pInterestDialog.setLocationByPlatform(true);

      final JTextField textField = new JTextField(10);
      textField.setEditable(false);
      textField.setFocusable(false);      

      JPanel mainPanel = new JPanel();
      mainPanel.add(textField);
      mainPanel.add(new JButton(new AbstractAction("Get Input") {

         @Override
         public void actionPerformed(ActionEvent e) {
            // show dialog
            pInterestDialog.setVisible(true);
            // dialog has returned, and so now extract Text
            textField.setText(myPInterface.getText());
         }
      }));

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

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

// by making the class a JPanel, you can put it anywhere you want
// in a JFrame, a JDialog, a JOptionPane, another JPanel
@SuppressWarnings("serial")
class MyPInterface extends JPanel {

   // no need for a String field since we can 
   // get our Strings directly from the JTextField
   private JTextField textField = new JTextField(10);

   public MyPInterface() {
      textField.addFocusListener(new FocusAdapter() {
         @Override
         public void focusGained(FocusEvent e) {
            JTextComponent textComp = (JTextComponent) e.getSource();
            textComp.selectAll();
         }
      });

      add(new JLabel("Enter Text Here:"));
      add(textField);

      textField.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e) {
            Window win = (Window) SwingUtilities.getWindowAncestor(MyPInterface.this);
            win.dispose();
         }
      });
   }

   public String getText() {
      return textField.getText();
   }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
2

A Good way of doing this is use Callback mechanism.

I have already posted an answer in the same context.

Please find it here JFrame in separate class, what about the ActionListener?.

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
0

Your method is a bit confusing:

public String userValue(String input) {
            return input;
        }

I guess you want to do something like this:

public String getInput() {
    return input;
}

public void setInput(String input) {
    this.input = input;
}

Also your JFrame is not visible yet. Set the visibility like this setVisible(true)

Klemens Morbe
  • 595
  • 9
  • 24