JDK-4264798 : JComponents with null borders receive borders after serialization
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.2.2
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_nt
  • CPU: x86
  • Submitted: 1999-08-23
  • Updated: 2000-02-24
  • Resolved: 2000-02-24
Related Reports
Relates :  
Description

Name: skT88420			Date: 08/23/99


This is a follow-up up of Bug #ID 4107584, which wasn't fully
addressed. JComponents that have null borders, after being
serialized, receive borders.

Below is an example which demonstrates this behavior.
Note that the leftmost JTextField and JButton should *not*
have borders, since I had set them to null before serializing
them. Also note that this is a problem with other
JComponents -- including the composite JComboBox.


//Source Code Below


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

public class A extends JFrame
{   
   public A()
   {
      super();
      
      final JPanel p = new JPanel(new FlowLayout());
            
      final JTextField tf = new JTextField("foo");
      tf.setBorder(null);
      
      p.add(tf);
            
      p.add(copyComponent(tf));    
      
      final JButton btn = new JButton("foo");
      btn.setBorder(null);
      
      p.add(btn);
      
      p.add(copyComponent(btn));    
                           
      setContentPane(p);            
   }
   
   //Makes a copy of the source component using serialization
   //As a side-effect, due to a bug, the source component border gets its border clobbered
   private JComponent copyComponent(final JComponent source)
   {
      JComponent copy = null;
      
      try
      {
         System.out.println("source's border prior to copying = " + source.getBorder());
         
         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
         final ObjectOutput os = new ObjectOutputStream(baos);
         
         os.writeObject(source);
         os.flush();
         os.close();
         baos.close();
         
         final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
         final ObjectInput is = new ObjectInputStream(bais);
         
         copy = (JComponent)is.readObject();

         System.out.println("source's border after copying = " + source.getBorder());         
         System.out.println();
      }
      catch(Exception ex)
      {
         ex.printStackTrace();
      }
      
      return copy;
   }   
      
   public static void main(String args[] )
   {
      A a = new A();
      
      a.pack();
      a.setVisible(true);
   }
}
(Review ID: 94255) 
======================================================================

Comments
EVALUATION Name: apC97674 Date: 02/18/2000 This problem really not with serialization mechanism as pointed in "Comments" field. When we serialize a JButton, the method LookAndFeel.installBorder(...) called automatically via JButton.writeObject(...) -> BasicButtonUI.installUI(...) -> -> MetalButtonUI.installDefaults(...) -> LookAndFeel.installBorder(...) public static void installBorder(JComponent c, String defaultBorderName) { Border b = c.getBorder(); if (b == null || b instanceof UIResource) { ^^^^^^^^^ c.setBorder(UIManager.getBorder(defaultBorderName)); } c.setBorder(null); } Hence, if the border sets to null then every BasicButtonUI.installUI(...) or JButton.updateUI() invocation will update the button border as default. This mechanism also distributed for other components (JTextFields and other). ###@###.### ====================================================================== Name: apC97674 Date: 02/23/2000 The couse of this problem is the same as problem discribed in the bug 4192869. ###@###.### ======================================================================
11-06-2004