Aligning icon to the left in JTabbedPane in Nimbus Look and Feel
Asked Answered
O

2

5

I was creating an application with JTabbedPane using Nimbus look and feel

I have used this code to place tabs:

pane.addTab("Welcome",new ImageIcon("resources\\1.png"),mainPanel,"Takes to the welcome page");

I want the icon to appear on the left and

screenshot of the application

Orme answered 5/11, 2013 at 10:47 Comment(2)
So... did you try my answer? It was useful? Note this solution is L&F independent.Folketing
yupp. I didnt apply it to my app but i tried ur code and understood the concept. I liked the one with close button (similar to google chrome tab and other tabbed applications) and yeah your solution is independent of look and feel because you are aligning it inside a panel and then adding itOrme
F
10

You can set a custom component for rendering the tab title, through JTabbedPane.setTabComponentAt(int index, Component component) method:

Sets the component that is responsible for rendering the title for the specified tab. A null value means JTabbedPane will render the title and/or icon for the specified tab. A non-null value means the component will render the title and JTabbedPane will not render the title and/or icon.

Note: The component must not be one that the developer has already added to the tabbed pane.

For instance you can do this:

JLabel label = new JLabel("Tab1");
label.setHorizontalTextPosition(JLabel.TRAILING); // Set the text position regarding its icon
label.setIcon(UIManager.getIcon("OptionPane.informationIcon"));

JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT);
tabbedPane.addTab(null, new JPanel());
tabbedPane.setTabComponentAt(0, label); // Here set the custom tab component

Screenshot 1:

enter image description here


Note: using this feature you can set any Component as you wish. For instance you can make a JPanel with a JButton to close the tab:

final JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT);

ActionListener actionListener = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        JButton button = (JButton)e.getSource();
        for(int i = 0; i < tabbedPane.getTabCount(); i++) {
            if(SwingUtilities.isDescendingFrom(button, tabbedPane.getTabComponentAt(i))) {
                tabbedPane.remove(i);
                break;
            }
        }
    }
};

JLabel label = new JLabel("Tab1", UIManager.getIcon("OptionPane.informationIcon"), JLabel.RIGHT);        
JButton closeButton = new JButton("X");
closeButton.addActionListener(actionListener);

JPanel tabComponent = new JPanel(new BorderLayout());
tabComponent.add(label, BorderLayout.WEST);
tabComponent.add(closeButton, BorderLayout.EAST);

tabbedPane.addTab(null, new JPanel());
tabbedPane.setTabComponentAt(0, tabComponent); // Here set the custom tab component

Screenshot 2:

enter image description here


Update

You might want to see this topic as well: JTabbedPane: tab placement set to LEFT but icons are not aligned

Folketing answered 5/11, 2013 at 11:53 Comment(1)
Tab component is CENTERED (regardless of how JPanel is constructed). Do you know the way how to make inner panel (tab component) LEFT or RIGHT aligned without rewriting UI? Best when solution is LAF-safe.Synthetic
L
0

There is a simpler solution using HTML formatting. This is an example using html code to format text, but you can also format other elements within the tab:

final String PRE_HTML = "<html><p style=\"text-align: left; width: 230px\">"; 
final String POST_HTML = "</p></html>"; 

tabbedpane.setTitleAt(0, PRE_HTML + "your title" + POST_HTML);
tabbedpane.setTitleAt(2, PRE_HTML + "your title 2" + POST_HTML);
Longanimity answered 18/11, 2015 at 13:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.