0

I have a jList and buttons in my project. My purpose is when I click on the item on the jList, the background color of the buttons needs to be changed. In my case, the value that I am returning when I click on the item is either "OK" or "NO". So when the item clicked in the jList, I am returning corresponded "OK" or "NO" without any problem. However, the background color doesn't change. Here is the code that I use:

private void listBoxProjectsValueChanged(javax.swing.event.ListSelectionEvent evt) {                                             

    String value = (String)listBoxProjects.getSelectedValue();
    String sql = "SELECT * FROM PBL_Projects where ProjectName=?";

    try {

         Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
         String url = "jdbc:sqlserver://webserver:1111; databaseName=sinfo;integratedSecurity=true;";

         Connection conn = DriverManager.getConnection(url);
         PreparedStatement pst = conn.prepareStatement(sql);
         pst.setString(1, value);
         ResultSet rs = pst.executeQuery();
         if(rs.next()){

             String studentName = rs.getString("StudentName");
             String projectName = rs.getString("ProjectName");
             String projectDetails = rs.getString("ProjectDetails");
             String t1s1 = rs.getString("T1S1");

             txtDisplayProjectName.setText(projectName);
             txtDisplayStudentName.setText(studentName);
             txtAreaDisplayProjectDetails.setText(projectDetails);
             lblResult.setText(t1s1);

             if(t1s1=="NO"){
                 btnT1S1.setBackground(Color.RED);
             }else if(t1s1 == "OK"){
                 btnT1S1.setBackground(Color.GREEN);
             }else{
                 btnT1S1.setBackground(Color.BLUE);
             }

         }


    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }

}  

In here, I can see the other properties like ProjectName, StudentName, and others. I use very simple if statement to change the background color of the buttons. But it won't let me.

Fabian
  • 279
  • 2
  • 4
  • 16

1 Answers1

1

I was comparing String using double equals operator. It should be .equals method.

if(t1s1.equals("NO")){
                 comboT1S1.setBackground(Color.red);
                 btnT1S1.setBackground(Color.RED);
                 btnT1S1.setText("NO");
             }else if(t1s1.equals("OK")){
                 btnT1S1.setBackground(Color.GREEN);
                 btnT1S1.setText("OK");
             }else if (t1s1.equals("")){
                 btnT1S1.setBackground(Color.BLUE);
             }
Fabian
  • 279
  • 2
  • 4
  • 16