简体   繁体   中英

Using Java Swing, how do you take an input from a user and store it for later use?

I am currently writing a program which takes an input from a user which is a file, and an integer. Both of these fields are entered into the gui, and then when the user clicks the button I prompt them on screen with, the program should then store the two inputs into variables for later use when it comes to searching the file. The issue is that for swing, there needs to be an ActionListener which is formatted like this:

    0 JButton okButton = new JButton("OK");
    1 okButton.addActionListener(new ActionListener() {
    2 public void actionPerformed(ActionEvent e) {
    3 //(WHERE I'M HAVING THE ISSUE)
    4 }
    5 });

My issue is that the user's click is registered, and then the code within the actionPerformed function (line 2-3) executes, but since it is in its own function, it cannot save to any variables which are made outside of it (which would be above line 0). It also cannot return any values since actionPerformed's return is supposed be void. Not only this, but you can't fully alter the gui inside of this function, so I cannot write the rest of the code inside of it. Is there any way for me to store the two inputs the user puts into my gui as variables which I can use for the rest of my program? For clarification, this is what I want to happen:

     String userInput = null;
     JButton okButton = new JButton("OK");
     okButton.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) {
     //userInput = (the string the user submitted)
     }
     });
     mainFrame.add(okButton);
     mainFrame.setVisible(true);
     //(And from here on use the updated user input as I need)

Overall I don't understand how to take information from a user input in my gui and store it for later.

ActionListener is a functional interface which means that, since JDK 8, you can implement it with a method reference .

Declare the GUI components, that the user uses to enter the file and the integer, as class member variables so that you can reference them from the method that implements ActionListener , for example:

public class Gui {
    private javax.swing.JTextField fileTextField;
    private javax.swing.JSpinner integerSpinner;

    private void createAndDisplayGui() {
        javax.swing.JButton button = new javax.swing.JButton("Hit me!");
        button.addActionListener(this::performAction);
    }

    // 'ActionListener' implementation.
    private void performAction(java.awt.event.ActionEvent event) {
        String file = fileTextField.getText();
    }
}

The name of the method that implements ActionListener interface can be any name you like. It just has to take the same parameters (as method actionPerformed ) and return the same value, ie void in the case of method actionPerformed which is the sole method in interface ActionListener . Also note that the method need not be public.

If it were me, I'd use a JPanel and JOptionPane

JPanel p = new JPanel();
JTextField tf = new JTextField(); // For the file name. You can use a JFileChooser also
JTextField tf2 = new JTextField(); // For the number;
p.setLayout(new GridLayout(1,2));
p.add(tf);
p.add(tf2);
int result = JOptionPane.showConfirmDialog(null, p);
String fileName = null;
if (result == JOptionPane.OK_OPTION) {
  fileName = tf.getText();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM