Search code examples
javaswingjtabbedpane

Modify the scroll button of a JTabbedPan


I'm working on a UI on Java with Swing, I want to change the scroll button of a JTabbedPane, so I use a new UI (MyTabbedPaneUI) that I create from the extends MetalTabbedPaneUI.

But when I create my JTabbedPanel there are 2 tabs which appear and I don't want them. If I remove them my scroll bar disappear.

The code :

  public class MyTabbedPaneUI extends MetalTabbedPaneUI{

        private Icon southIcon = new ImageIcon(MyTabbedPaneUI.class.getResource("south.png"));
        private Icon northIcon = new ImageIcon(MyTabbedPaneUI.class.getResource("north.png"));
        private Icon eastIcon  = new ImageIcon(MyTabbedPaneUI.class.getResource("flecheVerte-gauche-20px.png"));
        private Icon westIcon  = new ImageIcon(MyTabbedPaneUI.class.getResource("flecheVerte-droite-20px.png"));


        public static ComponentUI createUI( JComponent x ) {
            return new MyTabbedPaneUI();
        }


        @Override
        protected JButton createScrollButton(int direction) {

            if ((direction != SOUTH) && (direction != NORTH) && (direction != EAST) && (direction != WEST)) {
                throw new IllegalArgumentException("Direction must be one of: " + "SOUTH, NORTH, EAST or WEST");
            }


            JButton b = new JButton();

            //b.setText("");
            b.setPreferredSize(new Dimension(eastIcon.getIconWidth(), eastIcon.getIconHeight()));

            if (direction == SOUTH) {
                b.setIcon(southIcon);
            } else if (direction == NORTH) {
                b.setIcon(northIcon);
            } else if (direction == WEST) {
                b.setIcon(westIcon);
            } else {
                b.setIcon(eastIcon);
            }

            return b;
        }

    }

Solution

  • @Override
    protected JButton createScrollButton(int direction) {
        if (direction != SOUTH && direction != NORTH && direction != EAST
                && direction != WEST) {
            throw new IllegalArgumentException("Direction must be one of: "
                    + "SOUTH, NORTH, EAST or WEST");
        }
        return new ArrowButton(direction);
    }
    

    //

    public final class ArrowButton extends JButton implements UIResource, SwingConstants {
    
    protected int direction;
    private final Image left, disabledleft, pressedleft;
    private final Image right, disabledright, pressedright;
    
    public ArrowButton(int direction) {
        super();
        setDirection(direction);
        left = new ImageIcon(getClass().getResource("/resources/arrowicons/left.png")).getImage();
        disabledleft = new ImageIcon(getClass().getResource("/resources/arrowicons/disabledleft.png")).getImage();
        pressedleft = new ImageIcon(getClass().getResource("/resources/arrowicons/leftpressed.png")).getImage();
    
        right = new ImageIcon(getClass().getResource("/resources/arrowicons/right.png")).getImage();
        disabledright = new ImageIcon(getClass().getResource("/resources/arrowicons/disabledright.png")).getImage();
        pressedright = new ImageIcon(getClass().getResource("/resources/arrowicons/rightpressed.png")).getImage();
    }
    
    public int getDirection() {
        return direction;
    }
    
    public void setDirection(int direction) {
        this.direction = direction;
    }
    
    @Override
    public void paint(Graphics g) {
        boolean isPressed, isEnabled;
        int w, h, size;
        w = getSize().width;
        h = getSize().height;
        isPressed = getModel().isPressed();
        isEnabled = isEnabled();
    
        Image image = null;
        if(direction == EAST){
            if(isEnabled && isPressed){
                image = pressedright;
            }else if(!isEnabled){
                image = disabledright;
            }else{
                image = right;
            }
        }else{
            if(isEnabled && isPressed){
                image = pressedleft;
            }else if(!isEnabled){
                image = disabledleft;
            }else{
                image = left;
            }
        }
        g.drawImage(image, 0, 0, null);
    }
    
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(32, 32);
    }
    
    @Override
    public Dimension getMinimumSize() {
        return new Dimension(5, 5);
    }
    
    @Override
    public Dimension getMaximumSize() {
        return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
    }
    

    }

    //This is my solution pretty easy and usefull. You can use easily. Actually the post is 8 years old, probably it is not an issue for you anymore but somebody else can use.