1

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;
        }

    }
kleopatra
  • 51,061
  • 28
  • 99
  • 211
Fred37b
  • 822
  • 2
  • 10
  • 29

2 Answers2

1
@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.

Kazeka SDL
  • 38
  • 4
  • Thanks Kazeka, Have you a link to show the result ? So I can validate your answer. I do not use Swing anymore so I cannot test on my computer – Fred37b Apr 15 '20 at 07:12
  • https://github.com/kadirkoca/TorosProject this is the final project that includes the example – Kazeka SDL Apr 19 '20 at 06:26
1

please see my post for clarifying

enter image description here

based on code, notice required add tb.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Yes I want to change these two buttons with picture which I create – Fred37b Mar 14 '12 at 16:27
  • In `BasicTabbedPaneUI`, see `private class ScrollableTabButton extends BasicArrowButton` for a simple implementation. – trashgod Mar 14 '12 at 16:45
  • @Fred B sorry I haven't any luck with replacing these four AbstractButtons its Icon, you have look for (I lost link) full customized BasicTabbedPaneUI, these AbstractButtons must accepting preferredSize, the real success is use Custom Look and Feel, some of them fully override BasicTabbedPaneUI – mKorbel Mar 14 '12 at 17:05