Brandon
Brandon

Reputation: 15

Comparing two JTextFields with an ActionListener

what I am trying to do is compare two inputs from TextFields within a JFrame using an ActionListener. If the two inputs are equal and the user hits the button, a MessageDialog will pop up and say "equal". If they are not equal, a MessageDialog will pop up and say "not equal". I have the frame and ActionListener running, I just do not know how to take the inputs from the TextFields and compare them.

For example, if the user enters something like this, Equal TextFields, this will pop up, Equal Message

Here is my Main Class:

public class LabFiveOne
{

public static void main(String[] args)
    {   
    JFrame frame = new JFrame("String Equality Program");

    JTextField tf1 = new JTextField(10);
    tf1.setActionCommand(tf1.toString());
    tfListener tfListen = new tfListener(tf1);
    JTextField tf2 = new JTextField(10);
    tf2.setActionCommand(tf2.toString());
    JButton chEq = new JButton("Check Equality");
    chEq.addActionListener(tfListen);

    JPanel nPanel = new JPanel();
    nPanel.add(tf1);
    nPanel.add(tf2);
    frame.add(nPanel, BorderLayout.NORTH);
    JPanel sPanel = new JPanel();
    sPanel.add(chEq);
    frame.add(sPanel, BorderLayout.SOUTH);
    nPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    frame.pack();

    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

And here is my ActionListener Class:

class tfListener implements ActionListener
{
    private final JTextField tf3;

    public tfListener(JTextField nameTF)
    {
        tf3 = nameTF;
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        if(e.getActionCommand().equals("abc"))
        {
            JOptionPane.showMessageDialog(null, "equal");
        }
        else
        {
            JOptionPane.showMessageDialog(null, "not equal");
        }
    }   
}

Upvotes: 0

Views: 1211

Answers (3)

smithnblack
smithnblack

Reputation: 487

EDIT: ok than try to change the constructor in your ActionListener Class to

public tfListener(JTextField tf1, JTextField tf2){

{

Hi :) just don't overthink and you should be fine. The simple way would be to implement the ActionListener directly to your Main Class like this:

public class LabFiveOne
{

public static void main(String[] args)
    {   
    JFrame frame = new JFrame("String Equality Program");

    final JTextField tf1 = new JTextField(10);
    tf1.setActionCommand(tf1.toString());
    tfListener tfListen = new tfListener(tf1);
    final JTextField tf2 = new JTextField(10);
    tf2.setActionCommand(tf2.toString());
    JButton chEq = new JButton("Check Equality");  
    chEq.addActionListener(tfListen);


    JPanel nPanel = new JPanel();
    nPanel.add(tf1);
    nPanel.add(tf2);
    frame.add(nPanel, BorderLayout.NORTH);
    JPanel sPanel = new JPanel();
    sPanel.add(chEq);
    frame.add(sPanel, BorderLayout.SOUTH);
    nPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    frame.pack();

    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

{

class tfListener implements ActionListener
{
private final String tf1text;
private final String tf2text;


public tfListener(JTextField tf1, JTextField tf2)
{
    tf1text = new String(tf1.getText());
    tf1text = new String(tf2.getText());

}

@Override
public void actionPerformed(ActionEvent e)
{
    if(tf1text.equal(tf2text))
    {
        JOptionPane.showMessageDialog(null, "equal");
    }
    else
    {
        JOptionPane.showMessageDialog(null, "not equal");
    }
}   

} }

Upvotes: 1

Kebab Programmer
Kebab Programmer

Reputation: 1219

To be honest with you, I don't think you need two classes; one for implementing the GUI and one for handling the ActionListener when you can have everything in one class like the class below

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

public class LabFiveOne implements ActionListener
{

private JFrame frame;
private JPanel nPanel, sPanel;
private JTextField tf1, tf2;
private JButton chEq;

public static void main(String[] args)
{   
    new LabFiveOne();
}

public LabFiveOne(){
    frame = new JFrame("String Equality Program");

    tf1 = new JTextField(10);
    tf2 = new JTextField(10);
    chEq = new JButton("Check Equality");
    chEq.addActionListener(this);

    nPanel = new JPanel();
    nPanel.add(tf1);
    nPanel.add(tf2);
    frame.add(nPanel, BorderLayout.NORTH);
    sPanel = new JPanel();
    sPanel.add(chEq);
    frame.add(sPanel, BorderLayout.SOUTH);
    nPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    frame.pack();

    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    String action = e.getActionCommand();

    if(action.equals("Check Equality")){
        String number1 = tf1.getText();
        String number2 = tf2.getText();

        int num1 = Integer.valueOf(number1);
        int num2 = Integer.valueOf(number2);

        if(num1 == num2){
            JOptionPane.showMessageDialog(null, "Equal");
        }
        else{
            JOptionPane.showMessageDialog(null, "Not Equal");
        }
    }
}
}

I have everything declared globally so that the ActionPerformed method will have access the values in the Textfields.

Upvotes: 0

Thomas Refflinghaus
Thomas Refflinghaus

Reputation: 96

tf1.toString();

Shows you some information from the JTextField. use another methods to get your input from the field. I mean it's the method:

tfi.getText();

Better look in a JTextField javadoc

Upvotes: 0

Related Questions