removing tabs from a JTabbedPane

hi there,having problems removing tabs from a JTabbedPane, my application adds mulitple tabs dynamically, if i open over 4 tabs for example and jump to close the third tab, it the 4th. tab to the right is visible but is not updated and cannot be closed after closing the 3rd. tab from 4.?

Any help is greatly appreciate!!!!

public JTabbedPane tabber =new JTabbedPane(){

publicvoid removeTabAt(int tab){

super.removeTabAt( tab );

int after = getSelectedIndex();

// The selected tab remained the same after this remove, consider

// it a state changed

if (after == tab){

fireStateChanged();

}}};;

//adding a tab if it's not already open on -screen

if(rTrue ==false){

rTrue =true;

tabber.addTab("firstTab",null,scrollPane,"1");

tabIndex = tabber.getTabCount()-1;

tabber.setSelectedIndex(tabIndex);

//assign an integer to the tab Index to locate position

int rp = tabIndex;

tabber.revalidate();

tabber.repaint();

}

//else if the tab is already open and the user tries to open it again,jump to it

elseif (rTrue ==true)

{

tabber.setSelectedIndex(rp);

}

//.....................................................................................

//removing a tab

if (tabIndex== rp && rTrue ==true){

tabber.remove(rp);

rTrue =false;

tabIndex--;

tabber.revalidate();

tabber.repaint();

}

[2659 byte] By [noel_D] at [2007-9-30 18:08:50]
# 1

That is because tab 4 is now tab 3 after removing the previous tab 3.

You would be better off by looking up the tab index then removing it.

The following methods would work...

JTabbedPane.indexOfTab(String title)

JTabbedPane.indexOfComponent(Component component)

Then your code would look something like

int index tabbedPane.indexOfTab( title );

tabbedPane.removeTabAt( index );

ToddCorley at 2007-7-6 18:40:06 > top of Java-index,Archived Forums,Socket Programming...
# 2

Thanks for the prompt response, i taught my code works right for getting the index of the tab to be removed, but when i remove it, it's the update thats the problem, i taught by overwriting the remove method to cause a fireStateChanged();would update the index change and solve this....i have tried using indexofTab() but it's seems to still show the same problem..ne further ideas?

thanks again

noel_D at 2007-7-6 18:40:06 > top of Java-index,Archived Forums,Socket Programming...
# 3

Ok, I guess I didn't fully understand your question/problem.

Your problem as I now understand it:

When you remove the selected tab from the tabbed pane, the UI does not update correctly.

If you could post code that reproduces the problem it would help.

For now I'll see if I can recreate the issue on my end.

ToddCorley at 2007-7-6 18:40:06 > top of Java-index,Archived Forums,Socket Programming...
# 4

With the following, I couldn't reproduce your issue.

Are you by any chance trying to remove the tab from a thread that is not the event thread?

import javax.swing.*;

import javax.swing.event.*;

import java.awt.*;

import java.awt.event.*;

import java.util.TreeSet;

public class RemoveTabTest extends JFrame implements ChangeListener, ActionListener

{

JTabbedPane tabbedPane;

int tabCount;

JButton addBtn;

JButton removeBtn;

JTextField textField;

public RemoveTabTest()

{

setDefaultCloseOperation( EXIT_ON_CLOSE );

tabbedPane = new JTabbedPane();

textField = new JTextField();

tabbedPane.addChangeListener( this );

addTab();

addTab();

addTab();

JPanel contPanel = new JPanel();

removeBtn = new JButton("Remove");

removeBtn.addActionListener( this );

addBtn = new JButton( "Add");

addBtn.addActionListener( this );

contPanel.add( textField, BorderLayout.NORTH );

contPanel.add( addBtn, BorderLayout.WEST );

contPanel.add( removeBtn, BorderLayout.EAST );

getContentPane().add( tabbedPane, BorderLayout.CENTER );

getContentPane().add( contPanel, BorderLayout.SOUTH );

pack();

setLocationRelativeTo( null );

setVisible( true );

}

public static void main( String[] a )

{

try

{

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

}

catch( Exception e )

{

}

new RemoveTabTest();

}

private void addTab()

{

String title = "Tab "+tabCount++;

tabbedPane.addTab( title, new JLabel( title ) );

}

private void removeTab( String title )

{

try

{

tabbedPane.removeTabAt( tabbedPane.indexOfTab( title ) );

}

catch( Exception exc )

{

}

}

public void stateChanged(ChangeEvent e)

{

textField.setText( tabbedPane.getTitleAt( tabbedPane.getSelectedIndex() ) );

}

public void actionPerformed(ActionEvent e)

{

if( e.getSource().equals( addBtn ) )

{

addTab();

}

else if( e.getSource().equals( removeBtn ) )

{

removeTab( textField.getText() );

}

}

}

