Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
561 views
in Technique[技术] by (71.8m points)

java - Aligning icon to the left in JTabbedPane in Nimbus Look and Feel

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...