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!

Matthew Murdoch
  • 28,946
  • 26
  • 89
  • 125
clamp
  • 30,396
  • 73
  • 193
  • 291

1 Answers1

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);
}
Adamski
  • 51,827
  • 12
  • 103
  • 150
  • 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 help – Gagan93 Nov 05 '13 at 08:10
  • 1
    @Gagan93: Please can you post this as a separate question; without seeing the exception I'm not sure I can help. – Adamski Nov 05 '13 at 08:35