How to create the object of class made in the class of swing GUI java file

Viewed 37

Here is the code of my HomePage.java file in which I have created the object of LoginPageService class to call its methods.

public class HomePage extends javax.swing.JFrame {
    CardLayout cl;
    private LoginPageService service;

    /**
    * Creates new form trial
    */
    public HomePage() {
        initComponents();
        cl = (CardLayout) (jPanel6.getLayout());
        service = new LoginPageService();
        JTable jTable = service.getScheduledLectureList(jTable1, ScheduleLecture.class);
        jTable.setRowHeight(45);
        jTable.getColumnModel().getColumn(0).setPreferredWidth(1);
        JTableHeader tableHeader = jTable.getTableHeader();
        tableHeader.setBackground(new java.awt.Color(119, 124, 168));
        tableHeader.setForeground(Color.black);
        Font headerFont = new Font("Verdana", Font.PLAIN, 19);
        tableHeader.setFont(headerFont);
    }

    private void jLabel26MouseClicked(java.awt.event.MouseEvent evt) {                                    
        service = new LoginPageService();
        String id = getId1().getText();
        char ch[] = getPassword1().getPassword();
        String password = new String(ch);
        String value = (String) jComboBox2.getSelectedItem();

        Boolean result = service.checkCredential(id, password, value);
        if (result == true) {
            JOptionPane.showMessageDialog(this, "Welcome " + id);
            if ("Student".equals(value)) {
                new SignInAsStudent().setVisible(true);
                dispose();
            }
            if ("Instructor".equals(value)) {
                new main.java.com.lecture_backup.view.SignInAsInstructor().setVisible(true);
                dispose();
            }
        } else {
            JOptionPane.showMessageDialog(this, "Invalid Id or Password");
            getPassword1().setText("");
            getId1().setText("");
        }
    }
}

this is the code of LoginPageService:

public class LoginPageService {
   private HomePage hm;
   
    public LoginPageService(){
         hm = new HomePage();
    }
    
    public JTable getScheduledLectureList(JTable jTable1, Class sdl) {
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        Session session = sf.openSession();
        Date date = new Date();
        DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
        hm.getPassword1().setText("");
        hm.getjLabel13().setText("" + df.format(date));
        Criteria crit1 = session.createCriteria(sdl);
        Criteria crit = session.createCriteria(ScheduleLecture.class);
        crit.add(Restrictions.ge("date", df.format(date)));
        List<ScheduleLecture> data = crit.list();
        DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();
        dtm.setRowCount(0);
        for (ScheduleLecture sl : data) {
            Object obj[] = {sl.getSerialNo(), sl.getSubject(), sl.getTopic(), sl.getName(), sl.getDate(), sl.getTime()};
            dtm.addRow(obj);

        }
        return jTable1;
    }

Can you please tell me is this the right way to declare the reference object first private variable, then instantiate it in the respective methods to call the methods of LoginPageService class?

I have added the code of both the files. can someone tell me now where s the problem. Why after running the code it is not directing me to HomePage.

1 Answers

Instance variables (declared inside the class and outside methods) are kept in each object with their own reference. Unless you need reference for the instance variable across multiple methods of the same class, you can declare it locally (inside method when needed) alone.

Since you are using it only once and have no further reference to it (as far as it is show in the snippet) you may move it locally as follows:

    public class HomePage extends javax.swing.JFrame {
    ...
    // private LoginPageService service; // remove this
    ...
    //service = new LoginPageService(); // remove this
    ...
    JTable jTable = new LoginPageService().getScheduledLectureList(jTable1, ScheduleLecture.class);
   
    ...                                     
    //service = new LoginPageService(); // remove this
    ...
    Boolean result = new LoginPageService().checkCredential(id, password, value);
    }

Else you can instantiate it only once via the constructor (not reccommended if you may not use it but if you will use it in every case, then proceed with this)

private LoginPageService service;

HomePage() {
    service = new LoginPageService();
}
Related