I'm doing a project in school and I've a distinction feature to implement but have some issues. What I need to do is that I've to check the number of beds left in my SQL database and if it goes below a critical number a JOptionPane appears.
What I'm trying to do is another scenario which is tougher, 2 Frames opened. Eg, first frame user uses the bed, it'll drop to the critical value 10, on the other frame when the user moves his mouse arrow the JOptionPane will pop up showing a message.
I've looked up in this community but I can't seem to implement the codes to make it work. I've got to a point where there's no errors but I can't seem to close the JOptionPane. Basically I would only want this event to fire only when user of either frames go below the bed count.
I'm brand new to this actionevent. I'm doing this project as 3 tier programming with MySQL database.
I've looked at these sites but to no avail:
How to temporarily disable event listeners in Swing?
Code for the Frame to show the JOptionPane
public HomePageForm() {
setTitle("Home");
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setBounds(100, 100, 640, 428);
contentPane = new JPanel();
contentPane.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
String cardioMsg = "Beds are running out for Cardiothoracic Department!";
String oncoMsg = "Beds are running out for Oncology Department!";
String orthoMsg = "Beds are running out for Orthopaedic Department!";
String pediaMsg = "Beds are running out for Pediatric Department!";
if (ignoreMouseMovedEvents) {
return;
}
ArrayList<ChangeBed> bedList = new ArrayList<ChangeBed>();
ChangeBedControl cbc8 = new ChangeBedControl();
bedList = cbc8.processCountBedsAvailableDpt4();
for (int i = 0; i < bedList.size(); i++) {
bedsForPediatric = bedList.get(i).getRowCountAvail();
}
if (bedsForPediatric <= 3) {
int valuePedia = JOptionPane.showConfirmDialog(null, pediaMsg, "URGENT", JOptionPane.WARNING_MESSAGE);
if (valuePedia == 0 || valuePedia == 2) {
ignoreMouseMovedEvents = true;
valuePedia = JOptionPane.CLOSED_OPTION;
}
}
}
});
Codes for Controller Class
public ArrayList<ChangeBed> processCountBedsAvailableDpt4() {
ChangeBed changeBed = new ChangeBed();
ArrayList<ChangeBed> bedList = new ArrayList<ChangeBed>();
bedList = changeBed.countBedsAvailableDpt4();
return bedList;
}
Entity class to retrieve from SQL DB
public ArrayList<ChangeBed> countBedsAvailableDpt4() {
ArrayList<ChangeBed> bedList = new ArrayList<ChangeBed>();
boolean rowBooleanAvail;
int rowCountAvail;
try {
myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ooadp?useSSL=false", "root", "root");
Statement myStmt = myConn.createStatement();
ResultSet myRs = myStmt.executeQuery("SELECT count(*) FROM bed WHERE bedStatus = 'Available' AND department = 'Pediatric'");
while (myRs.next()) {
rowBooleanAvail = myRs.last();
rowCountAvail = myRs.getRow();
ChangeBed cb = new ChangeBed(myRs.getInt(rowCountAvail), "");
bedList.add(cb);
}
} catch (Exception e) {
e.printStackTrace();
}
return bedList;
}
Basically I also have a Login Frame, upon logging in it opens up the frame I've shown in the above codes. At first showing a JOptionPane upon Mousemoved event showing a warning message. Once I click Ok/Cancel it closes the JOptionPane and stops the mousemoved event.
If I have another login frame whereby I login to the homepage (codes of the frame shown above), the same process will repeat whereby an optionpane will be shown.
So now I'll have 2 homepages opened and both mousemoved event deactivated and it won't fire again.
For frame 2, if I were to hit the critical value of to show the JOptionPane as soon as I move the mouse in the first homepage frame - how can I do that?