JTabbedPane: icon on left side of tabs
Asked Answered
S

1

9

hello i am using the nimbus look-and-feel and have a tabbedpane with an icon and text. now the icon appears on the right side of the text, while i would like to have it on the left side.

also i would like to add some spacing between the icon and the text.

thanks!

Sizzle answered 23/11, 2009 at 10:15 Comment(0)
C
17

You need to set the tab component yourself; which governs how the tab title is rendered.

// Create tabbed pane and add tabs.
JTabbedPane tabbedPane = ...

// Create bespoke component for rendering the tab.
JLabel lbl = new JLabel("Hello, World");
Icon icon = new ImageIcon(getClass().getResource("/foo/bar/hello.jpg"));
lbl.setIcon(icon);

// Add some spacing between text and icon, and position text to the RHS.
lbl.setIconTextGap(5);
lbl.setHorizontalTextPosition(SwingConstants.RIGHT);

// Assign bespoke tab component for first tab.
tabbedPane.setTabComponentAt(0, lbl);

Obviously you could encapsulate this in a utility method:

private void addTab(JTabbedPane tabbedPane, Component tab, String title, Icon icon) {
  tabbedPane.add(tab);

  JLabel lbl = ... // Create bespoke label for rendering tab title.

  tabbedPane.setTabComponentAt(tabbedPane.getTabCount() - 1, lbl);
}
Charqui answered 23/11, 2009 at 10:46 Comment(2)
i was using this code to add (component,string,icon and tooltip) to the tab pane.addTab("Name of Tab", new ImageIcon("resources\\1.png"), mainPanel," tooltip text"); now i want the same (the text to appear on right and icon on left) i tried the same u specified in ur comment above ...and i got exception in my code ...please helpStreetlight
@Gagan93: Please can you post this as a separate question; without seeing the exception I'm not sure I can help.Charqui

© 2022 - 2024 — McMap. All rights reserved.