0

I'm creating a text adventure and I need to completely disable the mouse cursor. Not just hide it, although I already know how to do that, but disable it completely so that you must use Alt-Tab or a built-in quit button to stop. The main reason for this is because people can scroll with the mouse cursor and I need to disable that, I thought about canceling MouseEvents when they're fired but I couldn't get it to work (the listener that is.)

If someone knows how then please speak up and tell me! :)


EDIT: Whoops, I forgot my code. Here is my Console class. This is started by another class with new Console();


EDIT 2: Here are some snippets of me trying to create an invisible cursor and a mouse listener. The first one works, but the latter does not.

// Invisible cursor
Toolkit toolkit = Toolkit.getDefaultToolkit();
Point hotSpot = new Point(0,0);
BufferedImage cursorImage = new BufferedImage(1, 1, BufferedImage.TRANSLUCENT); 
Cursor invisibleCursor = toolkit.createCustomCursor(cursorImage, hotSpot, "InvisibleCursor");        
frame.setCursor(invisibleCursor);

// Adding mouse listener
frame.addMouseListener(new MouseAdapter() { 
      public void mousePressed(MouseEvent me) { 
        System.out.println(me); 
      } 
});

EDIT 3: To elaborate on the mouse listener it simply does not work. It doesn't print anything.

sam1370
  • 335
  • 2
  • 18
  • Possible duplicate of https://stackoverflow.com/questions/1984071/how-to-hide-cursor-in-a-swing-application ? – 0ddlyoko Sep 26 '17 at 18:03
  • @0ddlyoko No, because I'm not just trying to hide the mouse cursor, I'm trying to disable it. – sam1370 Sep 26 '17 at 18:08
  • Paste your code in the question – David Brossard Sep 26 '17 at 18:10
  • @DavidBrossard Is hastebin not working? It's really long code... – sam1370 Sep 26 '17 at 18:11
  • You need to paste the snippet that is relevant to the question. – David Brossard Sep 26 '17 at 18:12
  • 1
    @DavidBrossard Done – sam1370 Sep 26 '17 at 18:16
  • There are almost certainly better ways to inhibit scrolling than trying to disable the mouse. How are you implementing scrolling? Are you using a JScrollPane? Something else? – VGR Sep 26 '17 at 19:39
  • @VGR Yes, I am using a JScrollPane. I disabled scrolling but you can still scroll with the mouse cursor... – sam1370 Sep 26 '17 at 20:38
  • Do you want to show a scrollbar at all? If not, you could just use a bare [JViewport](https://docs.oracle.com/javase/9/docs/api/javax/swing/JViewport.html) instead (which is what JScrollPane uses internally). – VGR Sep 26 '17 at 21:43
  • @VGR No...so if I use a viewport there will be no scrolling whatsoever then? – sam1370 Sep 26 '17 at 23:29
  • @VGR I just tried it, and it didn't work -- I couldn't put a JTextArea in it. – sam1370 Sep 26 '17 at 23:35
  • What exactly are you trying to achieve? Do you have any use at all for scrolling? – VGR Sep 27 '17 at 13:38
  • @VGR I want the caret to stay at the bottom of the document, and no scrolling at all except by the computer putting the caret at the bottom. – sam1370 Sep 27 '17 at 17:56
  • You could attach a mouse/motion/wheel listener to the frames glass pane, just remember to make it visible, this should consume all the mouse events – MadProgrammer Sep 28 '17 at 02:48

1 Answers1

1

If you just want to prevent users from seeing old text, remove the old text from the JTextArea.

The easiest way to do it is to leave the JTextArea in a JScrollPane, and keep track of the lines yourself:

private static final int MAX_VISIBLE_LINES = 12;

private final Deque<String> lines = new LinkedList<>();

void appendLine(String line,
                JTextArea textArea) {

    lines.addLast(line);
    if (lines.size() > MAX_VISIBLE_LINES) {
        lines.removeFirst();
    }

    String text = String.join("\n", lines);
    textArea.setText(text);

    textArea.setCaretPosition(text.length());
    try {
        textArea.scrollRectToVisible(
            textArea.modelToView(text.length()));
    } catch (BadLocationException e) {
        throw new RuntimeException(e);
    }
}

Trying to commandeer the mouse on a multitasking desktop is just going to make users angry. Would you want an application preventing you from reading your e-mail?

Update:

If you want to base the number of lines of text on the JTextArea’s current height, use the JTextArea’s font metrics. I assume you don’t need to get it exactly right and it’s okay if the number is off by one or two lines. (To account for things like line wrapping would be considerably more difficult.)

private final Deque<String> lines = new LinkedList<>();

void appendLine(String line,
                JTextArea textArea) {

    FontMetrics metrics = textArea.getFontMetrics(textArea.getFont());

    JViewport viewport = (JViewport) textArea.getParent();
    int visibleLineCount = viewport.getExtentSize().height / metrics.getHeight();

    lines.addLast(line);
    while (lines.size() > visibleLineCount) {
        lines.removeFirst();
    }

    String text = String.join("\n", lines);
    textArea.setText(text);

    textArea.setCaretPosition(text.length());
    try {
        textArea.scrollRectToVisible(
            textArea.modelToView(text.length()));
    } catch (BadLocationException e) {
        throw new RuntimeException(e);
    }
}
VGR
  • 40,506
  • 4
  • 48
  • 63
  • Almost perfect, although I would want the max visible lines to correspond to the size of the screen. How would I do that? – sam1370 Sep 28 '17 at 23:05
  • Use the JTextArea’s font metrics to determine the approximate line count. Updated answer with an example. – VGR Sep 29 '17 at 14:12