Name: skT88420 Date: 07/13/99
1. Steps:
Run this program (code below) and close any tab except
the tab which is currently the last tab. There will be NO
changeEvent fired. In my sample code, changeEvents cause a
BANG followed by a number to be printed on System.out so you can
easily see which events cause changeEvents and which don't.
The reason why this happens probably is that the concept of
a state change must be INCORRECTLY based on which tab
INDEX is selected, rather than which ACTUAL TAB is selected.
So there is no changeEvent when you remove a tab which is not
the last tab since it does NOT cause a selected index change!!!
2. Source code:
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JTabbedPaneBug extends JFrame {
public JTabbedPaneBug() {
super("If you remove ANY TAB except the last tab there is NO state change!");
setSize(800, 300);
Container mainContentPane = getContentPane();
mainContentPane.setLayout(new BorderLayout(0,0));
mainContentPane.add("Center", pain);
JButton one = new JButton(REMOVE);
JButton two = new JButton(REMOVE);
JButton three = new JButton(REMOVE);
JButton four = new JButton(REMOVE);
JButton five = new JButton(REMOVE);
JButton six = new JButton(REMOVE);
JButton seven = new JButton(REMOVE);
one.addActionListener(new Foo());
two.addActionListener(new Foo());
three.addActionListener(new Foo());
four.addActionListener(new Foo());
five.addActionListener(new Foo());
six.addActionListener(new Foo());
seven.addActionListener(new Foo());
pain.addTab("one", one);
pain.addTab("two", two);
pain.addTab("three", three);
pain.addTab("four", four);
pain.addTab("five", five);
pain.addTab("six", six);
pain.addTab("seven", seven);
}
private static String REMOVE = "Click anywhere to remove the panel!";
PaneAndSuffering pain = new PaneAndSuffering();
class PaneAndSuffering extends JTabbedPane {
protected void fireStateChanged() {
super.fireStateChanged();
System.out.println("BANG " + bangCount++);
}
private int bangCount = 0;
}
class Foo implements ActionListener {
public void actionPerformed(ActionEvent e) {
pain.removeTabAt(pain.getSelectedIndex());
//if you can click the button it's the selected index
}
}
public static void main(String[] args) {
JTabbedPaneBug jtb = new JTabbedPaneBug();
jtb.setVisible(true);
}
}
(Review ID: 85497)
======================================================================