0

What I want to achieve is very simple. I have 2 classes. "SpeedingTicket" & "SpeedingTicket GUI". Inside my GUI I have 1 textbox name txtSpeedLimit & a button. Inside my SpeedingTicket class I have a variable "int speedingTicket". Inside my SpeedingTicket class I also have a get & set method for "speedingTicket".

I know how to get and set text using JTextFields, but I want to be able to:

receive input from the "txtSpeedLimit", and store that value into the "txtSpeedLimit" instance variable in the "SpeedTicket" class. I can then check for validation etc when I come to adding the vehicle speed.

Maybe this isn't the most efficient way of dealing with this program. Maybe I should scrap the instance variables in SpeedingTicket, and deal with it all in the GUI.

Any advice would be hugely appreciated.

Basically what I'm trying to do is this:

class confirmHandler implements ActionListener{
    public void actionPerformed(ActionEvent e){
        String val = txtSpeedLimit.getText();
        int realNum = speed.getSpeedLimit() = txtSpeedLimit; < but obviously that doesn't work, but I want the textbox link to the variable. 

EDIT: If we take away the GUI, all I want my program to do is the following: Speed Limit: 50 < enterd via textfield Speed: 60 < entered via textfield if the speed is blah blah (ive already coded this).. then output a result to one of my labels. I achieved this without making a GUI and making it only console based, but instead of the user typing it via the console, I want it to be typed via textfields.

THe values that are entered into the textfields should be stored in the two variables (speed and speedlimit) that are in the SpeedingTicket class.

TLG123
  • 99
  • 10
  • You can check for text changes like here: http://stackoverflow.com/questions/3953208/value-change-listener-to-jtextfield and create a costume listener "SpeedChangeListener" – Jarlik Stepsto Mar 05 '17 at 10:38
  • You want to read about how to use models for swing ui components. – GhostCat Mar 05 '17 at 10:39
  • Possible duplicate of [*How to wire one pane to another*](http://stackoverflow.com/q/10523343/230513). – trashgod Mar 05 '17 at 12:56

4 Answers4

0

You don't need to store values in JText or any GUI componenets... Use global static variables. For example:

public static int speed_limit;

You can access this variable from ANY method,class, etc.

ShlomiRex
  • 321
  • 1
  • 2
  • 10
0

There are multiple ways to do it.

You can detect textfield changes by using a DocumentListener or if you want (not recommended) by a KeyListener.

The Listener could be implemented directly by your gui class or by your other class. If you want more abstraction you could implement the DocumentListener by your gui class and create a method

public void addSpeedChangeListener(SpeedChangeListener scl) {

    this.speedChangeListeners.add(scl);
}

Your SpeedChangeListener could be very simple:

public interface SpeedChangeListener {

    public void speedChanged(int value);
}

Then your second class implements the SpeedChangeListener and calls addSpeedChangeListener(this) on your gui class. Inside the gui class, your document listener calls speedChanged(val) for every listener registered.

EDIT You can also use the Button and call the speedChanged on every listener inside the actionPerformed method of the ActionListener.

Jarlik Stepsto
  • 1,667
  • 13
  • 18
0

I think it would be easier to use a JOptionDialog which pop ups when the button is clicked. That way you can easily get input and also validate the input straight away.

user982467
  • 142
  • 8
0

You can update a value in:

public class SpeedingTicket {

    int speedingTicket;

    public SpeedingTicket() {

        speedingTicket = 500;
    }

    public int getSpeedingTicket() {
        return speedingTicket;
    }
}

by:

public class SpeedingTicketGUI extends JPanel{

    SpeedingTicket st;

    SpeedingTicketGUI() {

        st = new SpeedingTicket();
        setLayout(new FlowLayout(FlowLayout.LEFT));
        JTextField txtField = new JTextField(10);
        txtField.setText(""+st.getSpeedingTicket());
        add(txtField);

        JButton btn = new JButton("Update");
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setSpeedingTicket(txtField.getText());
            }
        });
        add(btn);
    }

    private void setSpeedingTicket(String text) {

        try {
            int speedTicket = Integer.parseInt(text);
            st.setSpeedingTicket(speedTicket);
            System.out.println("Speeding ticket set to " +st.getSpeedingTicket());
        } catch (NumberFormatException ex) {
            System.out.println("Invalid value " +text);
            ex.printStackTrace();
        }
    }

    public static void main(String[] args){

        JFrame frame = new JFrame("Speeding Ticket");
        frame.setSize(400,100);
        frame.add(new SpeedingTicketGUI());
        frame.setVisible(true);
    }
}
c0der
  • 18,467
  • 6
  • 33
  • 65
  • Okay so would I need just a document listener that links back to the original SpeedingTicket class when I enter a value in the GUI classes textbox? – TLG123 Mar 07 '17 at 18:23
  • No. If you want the `SpeedingTicketGUI` to change the value of `speedingTicket` in `SpeedingTicket` (say by pressing a button) you'll need a listener in `SpeedingTicketGUI`, like you did. Please edit the question to clarify what exactly are you trying to achieve. – c0der Mar 08 '17 at 04:39
  • Ahh brilliant, well this morning I tried implementing what you had said and when I was calling "("Speeding ticket set to " +st.getSpeedingTicket());" it would return to 0. So I set the "setSpeedingTicket" method to this.vehicleSpeed = vehicleSpeed and it now seems to be linking. That was the correct way of doing it right? – TLG123 Mar 09 '17 at 09:50
  • Thank you. I appreciate your help very very much. I guess I should make myself much clearer in future because it wasn't necessary to use any DocumentListeners etc. – TLG123 Mar 09 '17 at 10:19
  • I tried voting it up even though the 0 doesn't progress onto a 1, but it says it is still counted. – TLG123 Mar 10 '17 at 20:07