David Lund
David Lund

Reputation: 235

How to use ActionListener, and pass stuff in and out?

I am trying to make a program that converts Norwegian Krones into Swedish krones, when pressing my button. I started working with programs inside of Windows yesterday and am a complete beginner.

The problem is that I simply don't understand how I can connect the textbox and the button. I understand that something has to happen inside Knappelytter but I am not sure what.

Here is a picture of my program: http://gyazo.com/f3b0817bf6ae73985a098e5f97c9caf2)

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

class Vindu extends JFrame{
    int norskekr2 = 0; 
    int svenskekr2 = 0; 

    public Vindu(String tittel){
    setTitle(tittel); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setLayout(new FlowLayout()); 

    JButton knapp = new JButton("Regn om"); 
    add(knapp); 
    JLabel kr = new JLabel("Norske Kr");
    add(kr); 
    JTextField norskekr = new JTextField(5);
    add(norskekr);  
    JLabel skr = new JLabel("Svenske Kr");
    add(skr); 
    JTextField svenskekr = new JTextField(5); 
    svenskekr.setEnabled(false);
    add(svenskekr); 
    Knappelytter knappelytteren = new Knappelytter(); 
    knapp.addActionListener(knappelytteren);
    norskekr.addActionListener(knappelytteren);

    pack(); 
    }
}
class Knappelytter implements ActionListener{
public void actionPerformed (ActionEvent hendelse){

    //What happens right here? 


}


}
public class TilSvensk {
    public static void main(String[] args) {
        Vindu start = new Vindu("Regn om- Program"); 
        start.setVisible(true); 
    }

}

//NED EDITED CODE HERE

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

class Vindu extends JFrame{
    int norskekr2 = 0; 
    int svenskekr2 = 0; 
    private JTextField norskekr = new JTextField(5);
    private JLabel kr = new JLabel("Norske Kr");
    private JButton knapp = new JButton("Regn om"); 
    private JLabel skr = new JLabel("Svenske Kr");
    private JTextField svenskekr = new JTextField(5); 

    public Vindu(String tittel){
    setTitle(tittel); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setLayout(new FlowLayout()); 

    add(knapp); 
    add(kr); 
    add(norskekr);  
    add(skr); 
    svenskekr.setEnabled(false);
    add(svenskekr); 
    Knappelytter knappelytteren = new Knappelytter(); 
    knapp.addActionListener(knappelytteren);


    pack(); 
    }

    class Knappelytter implements ActionListener{
public void actionPerformed (ActionEvent hendelse){
 String text = norskekr.getText(); 
 double tall = Double.parseDouble(text);
 double nyttall = tall*0.80;
 String total2 = String.valueOf(nyttall);
    svenskekr.setText(total2);
}


}

}

public class TilSvensk {
    public static void main(String[] args) {
        Vindu start = new Vindu("Regn om- Program"); 
        start.setVisible(true); 
    }

}

Upvotes: 2

Views: 215

Answers (3)

Vince
Vince

Reputation: 15146

Pass the fields to the listener:

class Knappelytter implements ActionListener {
    private JTextField field;

    public Knappelytter(JTextField field) {
        this.field = field;
    }

    public void actionPerformed(ActionEvent e) {
        String text = field.getText();
    }
}

Wen you create the listener, pass the field to it:

JTextField field = new JTextField(10);
Knappelytter listener = new Knappelytter(field);

field.addActionListener(listener);

Or, if using Java 8+, you could just use a lambda expression instead of creating a new class for your listener:

field.addActionListener(event -> {
    String text = field.getText();
});

Upvotes: 3

Jack
Jack

Reputation: 384

You're using the ActionListener interface in an unusual way. Generally, it's used as an anonymous class. Using such a class changes your code to:

Knappelytter knappelytteren = new Knappelytter(); 
knapp.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) { 
        // what do we do here ?
    }
});

From here, you can add code that will access the text box's value. There's a catch, however. Variables that are outside of this anonymous class must be final. As such, you'll have to change norskekr's declaration to final JTextField norskekr = new JTextField(5);. This question may be of interest to you.

From there, you'll need to get the value of the text box (norskekr.getText()), parse it to some sort of numeric type. Whether that's int or double will depend on what you want your program to do. Then, apply the conversion rate and set the value of svenskekr with svenskekr.setText().

Upvotes: 0

maskacovnik
maskacovnik

Reputation: 3084

There you will pick the text from field, make integer, multiply by constant and put to second textfield like this:

double constant; //exchange rate
if(norskekr.getText().isEmpty()){ //converting svenske->norske
     String textfield = svenskekr.getText();
     double number=0;
     try{
         number = Double.parseDouble(textfield);
     }catch(NumberFormatException e){
         System.err.println("NaN");
         e.printStackTrace();
     }
     double result = number * constant;
     norskekr.setText(result+"");
}else if(svenskekr.getText.isEmpty()){ //converting norske->svenske
     String textfield = norskekr.getText();
     double number=0;
     try{
         number = Double.parseDouble(textfield);
     }catch(NumberFormatException e){
         System.err.println("NaN");
         e.printStackTrace();
     }
     double result = number / constant; //reverse exchange rate
     svenskekr.setText(result+"");
}

Not tested

Upvotes: 0

Related Questions