JTabbedPane滚动按钮位置

时间:2011-12-20 18:08:36

标签: java swing look-and-feel jtabbedpane

正在使用JTabbedPane并尝试使用SCROLL_TAB_LAYOUT专门针对滚动方向按钮进行自定义。

我正在扩展BasicTabbedPaneUI,但我没有看到改变滚动按钮位置的方法或能力。搜索周围,除了使用不同的外观和感觉之外,没有看到任何人这样做。一般来说,控制滚动方向按钮的位置非常有用。

在扩展BasicTabbedPaneUI或任何其他方法时,有没有人有任何想法?

2 个答案:

答案 0 :(得分:2)

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

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

您似乎必须使用自己的BasicTabbedPaneUI版本创建自己的createDecreaseButton课程。

答案 1 :(得分:0)

我在不使用许多其他组件的情况下更改了滚动按钮位置,但只是扩展了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;
                }
            }
        }
    }
}