Example usage for javax.swing.plaf.basic BasicButtonUI BasicButtonUI

List of usage examples for javax.swing.plaf.basic BasicButtonUI BasicButtonUI

Introduction

In this page you can find the example usage for javax.swing.plaf.basic BasicButtonUI BasicButtonUI.

Prototype

BasicButtonUI

Source Link

Usage

From source file:it.iit.genomics.cru.igb.bundles.mi.business.MIWorker.java

/**
 * Adds a component to a JTabbedPane with a little close tab" button on the
 * right side of the tab.//from w  ww.  jav a 2 s.  c  o  m
 *
 * @param tabbedPane
 *            the JTabbedPane
 * @param c
 *            any JComponent
 * @param title
 *            the title for the tab
 */
public static void addClosableTab(final JTabbedPane tabbedPane, final JComponent c, final String title) {
    // Add the tab to the pane without any label
    tabbedPane.addTab(null, c);
    int pos = tabbedPane.indexOfComponent(c);

    // Create a FlowLayout that will space things 5px apart
    FlowLayout f = new FlowLayout(FlowLayout.CENTER, 5, 0);

    // Make a small JPanel with the layout and make it non-opaque
    JPanel pnlTab = new JPanel(f);
    pnlTab.setOpaque(false);

    // Add a JLabel with title and the left-side tab icon
    JLabel lblTitle = new JLabel(title);

    // Create a JButton for the close tab button
    JButton btnClose = new JButton("x");
    // btnClose.setOpaque(false);
    int size = 17;
    btnClose.setPreferredSize(new Dimension(size, size));
    btnClose.setToolTipText("close this tab");
    // Make the button looks the same for all Laf's
    btnClose.setUI(new BasicButtonUI());
    // Make it transparent
    btnClose.setContentAreaFilled(false);
    // No need to be focusable
    btnClose.setFocusable(false);
    btnClose.setBorder(BorderFactory.createEtchedBorder());
    btnClose.setBorderPainted(false);
    // Making nice rollover effect
    // we use the same listener for all buttons
    btnClose.setRolloverEnabled(true);
    // Close the proper tab by clicking the button

    // Configure icon and rollover icon for button
    btnClose.setRolloverEnabled(true);

    // Set border null so the button doesnt make the tab too big
    btnClose.setBorder(null);
    // Make sure the button cant get focus, otherwise it looks funny
    btnClose.setFocusable(false);

    // Put the panel together
    pnlTab.add(lblTitle);
    pnlTab.add(btnClose);

    // Add a thin border to keep the image below the top edge of the tab
    // when the tab is selected
    pnlTab.setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
    // Now assign the component for the tab
    tabbedPane.setTabComponentAt(pos, pnlTab);

    // Add the listener that removes the tab
    ActionListener listener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (JOptionPane.showConfirmDialog(new JFrame(),
                    "Are you sure you want to remove this tab? All results will be lost!", "Remove tab",
                    JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                tabbedPane.remove(c);
            }
        }
    };
    btnClose.addActionListener(listener);

    // Optionally bring the new tab to the front
    tabbedPane.setSelectedComponent(c);

}