1

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:

Stopping mouseMoved

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?

Community
  • 1
  • 1
domster
  • 556
  • 2
  • 8
  • 26
  • 2
    Unrelated hint on code quality: try to not *couple* your UI to the rest of your program. Meaning: create classes/methods that deal with your "data"; and then your UI should be using those classes/methods. Do not put dozens of lines of code into single listener methods. That works for a day, but when you come back next week, you will find that such code is hard to read, maintain, extend ... – GhostCat Aug 01 '16 at 15:03
  • Alright i got it.. I'm doing 3 tier programming for my project. But just hoping to find someone who could help me with my problem.. Basically closing the JOptionPane and stopping the mousemoved event temporarily and knowing how to activate back the event.. Thanks will take note in the future! @GhostCat – domster Aug 01 '16 at 15:06
  • 1
    1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Aug 01 '16 at 16:38
  • @GhostCat hey! damnn it's been more than a year.. i was just relooking at the questions i asked that weren't marked as answered. back then i think i had little knowledge about codes in general even tho it's java. i relooked at the question and your answer it made sense what you were suggesting or the solution it's correct. i don't think there's anything much.. thank you! – domster Aug 17 '17 at 04:14

1 Answers1

1

Lets answer with a question back to you: why do you want to stop the events?

Meaning: you programmed a listener that reacts to certain events. Now you are saying: at some point; I don't want "reactions"; so how do I prevent those events going into my listener?!

That is one way to look at it.

The other: simply tell your listener(s) that they should ignore incoming events, like:

Class SomeExample {
  private boolean ignoreMouseMovedEvents = false;
  ...
  @Override
  public void mouseMoved(MouseEvent e) {
    if (ignoreMouseMovedEvents) {
      return; 
    }
    ... handle move events 

Now you just need to update that boolean field whenever you want to ignore mouse move events.

But lets be precise here: I am not saying that ignoring events is always the best solution. I am merely expressing: if it is hard for you to prevent events from coming "into your system"; then maybe it is easier to let them coming; but change the way how your system reacts to them.

Finally: there is also a slightly different variation of this: instead of telling your listener to ignore events; you can also de-register your listeners from the event source. In other words: you can simply drop your listener from the frame you attached it to. And you re-add it, when events should be processed again.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Omg.. Yup it works now! I'm done with my project. Thank you so so much @GhostCat! But what if i want to call it back? Like a vice versa scenraio whereby 1st Frame, user has already had the JOptionPane closed and 2nd Frame user is going to hit the critical value. How can user from the 1st Frame have the JOptionPane to pop back up? – domster Aug 01 '16 at 15:16
  • If the answer works for you, then simply check the "accept" thingy next to it. But please avoid the "one more question" as comment approach. But heck: I assume that your two JFrames are running within the same JVM instance. If so, you have to create an object that **both** your JFrames have access to. You see, that is my initial comment: your "DB" tier is provides its functions to one, two, N UI components. Meaning: JFrame A puts some information into that "DB layer"; and JFrame B gets its information from there. – GhostCat Aug 01 '16 at 15:22
  • Frankly im unsure of how to implement.. I get the idea.. Sorry but if i may further explain the scenario. 2 Login Frames, one user logs in and the homepage pops up with the JOptionPane @frame1. Another user logs in and the homepage pops up with the JOptionPane like frame1. But once the 2nd user for frame 2 hits the critical number.. Frame shows the JOptionPane again.. – domster Aug 01 '16 at 15:32
  • As said: comments are really a bad place; we should not get into a discussion mode. Hint: step back; and try to reduce your example code. Ideally, you should write a super-short program that only shows your problem. If you can do that, you should put up a new question. If you can't do that, chances are that we really can't help you. Because even after reading your stuff 3,5 times; I am still not sure what exactly you coded so far, and what you want to do differently. – GhostCat Aug 01 '16 at 15:39
  • alright i will redo it again.. thanks for the advice.. My very first posted question. @GhostCat – domster Aug 01 '16 at 15:40
  • Hint: if possible, show your question to another human person. If they can't understand the question; and immediately asks for more input ... then chances are that we will not do better. If now such person is around; try to find one "within yourself": Meaning - really try to step back; as if you were to review somebody elses question ... and btw: welcome to Stackoverflow! – GhostCat Aug 01 '16 at 15:44
  • MouseEvent.consume() – mKorbel Aug 01 '16 at 16:52