1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
tab.addTab( titre, new ImageIcon( getClass( ).getResource( "/close.png" ) ), newPanel );
tab.setSelectedComponent( newPanel );
final int index = tab.indexOfTab( titre );
JPanel pnlTab = new JPanel( new GridBagLayout( ) );
pnlTab.setOpaque( false );
JLabel lblTitle = new JLabel( titre );
JButton btnClose = new JButton( new ImageIcon( getClass( ).getResource( "/close.png" ) ) );
btnClose.setSize( new Dimension( 12, 12 ) );
btnClose.setPreferredSize( new Dimension( 12, 12 ) );
btnClose.setMinimumSize( new Dimension( 12, 12 ) );
btnClose.setMaximumSize( new Dimension( 12, 12 ) );
GridBagConstraints gbc = new GridBagConstraints( );
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
pnlTab.add( lblTitle, gbc );
gbc.gridx++;
gbc.weightx = 0;
pnlTab.add( btnClose, gbc );
tab.setTabComponentAt( index, pnlTab );
btnClose.addActionListener( new ActionListener( )
{
@Override
public void actionPerformed( ActionEvent e )
{
String titleAt = tab.getTitleAt( index );
int tabCount = tab.getTabCount( );
for( int i = 0; i < tabCount; i++ )
{
String title = tab.getTitleAt( i );
if( titleAt.equals( title ) )
{
tab.setSelectedIndex( i );
tab.remove( tab.getSelectedIndex( ) );
}
}
}
} ); |
Partager