views:

747

answers:

1

This is in conjunction to the question posed here: JTabbedPane: Components before and after the tabs themselves

I want to attach a mouse listener that allows for dragging the constructed google chrome-like frame. Starting out, the initial dragging code is rather easy, and the mouse-dragging code at this Kirill-post can be used pretty much directly. I'd only want this behaviour if the user clicks and drags on the "title bar" of the frame, that is, the area where the tabs (the stick-uppers) reside. This is also easy - just change the dragging code to only accept clicks in the upper area of the JTabbedPane, the part that contains the tabs.

However, I want to reduce the grabbable area further, and only allow click-and-drag-frame in the area NOT occupied by the tabs (the stick-uppers - anyone have a better name for this GUI element?) - again quite like Google Chrome (Chrome also adds a bar above the tabs when in windowed mode, to easier get hold of the frame if many tabs are active. But Chrome do this perfect: It is possible to grab the window in the tabbed part that doesn't have tabs, and even in the small v's inbetween the tabs!)

What I'd effectively want to do, is to be able to attach the mouse listeners to the background of the GUI for the tabs - but how to accomplish something like this?

A: 

After looking at the solutions for this question (draggable tabs in Swing), I found that the actual TabbedPaneUI has some methods that can fix both problems: Only drag window in tab area, which turned out to be the hardest part, and not drag when above the tabs themselves. The relevant code follows, in the two sections marked with "// ::". The code is adapted from the question-mentioned Kirill-code. The code doesn't handle other cases than when the tabs are at top - which makes sense when considering what I want to do.

        // mouse listener for dragging the host window
        MouseAdapter adapter = new MouseAdapter() {
            int lastX;
            int lastY;

            boolean _dragInitiated;

            @Override
            public void mousePressed(MouseEvent e) {
                TabbedPaneUI ui = _windowTabs.getUI();

                // :: Won't drag if we're positioned above a tab in tab area
                if (ui.tabForCoordinate(_windowTabs, e.getX(), e.getY()) != -1) {
                    _dragInitiated = false;
                    return;
                }

                // :: Won't drag if we're below the tab area
                int maxY = 0;
                for (int i = 0; i < _windowTabs.getTabCount(); i++) {
                    Rectangle bounds = ui.getTabBounds(_windowTabs, i);
                    int y = bounds.y + bounds.height;
                    if (y > maxY) {
                        maxY = y;
                    }
                }
                _dragInitiated = true;
                if (maxY > 0) {
                    if (e.getY() > maxY) {
                        _dragInitiated = false;
                    }
                }

                Point eventLocationOnScreen = e.getLocationOnScreen();
                if (eventLocationOnScreen == null) {
                    Component source = (Component) e.getSource();
                    eventLocationOnScreen = new Point(e.getX() + source.getLocationOnScreen().x, e.getY()
                            + source.getLocationOnScreen().y);
                }

                lastX = eventLocationOnScreen.x;
                lastY = eventLocationOnScreen.y;
            }

            @Override
            public void mouseDragged(MouseEvent e) {
                if (!_dragInitiated) {
                    return;
                }

                Point eventLocationOnScreen = e.getLocationOnScreen();
                if (eventLocationOnScreen == null) {
                    Component source = (Component) e.getSource();
                    eventLocationOnScreen = new Point(e.getX() + source.getLocationOnScreen().x, e.getY()
                            + source.getLocationOnScreen().y);
                }

                int dx = eventLocationOnScreen.x - lastX;
                int dy = eventLocationOnScreen.y - lastY;
                Window win = POTabbedFrame.this;
                Point loc = win.getLocation();
                win.setLocation(loc.x + dx, loc.y + dy);
                lastX = eventLocationOnScreen.x;
                lastY = eventLocationOnScreen.y;
            }
        };
        _windowTabs.addMouseListener(adapter);
        _windowTabs.addMouseMotionListener(adapter);
stolsvik

related questions