Patterdigeington
Patterdigeington

Reputation: 23

How Do I Get Input From A JTextField, Then Store It In A Variable?

I've setup a JTextField, for users to input the base of the triange (It's a triangle area calculator) and I need to know how to store their input into a variable.

I tried an action listener but it game an error.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;

import javax.swing.*;

public class Triangle extends JFrame implements ActionListener{

static Scanner sc = new Scanner(System.in);

public static void findingTriangle(){

    JFrame jf = new JFrame();
    JTextField textfield = new JTextField("", 30);
    JPanel panel = new JPanel();
    JLabel jl = new JLabel("Triangle Area Calculator");
    jf.setSize(500, 500);
    jf.setVisible(true);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.add(panel);
    panel.add(jl);
    panel.add(textfield);
    }

    public static void main(String[] args){

        findingTriangle();

    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }
}

Upvotes: 0

Views: 14152

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347334

You can attach an ActionListener to the JTextField which, when the user presses the Enter button, will trigger the actionPerfomed method.

textfield.addActionListener(this);

From there, you can extract the value entered by the user using JTextField#getText.

String value = textField.getText();

You could also add JButton to the UI and attach the same ActionListener to it, as it would give a better visual cue to the user

I would also suggest you use a JSpinner or JFormattedTextField instead, as they have functionality to validate the data automatically.

Have a look at How to Use Text Fields, How to Use Formatted Text Fields, How to Use Spinners and How to Write an Action Listeners for more details

Upvotes: 1

Jens
Jens

Reputation: 69480

Define a button and add the actionListener.

Define textfield and your variable as static fields and assign textarea.getText()to your variable:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Triangle extends JFrame implements ActionListener {

    static Scanner sc = new Scanner(System.in);
    static String s;
    static JTextField textfield;
    static JButton jButton;

    public static void findingTriangle() {

        JFrame jf = new JFrame();
        textfield = new JTextField("", 30);
        jButton = new JButton("Click");
        jButton.addActionListener(new Triangle());
        JPanel panel = new JPanel();
        JLabel jl = new JLabel("Triangle Area Calculator");
        jf.setSize(500, 500);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.add(panel);
        panel.add(jl);
        panel.add(textfield);
        panel.add(jButton);
    }

    public static void main(String[] args) {

        findingTriangle();

    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        s = textfield.getText();

    }
}

Upvotes: 1

Related Questions