Search code examples
javaswingjtabbedpanenimbus

Is there any way to fit an image in a tab component


I'm adding icons to tabs but i want the ImageIcon fits all the tabComponent.

enter image description here

I tried this code

ImageIcon icon = new ImageIcon("images/itemtexto-off.png");
Image img = icon.getImage() ;  
Image newimg = img.getScaledInstance( 50, 25,  java.awt.Image.SCALE_DEFAULT ) ;  
icon = new ImageIcon( newimg );
tabbedPaneProductDetail.setIconAt(0, icon);

Also i tried this as a solution but not worked.

JLabel label = new JLabel(icon);
label.setBackground(Color.BLUE);
tabbedPaneProductDetail.setTabComponentAt(1,label);

Solution

  • I found a solution, i don't know if it's the proper one, thanks to @camickr

    tabbedPane.setUI(new SynthTabbedPaneUI(){
    
    Insets insets =new Insets(0, 0, 0, 0);
    
    @Override
    protected Insets getTabInsets(int tabPlacement,
                      int tabIndex){
                      return insets;
    }
    
    });
    

    enter image description here

    UPDATE

    I found another solution setting this property

    UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab.contentMargins",  new Insets(0, 0, 0, 0));