ToddCorley at 2007-7-6 18:40:06 > top of Java-index,Archived Forums,Socket Programming...
# 5

cheers again for the response!

i have just ran ur example..it acts similiar, if u click on tab 1 from your on screen example and click remove, it removes the tab and tab2 jumps to location where tab1 was, then click remove again to remove tab 2 ,it doesn't remove,this is what i get! any tabs to te right of the tab i remove,cannot be removed! my code is very similiar to that u posted! thanks again

noel_D at 2007-7-6 18:40:06 > top of Java-index,Archived Forums,Socket Programming...
# 6

The example removes the tab based on the title in the text box.

When you click on a tab the text is set in the text box.

The reason the second remove is not being done, is due to the text in the field (from the deleted tab) not matching any of the existing tab titles.

If you change the code to the following it continues to remove tabs, by changing the text to the current tab title after deleting a tab.

import javax.swing.*;

import javax.swing.event.*;

import java.awt.*;

import java.awt.event.*;

import java.util.TreeSet;

public class RemoveTabTest extends JFrame implements ChangeListener, ActionListener

{

JTabbedPane tabbedPane;

int tabCount;

JButton addBtn;

JButton removeBtn;

JTextField textField;

public RemoveTabTest()

{

setDefaultCloseOperation( EXIT_ON_CLOSE );

tabbedPane = new JTabbedPane();

textField = new JTextField();

tabbedPane.addChangeListener( this );

addTab();

addTab();

addTab();

JPanel contPanel = new JPanel();

removeBtn = new JButton("Remove");

removeBtn.addActionListener( this );

addBtn = new JButton( "Add");

addBtn.addActionListener( this );

contPanel.add( textField, BorderLayout.NORTH );

contPanel.add( addBtn, BorderLayout.WEST );

contPanel.add( removeBtn, BorderLayout.EAST );

getContentPane().add( tabbedPane, BorderLayout.CENTER );

getContentPane().add( contPanel, BorderLayout.SOUTH );

pack();

setLocationRelativeTo( null );

setVisible( true );

}

public static void main( String[] a )

{

try

{

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

}

catch( Exception e )

{

}

new RemoveTabTest();

}

private void addTab()

{

String title = "Tab "+tabCount++;

tabbedPane.addTab( title, new JLabel( title ) );

}

private void removeTab( String title )

{

try

{

tabbedPane.removeTabAt( tabbedPane.indexOfTab( title ) );

// HERE IS THE CHANGE From the first ex

textField.setText( tabbedPane.getTitleAt( tabbedPane.getSelectedIndex() ) );

}

catch( Exception exc )

{

}

}

public void stateChanged(ChangeEvent e)

{

textField.setText( tabbedPane.getTitleAt( tabbedPane.getSelectedIndex() ) );

}

public void actionPerformed(ActionEvent e)

{

if( e.getSource().equals( addBtn ) )

{

addTab();

}

else if( e.getSource().equals( removeBtn ) )

{

removeTab( textField.getText() );

}

}

}

I am back to suspecting that you are trying to remove a tab that isn't there anymore.

In your removeTabAt method, to a printout of the tab count and which tab you are trying to remove.

ToddCorley at 2007-7-6 18:40:06 > top of Java-index,Archived Forums,Socket Programming...
# 7

hi todd,

i have ran ur current updated version,maybe i am doing something wrong but it still isn't updating the textField after a tab is removed..take the scenario where u remove tab 1 and tab2 moves to position tab 1..but the textfield value dosen't change and therefore the tab isn't removed. do u get the same result?

noel_D at 2007-7-6 18:40:06 > top of Java-index,Archived Forums,Socket Programming...
# 8

There does see to be a Swing issue here, but only when you remove the last tab.

The double call to removeAll() below, wrapped in a catch seems to work, but is REALLY ugly.

private void removeTab( String title )

{

try

{

if( tabbedPane.indexOfTab( title ) <= 0 && tabbedPane.getTabCount() == 1 )

{

textField.setText("");

try

{

tabbedPane.removeAll();

}

catch( Exception e )

{

tabbedPane.removeAll();

}

}

else

{

tabbedPane.removeTabAt( tabbedPane.indexOfTab( title ) );

}

textField.setText( tabbedPane.getTitleAt( tabbedPane.getSelectedIndex() ) );

}

catch( Exception exc )

{

}

}

ToddCorley at 2007-7-6 18:40:06 > top of Java-index,Archived Forums,Socket Programming...