Donald Abdee
Donald Abdee

Reputation: 11

How to get value from database and setColor to many JButton

I have 1 table in database

TableRoom
- NoRoom
- Status

and i have

room 
101,
102,
103,
104,
105,
201,
202,
203, 
204,
205

My problem is how to call a database and specify the colors for the buttons. if the status of the room was filled with the buttons should be colored red, and if the status of the room was empty, the button should be green

this mycode

JButton [] arr = new JButton[10];    
.........

if(r.next()){    
   status = r.getString("status");    
   noRoom = r.getString("noRoom");    
   for(int i=0; i<10; i++){    
      if (arr[i].getActionCommand().equals(noRoom)){
         r.beforeFirst();
         while(r.next()){
            arr[i].setBackground(Color.red);
         }    
         s.close(); 
         c.close();
      } else {
         arr[i].setBackground(Color.green);
      }
   }
}

Upvotes: 1

Views: 83

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347314

Start by creating a POJO of the Room... something like...

public class Room {

    private final int roomNumber;
    private final boolean status;

    public Room(int roomNumber, boolean status) {
        this.roomNumber = roomNumber;
        this.status = status;
    }

    public int getRoomNumber() {
        return roomNumber;
    }

    public boolean isOccupied() {
        return status;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 89 * hash + this.roomNumber;
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Room other = (Room) obj;
        if (this.roomNumber != other.roomNumber) {
            return false;
        }
        return true;
    }

}

for example. This allows you to contain all the information related to a individual room into a single class. Also, through the use of equals and hashcode, we don't need to keep track of a single instance for each Room object, but could have multiple instances, but which will equate to the same thing, this will make a little more sense after you....

Create a "general" utility method to load the rooms

protected List<Room> getRooms() throws SQLException {
    Connection con = null;
    List<Room> rooms = new ArrayList<>(25);
    try (PreparedStatement stmt = con.prepareStatement("select * from TableRoom")) {
        try (ResultSet rs = stmt.executeQuery()) {
            while (rs.next()) {
                Room room = new Room(rs.getInt("NoRoom"), rs.getBoolean("Status"));
                rooms.add(room);
            }
        }
    } 
    return rooms;
}

All this does is queries the database, loads the data into a series of Rooms and returns a List, nice. Now, when ever you want to check the database, you could simply call this method and get a List of Rooms

Next, you need some way to link the Room and it's button, a simple Map would suffice for this...

private Map<Room, JButton> buttons;

Next, you need to initialise the values...

    buttons = new HashMap<>(25);

    try {
        List<Room> rooms = getRooms();
        for (Room room : rooms) {
            JButton btn = new JButton(Integer.toString(room.getRoomNumber()));
            buttons.put(room, btn);
            updateRoomStatus(room);
            // Add button to UI as required
        }
    } catch (SQLException exp) {
        exp.printStackTrace();
    }

Now, when you want to update the status of the buttons you would simply do something like...

    try {
        List<Room> rooms = getRooms();
        for (Room room : rooms) {
            updateRoomStatus(room);
        }
    } catch (SQLException exp) {
        exp.printStackTrace();
    }

And finally, you would need a simple utility method, which when provided with a Room could update the state of the associated button, something like...

protected void updateRoomStatus(Room room) {

    JButton btn = buttons.get(room);
    if (btn != null) {
        if (room.isOccupied()) {
            btn.setBackground(Color.RED);
        } else {
            btn.setBackground(Color.GREEN);
        }
    }

}

Now, this is a pretty simple example, a more complex example would have you maintain a single instance of a Room and update it from the data in the database. This means that if you were to change the state any given Room, that change would be reflected in all references you had to the Room.

Also, this assumes that the number of rooms doesn't change between initialising the data and updating it, which seems to make sense, but is worth pointing out.

Have a look at JDBC Database Access and Collections Trail for more details

Upvotes: 1

Related Questions