Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4546123
Votes 2
Synopsis CardLayout becomes unusable after deleting an element
Category java:classes_awt
Reported Against 1.4 , merlin-beta3
Release Fixed 1.4.0_03
State 10-Fix Delivered, bug
Priority: 3-Medium
Related Bugs 4689398 , 4690266
Submit Date 04-DEC-2001
Description




java version "1.4.0-beta3"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta3-b84)
Java HotSpot(TM) Client VM (build 1.4.0-beta3-b84, mixed mode)


The cardlayout to me is the way that I would create a layout like I have in MS
word whereby I might have lots of word documents open but only one is showing.
When I am done with one I should be able to close it and remove it from the
cardlayout.  However it seems to me that the cardlayout becomes very unstable
after I delete an element from it.  This is not right at all.  I have included
the code below.  I took it from a bug report that is similar, but has to do
with an un-related cardlayout issue.  I looked at the cardlayout source and I
am really lost as to why this code would cause such a bad result.

Run the program and select a few different oanels from the second menu.  Make
sure your last choce is the red panel.  Then click close.  At that point the
program removes the redpanel from the cardlayout, but no other panels are
selectable from the second drop down.

Please help or advise.

Thanks
Jim Tyrrell

Source code below:
import java.awt.*;
import java.awt.event.*;
import java.awt.AWTEvent;


public class TestFrame extends Frame implements ActionListener
{
        public Panel            aPanel;
        public TestPanel        pageRed;
        public TestPanel        pageGreen;
        public TestPanel        pageBlue;
        public String           currentSelection = "";

        public MenuItem mi;
        public CardLayout       theCardLayout;


        public TestFrame()
        {
                super( "Test Frame - from Novell, Inc." );

                setBackground( Color.black );
                setLayout( new BorderLayout(5,5) );

                enableEvents( AWTEvent.WINDOW_EVENT_MASK );

                MenuBar mb = new MenuBar();

                Menu fileMenu = new Menu( "File" );
                Menu pageMenu = new Menu( "Pages" );

                mi = new MenuItem( "Exit" );
                mi.addActionListener( this );
                fileMenu.add( mi );

                mi = new MenuItem("Close");
                mi.addActionListener( this );
                fileMenu.add( mi );

                mi = new MenuItem( "Red" );
                mi.addActionListener( this );
                pageMenu.add( mi );

                mi = new MenuItem( "Green" );
                mi.addActionListener( this );
                pageMenu.add( mi );

                mi = new MenuItem( "Blue" );
                mi.addActionListener( this );
                pageMenu.add( mi );

                mb.add( fileMenu );
                mb.add( pageMenu );

                setMenuBar( mb );

                aPanel = new Panel();
                theCardLayout = new CardLayout();

                aPanel.setLayout( theCardLayout );

                pageRed = new TestPanel( "PageRed", Color.red );
                pageGreen = new TestPanel( "PageGreen", Color.green );
                pageBlue = new TestPanel( "PageBlue", Color.blue );

                aPanel.add( "PageRed", pageRed );
                aPanel.add( "PageGreen", pageGreen );
                aPanel.add( "PageBlue", pageBlue );

                add( "Center", aPanel );
                setSize( getPreferredSize());
        }


        public Insets getInsets()
        {
                return new Insets( 47, 9, 9, 9 );
        }


        public void actionPerformed( ActionEvent e )
        {
                if( e.getActionCommand().equals( "Exit" ))
                {
                        dispose();
                        System.exit(0);
                }
                else if( e.getActionCommand().equals( "Red" ))
                {
                        theCardLayout.show( aPanel, "PageRed" );
                        currentSelection = "PageRed";
                }
                else if( e.getActionCommand().equals( "Green" ))
                {
                        theCardLayout.show( aPanel, "PageGreen" );
                }
                else if( e.getActionCommand().equals( "Blue" ))
                {
                        theCardLayout.show( aPanel, "PageBlue" );
                }else if (e.getActionCommand().equals( "Close" )){
                    System.out.println("Closeing");

                    if(currentSelection.equals("PageRed"))
                    {
                        System.out.println("Remove page red");
                        theCardLayout.removeLayoutComponent(pageRed);
                    }
                }

        }

        protected void processEvent( AWTEvent event )
        {
                if( event instanceof WindowEvent )
                {
                        if( event.getID() == WindowEvent.WINDOW_CLOSING )
                        {
                                dispose();
                                System.exit(0);
                        }
                }

                super.processEvent( event );
        }

        static public void main( String[] args )
        {
                TestFrame       theTestFrame = new TestFrame();
                theTestFrame.setVisible( true );
        }
}

import java.awt.*;
import java.awt.event.*;
import java.awt.AWTEvent;
import javax.swing.*;


class TestPanel extends JPanel
{
        private String                  pageName;

      TestPanel(String pageName, Color color)
      {
         setBackground(color);
         add( new JLabel(pageName));
      }
}
(Review ID: 135826) 
======================================================================
I have another sample program.

1. Reproducing

 1) Compile the attached Test.java
 2) Invoke "java test"
    -> you will see green window.
 3) Click "remove red and show blue" button.
    -> The color stay green.

  CORRECT behavior:
    The color changes to blue.


2. configration
  MPU : Pentium  IV 1.4 [GHz]
  Mem : 384 [MB]
  OS  : Windows 2000 (SP2, Japanese)

2002-04-25
==========================================================================
Work Around




None found.  Breaks in all L&F's
======================================================================
Evaluation
Commit to fix in Tiger (regression).  
 xxxxx@xxxxx  2002-05-28





This bug is part of 4362381 (Once the remove button had press, next item from the list 
should show.) which was not fixed.  To fix it we should call next() method before 
removing current card from CardLayou.

 xxxxx@xxxxx  2002-09-04

======================================================================
Comments
  
  Include a link with my name & email   

Submitted On 06-DEC-2001
Movedtoky
I found a work around for this.  It seems that the Java 
under the covers only allows you to remove an object from a 
JPanel with a reference to the actual element you want to 
remove.  Using anything else results in weird results.


Submitted On 03-JUL-2002
gzsombi
workaround: get the CardLayout.class from the old, good 
jdk1.3, and put it into the rt.jar ... :-)
it's an ugly hack, but it works ...


Submitted On 09-OCT-2003
erik.zimmermann
workaround: You have to call the next() method before and the previous() 
method after removing a card from the layout. If you only call next(), the 
CardLayout gets more confused.

cardLayout.next(panel);
panel.remove(component);
cardLayout.removeLayoutComponent(component);
cardLayout.previous(panel);



PLEASE NOTE: JDK6 is formerly known as Project Mustang