mabe02 / lanterna

Java library for creating text-based GUIs
GNU Lesser General Public License v3.0
2.23k stars 243 forks source link

Scrolling though screen with mouse or mousepad #524

Open noamsuissa opened 3 years ago

noamsuissa commented 3 years ago

Hi,

I am new to this library and was wondering if someone can point me in the right direction for being able to scroll up or down in a Screen with a mouse wheel. Is this feature supported?

Thanks

noamsuissa commented 3 years ago

So I was able to get that to work with scrollLines(...) however the text gets erased at it passes though the window's boundaries until eventually there is nothing left. Any ideas on how to preserve all the printed text?

Here is my code:

TerminalFactory factory = new DefaultTerminalFactory();

    screen = new TerminalScreen(factory.createTerminal());

    if ( screen instanceof TerminalScreen ) {

        final Terminal terminal = ((TerminalScreen) screen).getTerminal();

        if ( terminal instanceof SwingTerminalFrame ) {
            final SwingTerminalFrame swingTerm = (SwingTerminalFrame) terminal;
            swingTerm.addMouseWheelListener(new MouseWheelListener() {

                @Override
                public void mouseWheelMoved(MouseWheelEvent e) {
                    int dir = (int) e.getWheelRotation();
                    screen.scrollLines(0, screen.getTerminalSize().getRows(), dir);
                    try {
                        screen.refresh();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            });
        }
    }
mabe02 commented 3 years ago

Right, so when you are using the Screen, it's like a double-buffered bitmap so there is no scrollback history. If you want to be able to scroll, I think you need to stay in pure Terminal mode (so don't create the TerminalScreen).