javaswinguser-interfacejavax

Accessing JTextField using a private class


I'm trying to store output from user inputed text, this is simply a template for it but I can't seem to figure out a way to properly access the JTextArea, all the examples I've found are doing it in a different way then I'm trying to accomplish it, is it possible to do it this way?

import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.*;

public class HelloWorldSwing {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;


public HelloWorldSwing(){
    prepareGUI();
}
public static void main(String[] args){
    HelloWorldSwing swingControlDemo = new HelloWorldSwing();
    swingControlDemo.showEventDemo();
}
private void prepareGUI(){
    mainFrame = new JFrame("Java Swing Demo");
    mainFrame.setSize(400,400);
    mainFrame.setLayout(new GridLayout(3,1));

    headerLabel = new JLabel("",JLabel.CENTER);
    statusLabel = new JLabel("",JLabel.CENTER);
    statusLabel.setSize(350,100);

    mainFrame.addWindowFocusListener(new WindowAdapter() {
        public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
        }
    });
    controlPanel = new JPanel();
    controlPanel.setLayout(new FlowLayout());

    mainFrame.add(headerLabel);
    mainFrame.add(controlPanel);
    mainFrame.add(statusLabel);
    mainFrame.setVisible(true);
}
private void showEventDemo(){
    headerLabel.setText("Control in action: Button");


    JButton submitButton = new JButton("Submit");
    JTextField userTextField = new JTextField("all",26);

    submitButton.setActionCommand("Submit");

    submitButton.addActionListener(new ButtonClickListener());

    controlPanel.add(userTextField);
    controlPanel.add(submitButton);

    mainFrame.setVisible(true);

}

private class ButtonClickListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e){
        JTextField textField = (JTextField) e.getSource();
        String text = textField.getText();
        statusLabel.setText(text);

    }
}

}


Solution

  • First, move the showEventDemo() code into the setup GUI (because that's still setting up the GUI).

    You have a class called ButtonClickListener which is listening to a JButton object, receiving ActionEvents that originate from a JTextField. This is not a sensible approach, because the ActionEvent is only going to have a source of the JButton you added it to as a listener.

    Instead, you need to rewrite your listener. You could create a constructor for it, passing in the JTextField.

    private class StatusUpdateListener implements ActionListener {
        private JTextField input;
        private JLabel status;
    
        public SubmitButtonListener(JTextField source, JLabel dest) {
           input = source;
           status = dest;
        }
    
        public void actionPerformed(ActionEvent e){
            String text = input.getText();
            status.setText(text);
        }
    }
    

    Then it would be used some where like

    submitButton.addActionListener(new StatusUpdateListener(userTextField, statusLabel));