0

i'm trying to disable some JButton with different account(something like permissions), here is my code to be more clear question...

try
    {
        stat = conn.prepareStatement(sql);
        rs=stat.executeQuery();
        while(rs.next())
        {
           System.out.println("found");
           String _name= rs.getString("name");
           String _pass = rs.getString("password");
           String _stat = rs.getString("status");
            if (_name == name && pass == _pass && _stat == "admin")
            {
                new SecondFrame().setVisible(true);//all buttons works as admin
            }  
            else if(_name == name && pass == _pass && _stat == "moderator")
            {
               SecondFrame ob = new SecondFrame();
               ob.admin_btn.setEnabled(false);//just user+moderator button works
            }
            else if(_name == name && pass == _pass && _stat == "user")
            {
                SecondFrame ob = new SecondFrame();

                ob.admin_btn.setEnabled(false);
                ob.moderator_btn.setEnabled(false);
                // just user button works
            }
        }           
    } 
    catch (SQLException SQLe)
    {
        System.out.println("not executed"+SQLe);
    }

... but i can't disable buttons like this(syntax is wrong), is there a way to make buttons disabled from this class?

thanks for help

  • Are you inside the main application class, or are you in a custom class? – Sirmyself Feb 02 '18 at 20:48
  • Your code suggest that `User` class has a `Button` or `JButton` field. AFAIK this is not recommended approach... – zlakad Feb 02 '18 at 20:50
  • Please read [how to compare Strings in Java](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). – Juan Carlos Mendoza Feb 02 '18 at 20:51
  • Of course, there is `String` comparison issue. – zlakad Feb 02 '18 at 20:52
  • @Sirmyself , i'm not sure what you mean, i have some bad ClassName, let me fix this for all – mhamad arsalan Feb 06 '18 at 13:13
  • @JuanCarlosMendoza it's a basic error as i know, my real problem is "how to disable this button when you login with different account", thank you:) – mhamad arsalan Feb 06 '18 at 13:31
  • @zlakad previous comment said same thing! AFAIK my question is required someone knows (JavaOOP) well, it's about (disabling button in another class), – mhamad arsalan Feb 06 '18 at 13:34
  • i'm using NetBeans8.2, because NetBeans is creating buttons in `generate code` i guess it's require someone like (Java Advance) to edit this code, it's too complicate,(is this impossible?) – mhamad arsalan Feb 06 '18 at 13:43

1 Answers1

0

1. String comparison error

Your main error is that you compare Strings with == in your code. In Java, this comparator will only work properly with basic types like long, double, boolean etc. therefore : use _stat.equals("moderator").

The reason why == won't work is that it compares the object's memory address instead of the inner values.

here's a code you can use :

try {
    stat = conn.prepareStatement(sql);
    rs=stat.executeQuery();
    while(rs.next()) {
        System.out.println("found");
        String _name= rs.getString("name");
        String _pass = rs.getString("password");
        String _stat = rs.getString("status");
        if (_name.equals(name) && pass.equals(_pass) && _stat.equals("admin")) {
            new User().setVisible(true);//all buttons works as admin
        } else if(_name.equals(name) && pass.equals(_pass) && _stat.equals("moderator")) {
            SecondFrame ob = new SecondFrame();
            ob.admin_btn.setEnabled(false);//just user+moderator button works
        } else if(_name.equals(name) && pass.equals(_pass) && _stat.equals("user")) {
            SecondFrame ob = new SecondFrame();

            ob.admin_btn.setEnabled(false);
            ob.moderator_btn.setEnabled(false);
            // just user button works
        }
    }           
} catch (SQLException SQLe) {
    System.out.println("not executed"+SQLe);
}

2. Access related issue

After that, your code may still not work since you may have an access issue. Check if your SecondFrame class's button attributes are public. If they are not, you would better create a method that will set enabled inside that class for you with the user's access. Something like this :

public void setButtonAccess (String pAccess) {
    user_button.setEnabled(false);
    moderator_btn.setEnabled(false);
    admin_btn.setEnabled(false);

    if (pAccess.equals("user")) {
        user_btn.setEnabled(true);
    } else if (pAccess.equals("moderator")) {
        user_btn.setEnabled(true);
        moderator_btn.setEnabled(true);
    } else if (pAccess.equals("admin")) {
        user_button.setEnabled(true);
        moderator_btn.setEnabled(true);
        admin_btn.setEnabled(true);
    } 
}

Although the use of an enum would suit this situation pretty well... But for more information, I suggest the following readings :

  1. Enum Types in Java
  2. The switch statement
Sirmyself
  • 1,464
  • 1
  • 14
  • 29
  • ok.... i'm trying to understand what are you want to tell me, sorry i'm (beginner something) of Java, where should i define `void setButtonAccess(String pAccess)`? i edited my question now my class name is easier to work with it, thank you :) – mhamad arsalan Feb 06 '18 at 13:26
  • In you second frame. In OOP, you ideally tell the object to do something and try to avoid "asking" the object to get something so that something else can do it. In other words, where something is is where that thing is managed. So the frame containing the buttons should also have a method to manage those buttons and this method should be inside the frame's class. – Sirmyself Feb 06 '18 at 14:11