-1

I´m trying to use a JLabel as a tab for a JTabbedPane, but I don't know how to do it. Is it possible?

If not, what I'm trying is to do tabs that look like these:

Win10Tabs:

https://i.stack.imgur.com/OxnPL.png

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Yulo Vallejos
  • 17
  • 1
  • 8
  • 1
    What is unique about those tabs? I see an Icon and text, which is supported by a JTabbedPane. Or I see a black background. All Swing components have a setBackground(...) method. Read the API for methods supported by the tabbed pane. Or read the section from the Swing tutorial on [How to Use Tabbed Panes](https://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html) for some working examples. – camickr Nov 10 '18 at 04:57
  • By using normal tabs, how to align the text(not the tabs) to the left? – Yulo Vallejos Nov 10 '18 at 05:20

1 Answers1

0

how to align the text(not the tabs) to the left?

The center alignment of the icon/text is controlled by the LAF. I never find overriding the default LAF behaviour to be an easy task, since you would need to create a custom LAF for all platforms.

Another option is to determine the preferred size of each label that you use as a tab component and then set the preferred size of all these labels to be the same width. This will force left alignment.

import javax.swing.*;
import java.awt.*;

public class TabbedPaneLeft extends JPanel
{
    private JTabbedPane tabbedPane;

    public TabbedPaneLeft()
    {
        ImageIcon icon = new ImageIcon( "copy16.gif" );

        tabbedPane = new JTabbedPane();
        tabbedPane.setTabPlacement(JTabbedPane.LEFT);
        add( tabbedPane );

        initTabComponent(icon, "Tab 1");
        initTabComponent(icon, "Tabbed Pane 2");
        initTabComponent(icon, "Tab 3");

        adjustTabComponentSize();
    }

    private void initTabComponent(Icon icon, String text)
    {
        JLabel label = new JLabel( text );
        label.setPreferredSize( new Dimension(300, 300) );

        tabbedPane.addTab(null, null, label);

        JLabel tabLabel = new JLabel( text );
        tabLabel.setIcon( icon );
        //tabLabel.setHorizontalAlignment(JLabel.LEFT); // doesn't work
        //tabLabel.setAlignmentX(0.0f); // doesn't work
        tabbedPane.setTabComponentAt(tabbedPane.getTabCount() - 1, tabLabel);
    }

    private void adjustTabComponentSize()
    {
        int width = 0;

        //  Find the width of the larget tab

        for (int i = 0; i < tabbedPane.getTabCount(); i++)
        {
            Dimension d = tabbedPane.getTabComponentAt(i).getPreferredSize();
            width = Math.max(width, d.width);
        }

        //  Set the width of all tabs to match the largest

        for (int i = 0; i < tabbedPane.getTabCount(); i++)
        {
            Component tabComponent = tabbedPane.getTabComponentAt(i);
            Dimension d = tabComponent.getPreferredSize();
            d.width = width;
            tabComponent.setPreferredSize( d );
        }
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new TabbedPaneLeft());
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args) throws Exception
    {
        java.awt.EventQueue.invokeLater( () -> createAndShowGUI() );
/*
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
*/
    }

}
camickr
  • 321,443
  • 19
  • 166
  • 288