Search code examples
javaswingnetbeansjframejtextfield

How to pass the inputted value of textfield (JFrame1) to label (JFrame2)


I'm stuck in my program for a long time. The reason is, i do not know how to pass the inputted text in textfield of JFrame1 to the label located in JFrame2. The inputted text in textfield should be displayed as label in JFrame 2. I'm a newbie in java so I really don't have any idea of what code am i going to use. please help me.

My JFrame1 is LoginForm.

enter image description here

Here's the code if you need:

import java.sql.*;
import javax.swing.*;

public class LoginForm extends javax.swing.JFrame {

Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;

public LoginForm() {
    initComponents();
    conn = dbconnect.ConnectDB();
}


private void b_loginMouseClicked(java.awt.event.MouseEvent evt) {                                     
    // TODO add your handling code here:

String sql = "select * from tbl_login where username = '"+username.getText()+"' and password = '"+password.getText()+"'";
        try {
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
                if (rs.next()){
                    String type = rs.getString("type");
                        if (type.equals("admin")) {
                            AdminHome ah = new AdminHome();
                            ah.setVisible(true);
                        }
                        else {
                            EmployeeHome eh = new EmployeeHome();
                            eh.setVisible(true);
                        }
                }
                else {
                    JOptionPane.showMessageDialog(null, "Incorrect username or password.");
                }
        }
        catch (Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
}                                    

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new LoginForm().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton b_login;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPasswordField password;
private javax.swing.JTextField username;
// End of variables declaration                   
}

Here's JFrame2 which is EmployeeHome

enter image description here

Here's the code of JFrame2:

import java.sql.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;


public class EmployeeHome extends javax.swing.JFrame {

Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;

/**
 * Creates new form EmployeeHome
 */
public EmployeeHome() {
    initComponents();
    conn = dbconnect.ConnectDB();
    DateTime ();
}


public void DateTime (){

    Thread clock = new Thread (){
        public void run () {
            for (;;){
                Calendar cal = new GregorianCalendar ();
                String month = cal.getDisplayName(Calendar.LONG, Calendar.MONTH, Locale.getDefault());
                int day = cal.get(Calendar.DAY_OF_MONTH);
                int year = cal.get(Calendar.YEAR);
                String week;
                    if (cal.get(Calendar.DAY_OF_WEEK) == 1) {
                        week = "Sunday";
                    }
                    else if (cal.get(Calendar.DAY_OF_WEEK) == 2) {
                        week = "Monday";
                    }
                    else if (cal.get(Calendar.DAY_OF_WEEK) == 3) {
                        week = "Tuesday";
                    }
                    else if (cal.get(Calendar.DAY_OF_WEEK) == 4) {
                        week = "Wednesday";
                    }
                    else if (cal.get(Calendar.DAY_OF_WEEK) == 5) {
                        week = "Thursday";
                    }
                    else if (cal.get(Calendar.DAY_OF_WEEK) == 6) {
                        week = "Friday";
                    }
                    else {
                        week = "Saturday";
                    }
                l_date.setText(week + ", " + month + " " + day + ", " + year);

                int hour = cal.get(Calendar.HOUR);
                int minute = cal.get(Calendar.MINUTE);
                int second = cal.get(Calendar.SECOND);
                String ampm;
                    if (cal.get(Calendar.AM_PM) == 0) {
                        ampm = "AM";
                    }
                    else {
                        ampm = "PM";
                    }
                l_time.setText(hour + " : " + minute + " : " + second + ampm);
                    try {
                        sleep (1000);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(EmployeeHome.class.getName()).log(Level.SEVERE, null, ex);
                    }
            }
        }
    };
    clock.start();
}


/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new EmployeeHome().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton b_logout;
private javax.swing.JButton b_tin;
private javax.swing.JButton b_tout;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JLabel l_address;
private javax.swing.JLabel l_age;
private javax.swing.JLabel l_bdate;
private javax.swing.JLabel l_contact;
private javax.swing.JLabel l_date;
private javax.swing.JLabel l_dept;
private javax.swing.JLabel l_id;
private javax.swing.JLabel l_name;
private javax.swing.JLabel l_time;
private javax.swing.JLabel l_username;
// End of variables declaration                   
}

My goal is to pass the inputted text to username textbox (image1) to the label next to username label (image2). Can you help me how to do this?


Solution

  • You can inject that property when you create your another JFrame instance.

    Example:

    EmployeeHome eh = new EmployeeHome(myTextField.getText());
    

    Or

    EmployeeHome eh = new EmployeeHome();
    eh.setUserNameText(myTextField.getText());
    

    You need to provide that methods in EmployeeHome. So change this class adding that.

    public EmployeeHome(String userName) {
        initComponents();
        userLabel.setText(userName);
        conn = dbconnect.ConnectDB();
        DateTime ();
    }
    

    Or

    public void setUserNameText(String name){
        this.userLabel.setText(name);
    }