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

Serialization doesn't work with menus

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.1.3
    • client-libs
    • None
    • x86
    • windows_nt



      Name: rlT66838 Date: 08/15/97


      The attached application runs fine the first time.
      The second time, when loaded from the previously
      The attached application does not correctly restore
      its state when serialized to disk and then
      run a second time. To see the problem, remove
      the file "foo.state" if you have one. Execute
      the application with "java foo." Select "Exit"
      from the main menu. Restart the program with
      "java foo," then try to exit again.

      The program exhibits two problems when run
      the second time (when state is restored from
      "foo.state"):

      1) The client area of the main frame is not erased
         correctly: There's garbage at the bottom. The
         area is redrawn properly when you move or
         resize the frame.

      2) The menu displays correctly, but selecting the
         "Exit" item hangs the application. This menu
         item works properly the first time, when the
         application doesn't load its state from the
         previously generated foo.state file.

      ---------------------- cut here ------------------------------
      import java.awt.*;
      import java.awt.event.*;
      import java.io.*;

      class foo extends Frame
      {
      private static foo instance = null;
      private static String state_file = "foo.state";

      class Quit_handler implements ActionListener
      { public void actionPerformed(ActionEvent event)
      { save_state_and_exit();
      }
      }

      private static void add_item( Menu menu, String legend, ActionListener handler )
      {
      MenuItem line_item = new MenuItem( legend );
      line_item.addActionListener( handler );
      menu.add( line_item );
      }

      private foo()
      {
      super("Appointment/Meeting Manger");

      MenuBar bar = new MenuBar();
      Menu main_menu = new Menu("foo");

      add_item( main_menu, "Exit", new Quit_handler() );

      bar.add ( main_menu );
      setMenuBar ( bar );
      setSize ( 300, 300 );
      setVisible ( true );
      }

      private void save_state_and_exit()
      {
      // Shut down the program, saving the current state to a file.

      try
      {
      FileOutputStream file_out = new FileOutputStream(state_file);
      ObjectOutputStream object_out = new ObjectOutputStream(file_out);
      object_out.writeObject(this);
      object_out.flush();
      object_out.close();
      System.exit( 0 );
      }
      catch( Exception e ) { System.out.println( e ); }
      }

      public static final foo get_instance()
      {
      if( instance == null )
      {
      while( true )
      {
      File the_file = new File( state_file ); // does it exist?
      try
      {
      if( !the_file.exists() )
      instance = new foo();
      else
      {
      System.out.println("Loading previously-saved state from " + state_file );

      FileInputStream input = new FileInputStream(state_file);
      ObjectInputStream objects = new ObjectInputStream(input);
      instance = (foo) objects.readObject();
      objects.close();

      instance.setVisible( true );
      }
      break;
      }
      catch( FileNotFoundException e )
      {
      throw new Error("Can't find state file: " + state_file);
      }
      catch( IOException e)
      {
      System.out.println( "Can't process " + state_file
      + " (" + e.getMessage() + ")" );

      System.out.println( "Deleting old state file" );
      if( !the_file.delete() )
      throw new Error("Couln't delete old state file: " +
      the_file.getPath() );
      }
      catch( ClassNotFoundException e)
      {
      System.out.println( "Class not found in state file: " + e );
      throw new Error("Can't load class");
      }
      }
      }
           return instance;
      }

      public static void main(String[] args)
      {
      try { foo c = get_instance(); }
      catch( Throwable e ) { e.printStackTrace(); }
      }
      }


      ======================================================================

            duke J. Duke
            rlewis Roger Lewis (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: