-
Bug
-
Resolution: Unresolved
-
P4
-
None
-
5.0
-
x86
-
windows_xp
FULL PRODUCT VERSION :
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode)
ALSO OCCURS ON
java version "1.6.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0-beta-b59g)
Java HotSpot(TM) Client VM (build 1.6.0-beta-b59g, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]
A DESCRIPTION OF THE PROBLEM :
Upon removing the last component from a "java.awt.CardLayout" container the
componentShown(ComponentEvent e) method in the ComponentListener set on the component is INCORRECTLY invoked!
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run supplied test case:
java de.gaskin.bugs.CompEventBug1
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The method componentShown(...) should NOT be invoked when the last card is removed.
ACTUAL -
The method componentShown(...) is invoked and the source of the event is incorrectly set.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
package de.gaskin.bugs;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
class CompEventBug1 extends ComponentAdapter implements ActionListener {
private final static String CARD1 = "CardOne";
private final static String CARD2 = "CardTwo";
private JPanel cardPane;
private CardLayout cl;
private JButton remove;
private JTextArea protocol;
CompEventBug1(JButton remove, JTextArea protocol) {
this.remove = remove;
this.protocol = protocol;
CardLayout cl = new CardLayout();
cardPane = new JPanel(cl);
// cardPane.addComponentListener(this);
add(CARD1);
add(CARD2);
}
private void add(String name) {
JLabel label = new JLabel(name);
label.setName(name);
cardPane.add(label, name);
label.addComponentListener(this);
}
public void actionPerformed(ActionEvent ae) {
cardPane.remove(0);
reportState();
}
public void componentShown(ComponentEvent e) {
Component source = (Component)e.getSource();
if (source.isShowing()) { // This is also the workaround
protocol.append("'componentShown' invoked and is correct "
+ source.getName() + " is now showing\n");
}
else { // THIS IS THE BUG
remove.setEnabled(false);
// System.out.println(source.getName() + " " + source.isShowing());
protocol.append("============== AND HERE IS THE BUG ==========\n");
protocol.append("'componentShown' invoked BUT SHOULD NOT HAVE BEEN\n");
protocol.append(" e.getSource() is " + source.getName() + "\n");
protocol.append(" e.isShowing() is " + source.isShowing()+ "\n");
}
}
private void reportState() {
int nbrCards = cardPane.getComponents().length;
protocol.append("CardPane has " + nbrCards + " cards\n");
if (nbrCards > 0) {
protocol.append("\nPlease Click 'remove'\n");
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("ComponentEvent Bug");
JButton remove = new JButton("Remove a Card");
JTextArea protocol = new JTextArea(10, 80);
CompEventBug1 ceb1 = new CompEventBug1(remove, protocol);
Container contentPane = frame.getContentPane();
remove.addActionListener(ceb1);
contentPane.add(ceb1.cardPane, BorderLayout.NORTH);
contentPane.add(remove, BorderLayout.CENTER);
contentPane.add(new JScrollPane(protocol), BorderLayout.SOUTH);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
ceb1.reportState();
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
public void componentShown(ComponentEvent e) {
Component source = (Component)e.getSource();
if (source.isShowing()) { // This is the workaround
.....
}
}
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode)
ALSO OCCURS ON
java version "1.6.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0-beta-b59g)
Java HotSpot(TM) Client VM (build 1.6.0-beta-b59g, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]
A DESCRIPTION OF THE PROBLEM :
Upon removing the last component from a "java.awt.CardLayout" container the
componentShown(ComponentEvent e) method in the ComponentListener set on the component is INCORRECTLY invoked!
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run supplied test case:
java de.gaskin.bugs.CompEventBug1
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The method componentShown(...) should NOT be invoked when the last card is removed.
ACTUAL -
The method componentShown(...) is invoked and the source of the event is incorrectly set.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
package de.gaskin.bugs;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
class CompEventBug1 extends ComponentAdapter implements ActionListener {
private final static String CARD1 = "CardOne";
private final static String CARD2 = "CardTwo";
private JPanel cardPane;
private CardLayout cl;
private JButton remove;
private JTextArea protocol;
CompEventBug1(JButton remove, JTextArea protocol) {
this.remove = remove;
this.protocol = protocol;
CardLayout cl = new CardLayout();
cardPane = new JPanel(cl);
// cardPane.addComponentListener(this);
add(CARD1);
add(CARD2);
}
private void add(String name) {
JLabel label = new JLabel(name);
label.setName(name);
cardPane.add(label, name);
label.addComponentListener(this);
}
public void actionPerformed(ActionEvent ae) {
cardPane.remove(0);
reportState();
}
public void componentShown(ComponentEvent e) {
Component source = (Component)e.getSource();
if (source.isShowing()) { // This is also the workaround
protocol.append("'componentShown' invoked and is correct "
+ source.getName() + " is now showing\n");
}
else { // THIS IS THE BUG
remove.setEnabled(false);
// System.out.println(source.getName() + " " + source.isShowing());
protocol.append("============== AND HERE IS THE BUG ==========\n");
protocol.append("'componentShown' invoked BUT SHOULD NOT HAVE BEEN\n");
protocol.append(" e.getSource() is " + source.getName() + "\n");
protocol.append(" e.isShowing() is " + source.isShowing()+ "\n");
}
}
private void reportState() {
int nbrCards = cardPane.getComponents().length;
protocol.append("CardPane has " + nbrCards + " cards\n");
if (nbrCards > 0) {
protocol.append("\nPlease Click 'remove'\n");
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("ComponentEvent Bug");
JButton remove = new JButton("Remove a Card");
JTextArea protocol = new JTextArea(10, 80);
CompEventBug1 ceb1 = new CompEventBug1(remove, protocol);
Container contentPane = frame.getContentPane();
remove.addActionListener(ceb1);
contentPane.add(ceb1.cardPane, BorderLayout.NORTH);
contentPane.add(remove, BorderLayout.CENTER);
contentPane.add(new JScrollPane(protocol), BorderLayout.SOUTH);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
ceb1.reportState();
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
public void componentShown(ComponentEvent e) {
Component source = (Component)e.getSource();
if (source.isShowing()) { // This is the workaround
.....
}
}