Suppression onglet dans jtabbedPane
Bonjour,
J'ai un JTabbedPane avec plusieurs onglets.
Lorsque je supprime un onglet inactif je m’aperçois que les tabs restant ont des index incohérents...
Comment puis-je ré-indexer mes tabs ?
Voici le code de création/suppresion d'un tab :
Code:
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( ) );
}
}
}
} ); |
Si par exemple j'ai 2 onglets ouvert.
L'onglet actif est le 2ème.
Je ferme le 1er onglet (inactif donc), le 2 ème est toujours considéré comme étant à l'index 2 alors qu'il devrait être au 1er après la suppression...
Par avance merci pour vos suggestions.