Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4546123

CardLayout becomes unusable after deleting an element

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P3
    • Resolution: Fixed
    • Affects Version/s: 1.4.0
    • Fix Version/s: 1.4.0_03
    • Component/s: client-libs
    • Labels:
    • Subcomponent:
    • Resolved In Build:
      03
    • CPU:
      x86
    • OS:
      windows_98, windows_2000

      Backports

        Description



        Name: gm110360 Date: 12/03/2001


        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
        ==========================================================================

          Attachments

            Issue Links

              Activity

                People

                Assignee:
                son Oleg Sukhodolsky (Inactive)
                Reporter:
                gmanwanisunw Girish Manwani (Inactive)
                Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                  Dates

                  Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: