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

ImageIcon does not display on JButton when using setPreferredSize

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.0
    • client-libs



      Name: bsC130419 Date: 07/13/2001


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

      Under JDK 1.4 beta, the ImageIcon assigned to a JButton does NOT display if you
      call setPreferredSize() on the JButton. Under JDK 1.3.0_02, the icon displayed
      fine, regardless whether you called setPreferredSize() or not.

      I've given the JDK 1.4 version information above.
      For JDK 1.3.0_02, the version info is:
      java version "1.3.0_02"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0_02)
      Java HotSpot(TM) Client VM (build 1.3.0_02, mixed mode)

      Here is the complete source code for a simple Java application that
      demonstrates the problem.

      // AppBug.java

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

      public class AppBug
      {
      JButton _buttonDeleteOne;
      JButton _buttonDeleteTwo;
      ImageIcon _iconDelete;
      JFrame _mainFrame;
      JPanel _mainPanel;

      public static void main(String args[])
      {
      try
      {
      AppBug myself = new AppBug();
      myself.doit(args);
      }
      catch(Exception exception)
      {
      System.out.println(exception.getMessage());
      exception.printStackTrace();
      }
      }

      public void doit(String args[])
      {
      _iconDelete = new ImageIcon("Delete16.gif");

      _mainFrame = new JFrame("Demonstrate JButton/ImageIcon Bug");
      _mainPanel = new JPanel();
      _mainFrame.setContentPane(_mainPanel);
      _mainPanel.setLayout(new FlowLayout());

      // Handle termination of the app
      WindowListener wl = new WindowAdapter()
      {
      public void windowClosing(WindowEvent ee) { System.exit
      (0); }
      };
      _mainFrame.addWindowListener(wl);

      // This button/icon does NOT display correctly
      // But, if you comment out the call to setPreferredSize, it
      WILL display correctly.
      _buttonDeleteOne = new JButton(_iconDelete);
      _buttonDeleteOne.setPreferredSize(new Dimension
      (_iconDelete.getIconWidth()+10,_iconDelete.getIconHeight()+10));
      _mainPanel.add(_buttonDeleteOne);

      // This button/icon displays correctly
      _buttonDeleteTwo = new JButton(_iconDelete);
      _mainPanel.add(_buttonDeleteTwo);

      _mainFrame.pack();
      _mainFrame.setVisible(true);
      }
      }
      (Review ID: 128049)
      ======================================================================

            svioletsunw Scott Violet (Inactive)
            bstrathesunw Bill Strathearn (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: