Search code examples
javaswingjframeactionlistenergetter

How do I get a variable collected from a JTextField and give it to a getter method?


Very new to using Java and spent hours looking for a solution, but I cannot find out how to get the input collected by a JTextField with a button and ActionListener then be used for a getter method that can receive idNum so I can use the input in another class.

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

public class Staff extends JFrame implements ActionListener {
    
    private staffBooking staffBooking = new staffBooking();

    String weekArray[] = { "Week1", "Week2" };
    String dayArray[] = { "Monday" , "Tuesday", "Wednesday", "Thursday", "Friday" };
    
    JPanel panel = new JPanel();
    JLabel weekLabel = new JLabel("Week:  ", SwingConstants.RIGHT);
    JLabel dayLabel = new JLabel("Day:  ", SwingConstants.RIGHT);
    JLabel idLabel = new JLabel("ID:  ", SwingConstants.RIGHT);
    static JTextField id = new JTextField("",2);
    JComboBox weekDrop = new JComboBox(weekArray);
    JComboBox dayDrop = new JComboBox(dayArray);
    JButton button = new JButton("Submit");

    private static int idNum;
        
    public Staff() {
        setTitle("Staff");
        setSize(250,250);
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        setLocationRelativeTo(null);
        
        setLayout(new GridLayout(4, 2));        
        
        add(weekLabel);
        add(weekDrop);
        
        add(dayLabel);
        add(dayDrop);
        
        add(idLabel);
        add(id);
        
        add(panel);
        add(button);
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                if(id.getText().equals("")==false) {
                    if (e.getSource() == button) {
                        staffBooking.setEnabled(true);
                        staffBooking.setVisible(true);
                        idNum = Integer.parseInt(id.getText());
                    }
                }
            }    
        });     
    }
    
    public static int getID() {
        
        return idNum;
    }   
}

Solution

  • I believe what you are trying to do is something like this:

    import javax.swing.*;
    import javax.swing.SwingConstants;
    
    public class Staff extends JFrame /*implements ActionListener*/ {
    
        private final JPanel panel = new JPanel();
        private final JLabel idLabel = new JLabel("ID:  ", SwingConstants.RIGHT);
        private final JTextField id = new JTextField("",5);
        private final JButton button = new JButton("Submit");
        private int idNum;
    
        public Staff() {
            StaffBooking staffBooking = new StaffBooking(this);
            setTitle("Staff");
            setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            setLocationRelativeTo(null);
            panel.add(idLabel);
            panel.add(id);
            panel.add(button);
            add(panel);
            button.addActionListener(e -> {
                if(! id.getText().isEmpty()) {
                    idNum = Integer.parseInt(id.getText());
                    staffBooking.setVisible(true);
                }
            });
    
            pack();
            setVisible(true);
        }
    
        public int getID() {
            return idNum;
        }
    
        public static void main(String[] args) {
            new Staff();
        }
    }
    
    class StaffBooking extends JDialog {
    
        public StaffBooking(Staff staff) {
            JPanel panel = new JPanel();
            JLabel idLabel = new JLabel("  ");
            JButton button = new JButton("Show ID");
            button.addActionListener(e -> {
                idLabel.setText(String.valueOf(staff.getID()));
            });
    
            panel.add(button);
            panel.add(idLabel);
            add(panel);
            setLocationRelativeTo(null);
            pack();
        }
    }
    

    Using a setter in StaffBooking would be better in this case:

    public class Staff extends JFrame /*implements ActionListener*/ {
    
        private final JPanel panel = new JPanel();
        private final JLabel idLabel = new JLabel("ID:  ", SwingConstants.RIGHT);
        private final JTextField id = new JTextField("",5);
        private final JButton button = new JButton("Submit");
    
        public Staff() {
            StaffBooking staffBooking = new StaffBooking();
            setTitle("Staff");
            setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            setLocationRelativeTo(null);
            panel.add(idLabel);
            panel.add(id);
            panel.add(button);
            add(panel);
            button.addActionListener(e -> {
                if(! id.getText().isEmpty()) {
                    int idNum = Integer.parseInt(id.getText());
                    staffBooking.setID(idNum);
                    staffBooking.setVisible(true);
                }
            });
    
            pack();
            setVisible(true);
        }
    
        public static void main(String[] args) {
            new Staff();
        }
    }
    
    class StaffBooking extends JDialog {
    
        private int idNum;
    
        public StaffBooking() {
            JPanel panel = new JPanel();
            JLabel idLabel = new JLabel("   ");
            JButton button = new JButton("Show ID");
            button.addActionListener(e -> {
                idLabel.setText(String.valueOf(idNum));
            });
    
            panel.add(button);
            panel.add(idLabel);
            add(panel);
            setLocationRelativeTo(null);
            pack();
        }
    
        public void setID(int idNum) {
            this.idNum = idNum;
        }
    }
    

    A further improvement oof the structure can be achieved by using a model class, shared between Staff and StaffBooking:

    public class Staff extends JFrame /*implements ActionListener*/ {
    
        private final JPanel panel = new JPanel();
        private final JLabel idLabel = new JLabel("ID:  ", SwingConstants.RIGHT);
        private final JTextField id = new JTextField("",5);
        private final JButton button = new JButton("Submit");
    
        public Staff() {
            Model model = new Model();
            StaffBooking staffBooking = new StaffBooking(model);
            setTitle("Staff");
            setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            setLocationRelativeTo(null);
            panel.add(idLabel);
            panel.add(id);
            panel.add(button);
            add(panel);
            button.addActionListener(e -> {
                if(! id.getText().isEmpty()) {
                    int idNum = Integer.parseInt(id.getText());
                    model.setID(idNum);
                    staffBooking.setVisible(true);
                }
            });
    
            pack();
            setVisible(true);
        }
    
        public static void main(String[] args) {
            new Staff();
        }
    }
    
    class StaffBooking extends JDialog {
    
        public StaffBooking(Model model) {
            JPanel panel = new JPanel();
            JLabel idLabel = new JLabel("   ");
            JButton button = new JButton("Show ID");
            button.addActionListener(e -> {
                idLabel.setText(String.valueOf(model.getID()));
            });
    
            panel.add(button);
            panel.add(idLabel);
            add(panel);
            setLocationRelativeTo(null);
            pack();
        }
    }
    
    class Model{
    
        private int idNum;
    
        public int getID() {
            return idNum;
        }
    
        public void setID(int idNum) {
            this.idNum = idNum;
        }
    }