Dulan Udayanga
Dulan Udayanga

Reputation: 3

running methods declared in a swing.Jframe Class

I have created two swing.JFrames. login GUI and user GUI. what I want is when it's switches to login gui to user gui, there is a Jlabel in user GUI which needs to be changed as ("you're logged in as" + username);

I tried this code in userjframe source code.

`loggedInAsLable.setText("you're logged in as" + username);` 

in a method and it's called in main method of user jframe. but for some reasons it doesn't work.

how can I run some methods when a Jframe is becoming visible?

public class CustomerServiceOfficerUI extends javax.swing.JFrame {

private static Statement st;
ResultSet rs;
Connection con = null;
Login loginUI = new Login(); // gets current user Id
Employee cso = new CustomerServiceOfficer();  //creates new customer service officer object

/**
 * Creates new form CustomerServiceOfficer
 */
public CustomerServiceOfficerUI() {
    initComponents();
}

public void getCSOdetails() {

    try {
        Class.forName("com.mysql.jdbc.Driver");

        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/flyingcarsdb", "root", "");
        System.out.println("database connected");

    } catch (ClassNotFoundException | SQLException ex) {
        System.out.println("Error: " + ex);
    }

    try {
        // Retrieve customer service officer details
        st = con.createStatement();
        String query = "select * FROM customerserviceofficer WHERE Id = '" + loginUI.getCurrentUserId() + "'";

        rs = st.executeQuery(query);

        while (rs.next()) {
            //Assign the details with setters
            cso.setFname(rs.getString("Fname"));
            cso.setEmail(rs.getString("Email"));
        }

    } catch (Exception ex) {
        System.out.println("Error : " + ex);
    }
loggedInAsLable.setText("you're logged in as : " + cso.getId()); 
//this is where LABLE is changed, 'cso.getId()' returns the user ID
   }

Upvotes: 0

Views: 45

Answers (2)

ragelh
ragelh

Reputation: 165

If you really need to update your JFrame when it becomes visible (as your last statement suggests), you can use the the WindowListener to call your getCSODetails() method.

public CustomerServiceOfficerUI() {
    initComponents();
    this.addWindowListener(new WindowAdapter() {

        @Override
        public void windowOpened(WindowEvent e)
        {
            this.getCSODetails();
        }

        @Override
        public void windowDeiconified(WindowEvent e)
        {
            this.getCSODetails();
        }

        @Override
        public void windowActivated(WindowEvent e)
        {
            this.getCSODetails();
        }

    });
}

I've included three activation events - opening, activation and deiconfication; you can remove any of them to limit the update to a specific event suiting your needs. If you need to update the label only once the window is opened, remove the methods windowDeiconified() and windowActivated(). Note, however, that the getCSODetails() method is designed quite poorly and calling it whenever the window becomes visible/focused would incur a performance penalty and the responsiveness of your GUI will be heavily influenced by performance of your database. I guess that the customer details you're displaying are not changed during a login session, so it would be more appropriate to perform the query once, cache the details and then display them from the cache.

Upvotes: 1

Krzysztof Cichocki
Krzysztof Cichocki

Reputation: 6414

try this:

    public class CustomerServiceOfficerUI extends javax.swing.JFrame {

    private static Statement st;
    ResultSet rs;
    Connection con = null;
    Login loginUI = new Login(); // gets current user Id
    Employee cso = new CustomerServiceOfficer();  //creates new customer service officer object

    /**
     * Creates new form CustomerServiceOfficer
     */
    public CustomerServiceOfficerUI() {
        initComponents();
    }

    public void getCSOdetails() {

        try {
            Class.forName("com.mysql.jdbc.Driver");

            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/flyingcarsdb", "root", "");
            System.out.println("database connected");




            // Retrieve customer service officer details
            st = con.createStatement();
            String query = "select * FROM customerserviceofficer WHERE Id = '" + loginUI.getCurrentUserId() + "'";

            rs = st.executeQuery(query);

            while (rs.next()) {
                //Assign the details with setters
                cso.setFname(rs.getString("Fname"));
                cso.setEmail(rs.getString("Email"));
            }

SwingUtilities.invokeLater(new Runnable()
        {

            public void run()
            {
                loggedInAsLable.setText("you're logged in as : " + cso.getId()); 
                loggedInAsLable.repaint();

            }
        });

        } catch (Throwable ex) {
            System.out.println("Error : " + ex);
SwingUtilities.invokeLater(new Runnable()
        {

            public void run()
            {
                loggedInAsLable.setText("There is a problem with your code : " + ex); 
                loggedInAsLable.repaint();

            }
        });

        } finally {

        }
    //this is where LABLE is changed, 'cso.getId()' returns the user ID
       }

Upvotes: 0

Related Questions