I have a login form (jFrame1) which has a jTextField1 to take input (employee id) from the user.

I want that the value of employee id given by user in jTextfield1 should be transfered to jLabel1 in user main frame (jFrame2) when the user press login button present in the login form (jFrame1)

Please Help !
Deadline is near to submitt my project.. and this is a big headache for me .

Thanx in Advance

email : priyanjan.kumar19@gmail.com

Dani AI

Generated

Short practical note: avoid keeping multiple top-level JFrames — they cause focus and lifecycle headaches. Use a modal dialog (JDialog) or swap cards (CardLayout) as suggested. For transferring the employee id, passing it via a constructor (as showed) works, but a cleaner, more decoupled approach is to emit a login event and let the main UI listen for it.

Listener pattern (decoupled, easy to test)

public interface LoginListener { void onLogin(String empId); }

public class LoginFrame extends JFrame {
    private final List<LoginListener> listeners = new ArrayList<>();
    public void addLoginListener(LoginListener l){ listeners.add(l); }
    // inside login button action:
    String id = textField.getText().trim();
    for (LoginListener l : listeners) l.onLogin(id);
}

public class MainFrame extends JFrame implements LoginListener {
    @Override
    public void onLogin(String id){
        SwingUtilities.invokeLater(() -> label.setText(id));
    }
}

Modal-dialog alternative (simple flow control)

LoginDialog dlg = new LoginDialog(mainFrame, true); // modal
dlg.setVisible(true);                 // blocks until closed
String id = dlg.getEmployeeId();      // retrieve entered value
label.setText(id);

Troubleshooting & tips: always update Swing components on the EDT (use SwingUtilities.invokeLater), validate and trim the input, handle null/empty ids, and dispose the login window to avoid leaks. If using a GUI builder, expose a setter or listener hook rather than manipulating components directly from another class. This complements ’s and ’s ideas while keeping components loosely coupled and easier to maintain.

Recommended Answers

All 6 Replies

In the MainFrame class call a method in the LoginForm class that returns the contents of jTextField1
and use thst to set the text of jLabel1.

Or have the button listener in the LoginFormm class get the value from the text field and call a method in the MainFrame class and pass it the value that it can set the jLabel text to.

Basically you are trying to submit the employeeId from the frame1 which is being implemented in let's say class Employee to a frame2 which is let 's say being implemented in the class User

So, what you should do is when you call the class User, you parse the text you got from the TextField as parameter.
Example below:

     public class Employee
     //some codes
     //When calling the class User
         new User(empId);
     //empId: text received from TextField(employeeId)
     //some codes   

     public class User
     //some codes
     //when defining the constructor
     //parse the employeeID from Employee
         String empId;
         public User(String empId)
         {
             this.empId=empId;
         }
     //some codes

Hope this helps dude :)

don't use two or more JFrames, use CardLayout instead, if you need popup window for input of value use JOptionPane

Than you sooo much Mr Mehfooz ..
Solution given by you is perfect and exactly to the point what was needed by me.
Thanx...

Refer

You are Welcome! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.