0

I'm trying to use user management levels in my java swing project. I want to disable few buttons to users which only admin can click. So in my project I designed my home page using JFrame. And I have few buttons called Stock, Invoice, Grn etc..

Home

In each button I changed access level as "public" by using properties==> cord==> Variable modifiers.

Login

But in Home JFrame I couldn't change the access level as "public".
In my login frame I when I'm logging as a user I want to code like given below. But still I can't call Home.hm.btnStock because I can't change the user level of Home.

In login interface

LogToHome lh = new LogToHome();
public static Login log;

     private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {                                         
    lh.logHome(comboUn, jpass);
}

in LogToHome java class

        public void logHome(JComboBox combo, JPasswordField jpass) {
    if (combo.getSelectedItem().equals("--SELECT--")) {
        JOptionPane.showMessageDialog(null, "Select User name");
    } else if (jpass.getText().isEmpty()) {
        JOptionPane.showMessageDialog(null, "Insert your Password");
        jpass.grabFocus();
    } else {
        try {
            String un = combo.getSelectedItem().toString();
            String pass = new String(jpass.getPassword());
            ResultSet rs = new JDBC().getData("SELECT * FROM login WHERE un='" + un.trim() + "' AND pw='" + pass + "'");
            if (rs.next()) {

                JOptionPane.showMessageDialog(null, "Login OK");
                Login.log.setVisible(false);
                if (un == "user") {
                    Home.hm = new Home();
                    Home.hm.btnStock.setenabled(false);
                    Home.hm.setVisible(true);
                } else {
                    Home.hm = new Home();
                    Home.hm.setVisible(true);
                }

            } else {
                JOptionPane.showMessageDialog(null, "Enter valid user name or password");

                jpass.setText("");
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, this.getClass().getName() + " " + e);
        }
    }

}

}

Dilini
  • 149
  • 2
  • 4
  • 13
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Reimeus Jan 23 '14 at 14:09

1 Answers1

1

"In my login frame I when I'm logging as a user I want to code like given below. But still I can't call Home.hm.btnStock because I can't change the user level of Home."

Don't compare Strings with ==. Use equals() or equalsIgnoreCase()

if (un == "user") 

Should be

if ("user".equals(un)) 

Using ==, the case below won't be true

if (un == "user") {
      Home.hm = new Home();
      Home.hm.btnStock.setenabled(false);
      Home.hm.setVisible(true);

Also this line Home.hm.btnStock.setenabled(false); looks suspect. Should be setEnabled with a capital E, unless you have a custom setenabled method

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720