JTabbedPane 滚动按钮位置

发布于 2024-12-22 07:11:03 字数 277 浏览 2 评论 0原文

一直在使用 JTabbedPane 并尝试在使用 SCROLL_TAB_LAYOUT 特别是滚动方向按钮时对其进行自定义。

我正在扩展 BasicTabbedPaneUI,但我没有看到更改滚动按钮位置的方法或能力。环顾四周,除了使用不同的外观和感觉之外,没有看到任何人这样做。我认为控制滚动方向按钮的位置通常非常有用。

有谁知道如何在扩展 BasicTabbedPaneUI 或任何其他方法时执行此操作?

Been working with JTabbedPane and trying to customize it when using SCROLL_TAB_LAYOUT specifically with the scroll direction buttons.

I'm extending BasicTabbedPaneUI, but I don't see a method or ability to change the location of the scroll buttons. Searched around and don't see any one doing this other than just using different look and feels. Control over the location of scroll direction buttons would be very useful in general I feel.

Does anyone have any ideas how to do this while extending BasicTabbedPaneUI or any other method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疧_╮線 2024-12-29 07:11:03

我假设您想将向后滚动按钮移动到选项卡的另一侧。

BasicTabbedPaneUI 类中,有一个 createDecreaseButton 方法仅属于包(无访问修饰符)。

看来您必须使用您自己的 createDecreaseButton 版本创建自己的 BasicTabbedPaneUI 类。

I'm assuming you want to move the scroll backwards button to the other side of the tabs.

In the BasicTabbedPaneUI class, there's a createDecreaseButton method that's package only (no access modifier).

It appears you're going to have to create your own BasicTabbedPaneUI class, with your own version of createDecreaseButton.

醉殇 2024-12-29 07:11:03

我在没有使用许多其他组件的情况下更改了滚动按钮位置,而只是扩展了 BasicTabbedPaneUI 并修改了其 Paint() 方法来调整按钮位置。

这是代码:

public class MyTabbedScrollPane extends JTabbedPane {       
    public MyTabbedScrollPane ()
    {
        super ();
    }

    public MyTabbedScrollPane ( final int tabPlacement )
    {
        super ( tabPlacement );
    }

    public MyTabbedScrollPane ( final int tabPlacement, final int tabLayoutPolicy )
    {
        super ( tabPlacement, tabLayoutPolicy );
        initialize();
    }

    public void initialize() {
        setUI(new MyTabbedPaneUI());
    }

    private class MyTabbedPaneUI extends BasicTabbedPaneUI {
        private int leadingTabIndex;
        private Point tabViewPosition;
        private Component adjustedButton;
        private boolean scrollableTabLayoutEnabled() {
            return tabPane.getTabLayoutPolicy() == SCROLL_TAB_LAYOUT;
        }
        /*
         * Target button and view port utilities
         */
        private Component findBackwardButton() {
            Component[] comps = tabPane.getComponents();
            for(Component comp:comps) {
                if(comp instanceof BasicArrowButton) {
                    int direction = ((BasicArrowButton)comp).getDirection();
                    if(tabPane.getTabPlacement() == TOP || tabPane.getTabPlacement() == BOTTOM) {
                        if(direction == WEST) {
                            return comp;
                        }
                    }
                }
            }
            return null;
        }
        private JViewport findViewPort() {
            Component[] comps = tabPane.getComponents();
            for(Component comp:comps) {
                if(comp instanceof JViewport) {
                    return (JViewport)comp;
                }
            }
            return null;
        }
        /*
         * Override View port controlling (copy from BasicTabbedPaneUI.java)
         */
        public void scrollForward(int tabPlacement) {
            JViewport viewport = findViewPort();
            Dimension viewSize = viewport.getViewSize();
            Rectangle viewRect = viewport.getViewRect();

            if (tabPlacement == TOP || tabPlacement == BOTTOM) {
                if (viewRect.width >= viewSize.width - viewRect.x) {
                    return; // no room left to scroll
                }
            } else { // tabPlacement == LEFT || tabPlacement == RIGHT
                if (viewRect.height >= viewSize.height - viewRect.y) {
                    return;
                }
            }
            setLeadingTabIndex(tabPlacement, leadingTabIndex+1);
        }

        public void scrollBackward(int tabPlacement) {
            if (leadingTabIndex == 0) {
                return; // no room left to scroll
            }
            setLeadingTabIndex(tabPlacement, leadingTabIndex-1);
        }

