1

I'm creating a Java application and I want to know when a JDialog window appears and blocks the other frames how I can change the cursor icon when it's outside of the dialog.

I mean when the cursor is within the dialog it will look normal, and when it's outside of the dialog I want to change the cursor so the user knows that the main frame is blocked until he closes the current dialog.

Boann
  • 48,794
  • 16
  • 117
  • 146
SlimenTN
  • 3,383
  • 7
  • 31
  • 78
  • *"block the other frames"* Are these other `JFrame` objects created by your code, other apps. on the system, or something else? For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example). – Andrew Thompson Sep 29 '14 at 10:02
  • no it's frames created by me in the application – SlimenTN Sep 29 '14 at 10:03
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Sep 29 '14 at 10:03
  • sorry this is not what I'm looking for. I just want to know how to change the cursor icon when it's inside and outside the JDialog – SlimenTN Sep 29 '14 at 10:05
  • *"this is not what I'm looking for."* That's why I didn't enter it as an answer, but as soon as you address that and post an MCVE I'll pay more attention to the rest of the problem. – Andrew Thompson Sep 29 '14 at 10:07
  • lol. I saw the post that's why I said "this is not what I'm looking for" but thnx for ur reply anyway. – SlimenTN Sep 29 '14 at 10:09

1 Answers1

0

This doesn't appear to be possible. I was going to suggest a basic loop that would iterate open windows and set all those other than the dialog to a different cursor:

    JDialog dialog = new JDialog(ownerFrame, "dialog", true); // modal
    dialog.setLocationRelativeTo(ownerFrame);

    Cursor cursorInvalid;
    try {
        cursorInvalid = Cursor.getSystemCustomCursor("Invalid.32x32");
    } catch (HeadlessException | AWTException e) {
        throw new RuntimeException(e);
    }

    Map<Window,Cursor> previousCursors = new HashMap<>();
    for (Window w : Window.getWindows()) {
        if (w != dialog) {
            previousCursors.put(w, w.getCursor());
            w.setCursor(cursorInvalid);
        }
    }

    dialog.setVisible(true);

    for (Window w : Window.getWindows()) {
        w.setCursor(previousCursors.get(w));
    }

This does change the cursor property of the windows successfully, but while the modal dialog is open, then the cursor property of those windows is apparently ignored and they always display a default arrow. I also tried setting the cursor property of individual components of the window, but again, as soon as the modal dialog is opened, the cursor property is ignored.

I tested this on Windows. The behavior is quite possibly platform-dependent, so it might work on other operating systems.

However, even if it works, I'd like to suggest that it's not a good idea do it. It risks confusion to defy user's expectations of how basic windows and dialogs work. The operating system already has its own rules for that. On Windows for example, attempting to interact with a window blocked by a modal dialog causes a beep sound and the modal dialog's title bar is flashed, and that is well-understood behavior.

Boann
  • 48,794
  • 16
  • 117
  • 146