skip to Main Content

I need make a JTabbedPane like this (I made the image in Photoshop):

awe

But in my look and feel (based on TabbedPaneUI: javax.swing.plaf.basic.BasicTabbedPaneUI) looks like this:

awe

How can I do it?

I’ve tried change LAF properties, but I didn’t find a solution.
If I use setBorder method the swing make this:

jtabbedpane1.setBorder(BorderFactory.createLineBorder(Color.WHITE, 1, true));

awe

Java changed only the upper left corner as outer border as image above shows.

I need a solution that might use the Paint method on an extended JTabbedPane class, but I really don’t know if this is correct or how do this.

3

Answers


  1. Chosen as BEST ANSWER

    I read the tutorial above and tried override paintComponent method in my extended JTabbedPane class, see:

    public class MyTabbedPane extends JTabbedPane {    
    
    [...]
    @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.YELLOW);
            g.drawRoundRect(getX()-12, getY()-11, getWidth()-4, getHeight()-22, 6, 6);
        }
    }
    

    The result:

    https://i.imgur.com/YLXkVRS.jpg


  2. Rounded corners are actually a boolean argument when instantiating a border, as can be seen here with BorderFactory.

    So what we can do is something like this:

        pane.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 2, true));
    

    Where “true” refers to rounded corners.

    If you are interested in customizing the border further, you will most likely have to paint it yourself, in which case I would look here for a further read.

    Edit regarding your code:

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.YELLOW);
        g.drawPolyLine(new int[]{getX(), getX() getX() + 12}, new int[]{getY() + 12, getY(), getY()});
        g.drawPolyLine(.....); // next corner
        g.drawPolyLine(.....); // next corner
    }
    

    etc. where you repeat for each corner that you want your L shape at.

    Login or Signup to reply.
  3. Here is the start of an answer.

    import javax.swing.*;
    import java.awt.Dimension;
    import javax.swing.plaf.TabbedPaneUI;
    import javax.swing.plaf.metal.MetalTabbedPaneUI;
    import java.awt.Graphics;
    import java.awt.Color;
    import java.awt.Insets;
    public class Bordered{
    
        public static void main(String[] args){
            JFrame frame = new JFrame("border check");
            JPanel content = new JPanel();
            JTabbedPane tabs = new JTabbedPane();
    
            JPanel one = new JPanel();
    
            one.add(new JLabel("first tab"));
            one.setOpaque(true);
            one.setBackground(Color.WHITE);
            JPanel two = new JPanel();
            two.add(new JLabel("second tab"));
    
            tabs.add("one", one);
            tabs.add("two", two);
    
            tabs.setUI( new MetalTabbedPaneUI(){
    
                @Override
                protected void paintContentBorder(Graphics g, int placement, int selectedIndex){
                    int width = tabPane.getWidth();
                    int height = tabPane.getHeight();
                    Insets insets = tabPane.getInsets();
                    Insets tabAreaInsets = getTabAreaInsets(placement);
                    int x = insets.left;
                    int y = insets.top;
                    int w = width - insets.right - insets.left;
                    int h = height - insets.top - insets.bottom;
                    y += calculateTabAreaHeight(placement, runCount, maxTabHeight);
    
                    h -= (y - insets.top);
                    //g.fillRoundRect(x, y, w, h, 5, 5);
                }
            });
    
            tabs.setPreferredSize(new Dimension(400, 200));
            content.add(tabs);
            frame.setContentPane(content);
            frame.pack();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
    
        }
    
    }
    

    Somethings to note, The inner panel the ones holding the jlabel have square corners. I’ve shown this by making one white. I’ve taken some of the boundary code from BasicTabbedPaneUI source code.

    They really did not make this easy to manage, but looking at the source for the MetalTabbedPaneUI you can see they draw each border as a line, and it would need to be modified to draw a curve at the ends.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search