0

When I use if...else... statements with database connections, it won't set the icon correctly.

This is the database with its values:

Database

The JFrame has two labels on it, specifically for the icons. Where just going to focus on one of these labels for now, because they function the same way.

What happens is that when the record is extracted, one of the columns "MSGTYPE" contains either "INFORMATION", "ANNOUNCEMENT", "AWARD" or "WARNING". the JLabel's icon has to match the image with the type of message, eg. If the current record is an announcement, the icon needs to be the "announcement" icon. What's happening, is that I use if...else... statements but it defaults to the else statement. See the code below.

try {
     String sql = "Select * from app.EBULLETINS ORDER BY msgid DESC FETCH FIRST 2 ROWS ONLY";
     Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
     Class.forName("org.apache.derby.jdbc.ClientDriver");
     Connection con = (Connection) DriverManager.getConnection("jdbc:derby://localhost:1527/GTPADB", "some#uSERn@me, "some#P@$$w0rd");
     Statement stmt = con.createStatement();
     ResultSet rs = stmt.executeQuery(sql);

     if (rs.next()) {
          eBul1_Title = rs.getString("Title");
          eBul1_Msg = rs.getString("Msg");
          eBul1_Type = rs.getString("MSGTYPE");
     }

     eBul1T.setText(eBul1_Title + "/" + eBul1_Type);
     eBul1M.setText(eBul1_Msg);
     System.out.println("Setting Icons");

     if (eBul1_Type == "INFORMATION") {
          TypeImage1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Information.png")));
          System.out.println("Info");
     } 
     else if (eBul1_Type == "ANNOUNCEMENT") {
          TypeImage1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/announcement.png")));
          System.out.println("Announce");
     } 
     else if (eBul1_Type == "WARNING") {
          TypeImage1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Warning.png")));
          System.out.println("Warning");
     } 
     else if (eBul1_Type == "AWARD") {
          TypeImage1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/award.png")));
          System.out.println("Award");
     } 
     else {
          TypeImage1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Information.png")));
          System.out.println("Other");
     }
} 
catch (Exception eB1Exc) {
     JOptionPane.showMessageDialog(this, eB1Exc);
}

When run, this outputs the following printlns:

1. Setting Icons
2. Other
3. Other

The JFrame also shows it like this: (Ignore the fields that do not show)

* You'll note that the Announcement has the same icon as the information *

The Frame in all its glory

Bryan Pendleton
  • 16,128
  • 3
  • 32
  • 56
Johan Brink
  • 333
  • 5
  • 20
  • possible duplicate of [Java String.equals versus ==](http://stackoverflow.com/questions/767372/java-string-equals-versus) – Andrew Thompson Sep 08 '13 at 13:12
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Dennis Meng Dec 08 '13 at 08:06

1 Answers1

2

The problem is the String comparison. Your if statement is not resulting to true due to == operator check.

You need to use equals() method instead of == when comparing Objects/Strings.

if (eBul1_Type.equals("INFORMATION")) {

Better yet, you could use a switch statement when evaluating the Strings

switch (eBul1_Type) {
    case "INFORMATION":
          typeImage1.setIcon(...);
          System.out.println("Info");
          break;
    case "ANNOUNCEMENT":
          typeImage1.setIcon(...);
          System.out.println("Info");
          break;
    case "WARNING":
          typeImage1.setIcon(...);
          System.out.println("Warning");
          break;
    case "Award":
          typeImage1.setIcon(...);
          System.out.println("Award");
          break;
    default:
         typeImage1.setIcon(...);
         System.out.println("Other");
}

This will provide an easier syntax to manage should any bulletin types be added/removed.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Of course!!!! See, that's what happens when I continuously code for more that eight consecutive hours without taking a break. I know that, and it's common logic, but tiredness > logic. Thanks. – Johan Brink Sep 08 '13 at 13:11