        public void setLeadingTabIndex(int tabPlacement, int index) {
            JViewport viewport = findViewPort();
            leadingTabIndex = index;       
            Dimension viewSize = viewport.getViewSize();
            Rectangle viewRect = viewport.getViewRect();

            int offsetX = adjustedButton.getWidth()+2;

            switch(tabPlacement) {
              case TOP:
              case BOTTOM:
                tabViewPosition.x = leadingTabIndex == 0? 0-offsetX : rects[leadingTabIndex].x-offsetX;

                if ((viewSize.width - tabViewPosition.x) < viewRect.width) {
                    // We've scrolled to the end, so adjust the viewport size
                    // to ensure the view position remains aligned on a tab boundary
                    Dimension extentSize = new Dimension(viewSize.width - tabViewPosition.x, 
                                                         viewRect.height);
                    viewport.setExtentSize(extentSize);
                }
                break;
              case LEFT:
              case RIGHT:
                tabViewPosition.y = leadingTabIndex == 0? 0 : rects[leadingTabIndex].y;

                if ((viewSize.height - tabViewPosition.y) < viewRect.height) {
                // We've scrolled to the end, so adjust the viewport size
                // to ensure the view position remains aligned on a tab boundary
                     Dimension extentSize = new Dimension(viewRect.width, 
                                                          viewSize.height - tabViewPosition.y);
                     viewport.setExtentSize(extentSize);
                }
            }
            viewport.setViewPosition(tabViewPosition);
        }
        /*
         * UI Rendering
         */
        public void paint(final Graphics g, JComponent c) {
            super.paint(g, c);
            if(scrollableTabLayoutEnabled()) {
                if(adjustedButton == null) {
                    adjustedButton = findBackwardButton();
                    tabViewPosition = new Point(0-(adjustedButton.getWidth()+2), 0);
                    Component[] comps = tabPane.getComponents();
                    for(Component comp:comps) {
                        if(comp instanceof BasicArrowButton) {
                            if(comp instanceof BasicArrowButton) {
                                BasicArrowButton button = (BasicArrowButton)comp;
                                int direction = button.getDirection();
                                if(tabPane.getTabPlacement() == TOP || tabPane.getTabPlacement() == BOTTOM) {
                                    // left align the west button
                                    if(direction == WEST) {
                                        button.removeActionListener(button.getActionListeners()[0]);
                                        button.addActionListener(new ActionListener() {                                         
                                            @Override
                                            public void actionPerformed(ActionEvent e) {
                                                scrollBackward(tabPane.getTabPlacement());
                                            }
                                        });
                                    } else if(direction == EAST) {
                                        button.removeActionListener(button.getActionListeners()[0]);
                                        button.addActionListener(new ActionListener() {                                         
                                            @Override
                                            public void actionPerformed(ActionEvent e) {
                                                scrollForward(tabPane.getTabPlacement());
                                            }
                                        });
                                    }
                                }
                            }
                        }
                    }
                }
                if(adjustedButton != null && adjustedButton.isVisible()) {
                    // move the scroll button
                    int by = adjustedButton.getY();
                    adjustedButton.setLocation(0, by);
                    findViewPort().setViewPosition(tabViewPosition);
                    return;
                }
            }
        }
    }
}

I changed the scroll button position without using many other components but simply extends the BasicTabbedPaneUI and hacking its paint() method to adjust the button position.

Here is the code:

public class MyTabbedScrollPane extends JTabbedPane {       
    public MyTabbedScrollPane ()
    {
        super ();
    }

    public MyTabbedScrollPane ( final int tabPlacement )
    {
        super ( tabPlacement );
    }

    public MyTabbedScrollPane ( final int tabPlacement, final int tabLayoutPolicy )
    {
        super ( tabPlacement, tabLayoutPolicy );
        initialize();
    }

    public void initialize() {
        setUI(new MyTabbedPaneUI());
    }

