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

Serialization corrupts focus of a JButton

XMLWordPrintable



      Name: stC104175 Date: 05/25/2000


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


      Serialization appears to corrupt the focus behavior of a JButton.
      This is specific to JDK 1.3. The problem does not occur on JDK 1.2.2

      At the bottom is an example program which demonstrates this problem.

      Compile and run the example program under JDK 1.3.

      Press the "Press to serialize this container" button. Another Frame should come,
      titled, "Contains the serialized panel".

      The fact that the serialized "Press to serialize this container" button is
      displaying the focus selection outline is a little peculiar to me, yet I have
      not fully investigated why this is. What seems strikingly wrong is the fact that
      the serialized "Press to serialize this container" button cannot receive the
      keyboard input focus. You can test this by pressing the tab key.

      Now try running the program under JDK 1.2.2. While the serialized "Press to
      serialize this container" button still displays the selection outline, it is
      however capable of recieving the keyboard focus, as you would expect.



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


      public class Test extends JFrame
      {
         public Test()
         {
            final JPanel p = new JPanel()
            {
               private void readObject(final ObjectInputStream s) throws IOException
               {
                  try
                  {
                     s.defaultReadObject();

                     //Clobbering model so button won't appear pressed
                     ((AbstractButton)getComponent(2)).setModel(new
      DefaultButtonModel());
                  }
                  catch(ClassNotFoundException ex)
                  {
                     ex.printStackTrace();
                  }
               }
            };


            setContentPane(p);

            p.add(new JButton("A"));

            p.add(new JButton("B"));

            JButton btn = new JButton("Press to serialize this container");

            btn.addMouseListener(new MouseAdapter()
            {
               public void mousePressed(MouseEvent ev)
               {
                  JFrame f = new JFrame("Contains the serialized panel");

                  try
                  {
                     f.setContentPane((JPanel)recreateObjectFromObject(p));
                  }
                  catch(Exception ex)
                  {
                     ex.printStackTrace();
                  }

                  f.setLocation(300, 300);
                  f.pack();
                  f.setVisible(true);
               }
            });

            p.add(btn);
         }


         static public Serializable recreateObjectFromObject(final Serializable
      object)
                throws IOException, ClassNotFoundException
         {
            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
            final ObjectOutputStream oos = new ObjectOutputStream(baos);

            oos.writeObject(object);
            oos.flush();

            final byte[] bytes = baos.toByteArray();

            oos.close();
            baos.close();

            final ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            final ObjectInput is = new ObjectInputStream(bais);

            final Serializable retObj = (Serializable)is.readObject();

            is.close();
            bais.close();

            return retObj;
         }


         public static void main(String[] args)
         {
            Test t = new Test();
            t.pack();
            t.setVisible(true);
         }
      }
      (Review ID: 105324)
      ======================================================================

            apikalev Andrey Pikalev
            stompkinsunw Sean Tompkins (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: