'Where to enter the if condition in javax.swing?

Where to enter the if condition in javax.swing?

I’m creating a registration form and want to add the following conditions to the form:
• Name: can not be less than 3 letters.
• Address1 and address2: Both addresses should not be the same.
• Age: not less than 18 years.
• Height: not less than 130.
• Weight: not less than 30

But I do not know where to enter the if condition.

package com.signupform;

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

class MyFrame
        extends JFrame
        implements ActionListener {

    private Container c;
    private JLabel title;
    private JLabel name;
    private JTextField tname;
    private JLabel age;
    private JTextField tage;
    private JLabel height;
    private JTextField theight;
    private JLabel weight;
    private JTextField tweight;
    private JLabel address1;
    private JTextField taddress1;
    private JLabel add2;
    private JTextField tadd2;
    private JButton sub;
    private JTextArea tout;


    public MyFrame()
    {
        setTitle("Registration Form");
        setBounds(300, 90, 900, 600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        getContentPane().setBackground(Color.lightGray);

        c = getContentPane();
        c.setLayout(null);

        title = new JLabel("Registration Form");
        title.setFont(new Font("Arial", Font.PLAIN, 30));
        title.setBounds(300,30,300,30);
        c.add(title);

        name = new JLabel("Name");
        name.setFont(new Font("Arial", Font.PLAIN, 20));
        name.setBounds(50,100,100,20);
        c.add(name);

        tname = new JTextField();
        tname.setFont(new Font("Arial", Font.PLAIN, 15));
        tname.setBounds(150,100,190,20);
        c.add(tname);

        age = new JLabel("Age");
        age.setFont(new Font("Arial", Font.PLAIN, 20));
        age.setBounds(50,150,100,20);
        c.add(age);

        tage = new JTextField();
        tage.setFont(new Font("Arial", Font.PLAIN, 15));
        tage.setBounds(150,150,60,20);
        c.add(tage);

        height = new JLabel("Height");
        height.setFont(new Font("Arial", Font.PLAIN, 20));
        height.setBounds(50,200,100,20);
        c.add(height);

        theight = new JTextField();
        theight.setFont(new Font("Arial", Font.PLAIN, 15));
        theight.setBounds(150,200,60,20);
        c.add(theight);

        weight = new JLabel("Weight");
        weight.setFont(new Font("Arial", Font.PLAIN, 20));
        weight.setBounds(50,250,100,20);
        c.add(weight);

        tweight = new JTextField();
        tweight.setFont(new Font("Arial", Font.PLAIN, 15));
        tweight.setBounds(150,250,60,20);
        c.add(tweight);

        address1 = new JLabel("Address 1");
        address1.setFont(new Font("Arial", Font.PLAIN, 20));
        address1.setBounds(50,300,100,20);
        c.add(address1);

        taddress1 = new JTextField();
        taddress1.setFont(new Font("Arial", Font.PLAIN, 15));
        taddress1.setBounds(150,300,450,30);
        c.add(taddress1);

        add2 = new JLabel("Address 2");
        add2.setFont(new Font("Arial", Font.PLAIN, 20));
        add2.setBounds(50,350,100,20);
        c.add(add2);

        tadd2 = new JTextField();
        tadd2.setFont(new Font("Arial", Font.PLAIN, 15));
        tadd2.setBounds(150,350,450,30);
        c.add(tadd2);

        sub = new JButton("Submit");
        sub.setFont(new Font("Arial", Font.PLAIN, 15));
        sub.setBounds(150,420,100,30);
        sub.addActionListener(this);
        c.add(sub);

        tout = new JTextArea();
        tout.setFont(new Font("Arial", Font.PLAIN, 15));
        tout.setBounds(500,80,300,200);
        tout.setLineWrap(true);
        tout.setEditable(false);
        c.add(tout);

        setVisible(true);
    }


    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == sub) {
            String data1 = "Name : " + tname.getText() + "\n"
                         + "Age : " + tage.getText() + "\n";
            String data2 = "Height : " + theight.getText() + "\n"
                         + "Weight : " + tweight.getText() + "\n";
            String data3 = "Address 1 : " + taddress1.getText() + "\n"
                         + "Address 2 : " + tadd2.getText();

            tout.setText(data1 + data2 + data3 );
            tout.setEditable(false);
        }
    }

}

class Registration {

    public static void main(String[] args) throws Exception
    {
        MyFrame f = new MyFrame();

    }
}


Solution 1:[1]

If statements can be placed inside actionPerformed() method.

In your case You can do,

public void actionPerformed(ActionEvent e) {
if (e.getSource() == sub) {

if(Integer.parseInt(tname.getText().length()) > 3 && 
  address1.getText() != address2.getText())
 }
}

and goes...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1