    private class MyTabbedPaneUI extends BasicTabbedPaneUI {
        private int leadingTabIndex;
        private Point tabViewPosition;
        private Component adjustedButton;
        private boolean scrollableTabLayoutEnabled() {
            return tabPane.getTabLayoutPolicy() == SCROLL_TAB_LAYOUT;
        }
        /*
         * Target button and view port utilities
         */
        private Component findBackwardButton() {
            Component[] comps = tabPane.getComponents();
            for(Component comp:comps) {
                if(comp instanceof BasicArrowButton) {
                    int direction = ((BasicArrowButton)comp).getDirection();
                    if(tabPane.getTabPlacement() == TOP || tabPane.getTabPlacement() == BOTTOM) {
                        if(direction == WEST) {
                            return comp;
                        }
                    }
                }
            }
            return null;
        }
        private JViewport findViewPort() {
            Component[] comps = tabPane.getComponents();
            for(Component comp:comps) {
                if(comp instanceof JViewport) {
                    return (JViewport)comp;
                }
            }
            return null;
        }
        /*
         * Override View port controlling (copy from BasicTabbedPaneUI.java)
         */
        public void scrollForward(int tabPlacement) {
            JViewport viewport = findViewPort();
            Dimension viewSize = viewport.getViewSize();
            Rectangle viewRect = viewport.getViewRect();

            if (tabPlacement == TOP || tabPlacement == BOTTOM) {
                if (viewRect.width >= viewSize.width - viewRect.x) {
                    return; // no room left to scroll
                }
            } else { // tabPlacement == LEFT || tabPlacement == RIGHT
                if (viewRect.height >= viewSize.height - viewRect.y) {
                    return;
                }
            }
            setLeadingTabIndex(tabPlacement, leadingTabIndex+1);
        }

        public void scrollBackward(int tabPlacement) {
            if (leadingTabIndex == 0) {
                return; // no room left to scroll
            }
            setLeadingTabIndex(tabPlacement, leadingTabIndex-1);
        }

        public void setLeadingTabIndex(int tabPlacement, int index) {
            JViewport viewport = findViewPort();
            leadingTabIndex = index;       
            Dimension viewSize = viewport.getViewSize();
            Rectangle viewRect = viewport.getViewRect();

            int offsetX = adjustedButton.getWidth()+2;

            switch(tabPlacement) {
              case TOP:
              case BOTTOM:
                tabViewPosition.x = leadingTabIndex == 0? 0-offsetX : rects[leadingTabIndex].x-offsetX;

                if ((viewSize.width - tabViewPosition.x) < viewRect.width) {
                    // We've scrolled to the end, so adjust the viewport size
                    // to ensure the view position remains aligned on a tab boundary
                    Dimension extentSize = new Dimension(viewSize.width - tabViewPosition.x, 
                                                         viewRect.height);
                    viewport.setExtentSize(extentSize);
                }
                break;
              case LEFT:
              case RIGHT:
                tabViewPosition.y = leadingTabIndex == 0? 0 : rects[leadingTabIndex].y;

                if ((viewSize.height - tabViewPosition.y) < viewRect.height) {
                // We've scrolled to the end, so adjust the viewport size
                // to ensure the view position remains aligned on a tab boundary
                     Dimension extentSize = new Dimension(viewRect.width, 
                                                          viewSize.height - tabViewPosition.y);
                     viewport.setExtentSize(extentSize);
                }
            }
            viewport.setViewPosition(tabViewPosition);
        }
        /*
         * UI Rendering
         */
        public void paint(final Graphics g, JComponent c) {
            super.paint(g, c);
            if(scrollableTabLayoutEnabled()) {
                if(adjustedButton == null) {
                    adjustedButton = findBackwardButton();
                    tabViewPosition = new Point(0-(adjustedButton.getWidth()+2), 0);
                    Component[] comps = tabPane.getComponents();
                    for(Component comp:comps) {
                        if(comp instanceof BasicArrowButton) {
                            if(comp instanceof BasicArrowButton) {
                                BasicArrowButton button = (BasicArrowButton)comp;
                                int direction = button.getDirection();
                                if(tabPane.getTabPlacement() == TOP || tabPane.getTabPlacement() == BOTTOM) {
                                    // left align the west button
                                    if(direction == WEST) {
                                        button.removeActionListener(button.getActionListeners()[0]);
                                        button.addActionListener(new ActionListener() {                                         
                                            @Override
                                            public void actionPerformed(ActionEvent e) {
                                                scrollBackward(tabPane.getTabPlacement());
                                            }
                                        });
                                    } else if(direction == EAST) {
                                        button.removeActionListener(button.getActionListeners()[0]);
                                        button.addActionListener(new ActionListener() {                                         
                                            @Override
                                            public void actionPerformed(ActionEvent e) {
                                                scrollForward(tabPane.getTabPlacement());
                                            }
                                        });
                                    }
                                }
                            }
                        }
                    }
                }
                if(adjustedButton != null && adjustedButton.isVisible()) {
                    // move the scroll button
                    int by = adjustedButton.getY();
                    adjustedButton.setLocation(0, by);
                    findViewPort().setViewPosition(tabViewPosition);
                    return;
                }
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文