2

Based on http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/size.html with the purpose to create a button that fits one tab from JTabbedPane How to add close button to a JTabbedPane Tab? I built the code below. It should show a mini button as is in java documentation, but even that I change the size the results are the same. What am I doing wrong please? Thanks in advance

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class buttontest {

public static void main (String[] args){

        buttontest t = new buttontest();

    }

public buttontest(){
    JFrame test = new JFrame();
    JPanel pnlTab = new JPanel();
    JButton btnClose = new JButton("x");
    btnClose.putClientProperty("JComponent.sizeVariant", "mini");

    pnlTab.add(btnClose);
    test.add(pnlTab);
    test.setVisible(true);
            }
}

EDIT ONE:

try {
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (UnsupportedLookAndFeelException e) {
        // handle exception
    } catch (ClassNotFoundException e) {
        // handle exception
    } catch (InstantiationException e) {
        // handle exception
    } catch (IllegalAccessException e) {
        // handle exception
    }
Community
  • 1
  • 1
Victor Oliveira
  • 3,293
  • 7
  • 47
  • 77
  • 4
    You can only use that property with LAF's that support that property. Nimbus LAF supports that property. – camickr Jul 12 '13 at 15:31
  • See also [`TabComponentsDemo`](http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html#eg). – trashgod Jul 12 '13 at 15:35
  • @camickr which can be called on the code using the edit one update, but I didnt see any big difference, is that really usefull? – Victor Oliveira Jul 12 '13 at 15:39
  • @trashgod I already toke a look on it but was really quick, I will take another time - thanks; Do you guys have any other Idea of what I could do? http://tips4java.files.wordpress.com/2009/09/component-border.jpg I would like something like this "x" button... – Victor Oliveira Jul 12 '13 at 15:41

1 Answers1

1

I would like something like this "x" button

You can use the actual Icon from that image.

In the ButtonTabComponent class from the demo code, you can add the following statement to the constructor of the TabButton private class:

setIcon( UIManager.getIcon("InternalFrame.closeIcon") );

Then comment out the paintComponent() code of the class and the Icon will be painted as the close button.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I will checkout later cos I'm already tired of programm; But I would like to ask, since this work like a paintComponent() it will be painted as pixel size of the icon or like a default size from the button component? thank you for the support, soon as possible I will check as correct answer if it applys – Victor Oliveira Jul 12 '13 at 18:04