JDK-4391320 : Solaris Merlin: icons in JToolBars are clipped
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.4.0
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: solaris_7
  • CPU: sparc
  • Submitted: 2000-11-21
  • Updated: 2001-01-03
  • Resolved: 2001-01-03
Related Reports
Relates :  
Description
ingrid.yao@Eng 2000-11-21

J2SE Version (please include all output from java -version flag):
------------------------------------------------------------------
java version "1.4.0beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0beta-b40)
Java HotSpot(TM) Client VM (build 1.4beta-B40, mixed mode)


Does this problem occur on J2SE 1.3?  Yes / No (pick one)
----------------------------------------------------------
No


Operating System Configuration Information (be specific):
-------------------------------------------------------
Solaris 2.7 & Win2000



Hardware Configuration Information (be specific):
------------------------------------------------
SunOS redgum 5.7 Generic_106541-12 sun4u sparc SUNW,Ultra-60


Bug Description:
---------------
Take a look at the attached screenshot(snapshot01.gif). 
The top window shows on customer's application running within 
JDK 1.4 and the bottom one is 1.3.  Compare the icons shown 
in the JToolBars, and you'll see that the 1.4 gui icons are 
clipped on left and right, by about a pixel.

Also attached is one of the icons(open.gif), they are all 16x16 pixels.
The icons are typically added to the ToolBar via an anonymous
AbstractAction subclass instantiation.

-------------------------------------------
More information from customer:

The problem is happening within the method named setButtonTexts() which is more-or-less cut out of their ArcExplorer apps code.  Here is what happens:

  1) using JDK 1.3.0 on either RedHat 7 Linux or Solaris 2.7, the icon looks fine
  2) using JDK 1.4.0 B44 on RedHat Linux7, no icon shows at all
  3) using JDK 1.4.0 B44 on Solaris 2.7, the icon is clipped on the left.

But I only can reproduce the problem on my winNT machine, it works fine
with my Solaris 7 machine with all recommended/required patches installed.

Test case:
=============

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

public class Application1 {
    boolean packFrame = false;

    /**Construct the application*/
    public Application1() {
        Frame1 frame = new Frame1();
        //Validate frames that have preset sizes
        //Pack frames that have useful preferred size info, e.g. from their layout
        if (packFrame) {
            frame.pack();
        }
        else {
            frame.validate();
        }
        //Center the window
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = frame.getSize();
        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }
        frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
        frame.setVisible(true);
    }
    /**Main method*/
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        new Application1();
    }
}

class Frame1 extends JFrame {
    JPanel contentPane;
    BorderLayout borderLayout1 = new BorderLayout();
    JToolBar jToolBar1 = new JToolBar();
    Action action;

    /**Construct the frame*/
    public Frame1() {
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
        try {
            jbInit();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
    /**Component initialization*/
    private void jbInit() throws Exception  {
        //setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
        contentPane = (JPanel) this.getContentPane();
        contentPane.setLayout(borderLayout1);
        this.setSize(new Dimension(400, 300));
        this.setTitle("Frame Title");
        setVisible(true);
        contentPane.add(jToolBar1, BorderLayout.NORTH);
        java.net.URL  url = getClass().getResource("zi.gif");
        ImageIcon icon = new ImageIcon(url);
        action = new AbstractAction("Zoom",icon) {
            public void actionPerformed(ActionEvent event) {
                int red = (int)(Math.random() * 256.0);
                int green = (int)(Math.random() * 256.0);
                int blue = (int)(Math.random() * 256.0);
                Frame1.this.getContentPane().setBackground(new Color(red,green,blue));
            }
        };
        JButton button = jToolBar1.add(action);
        setButtonTexts(button,"press me for pretty colours", 24, 24);
    }
    /**Overridden so we can exit when window is closed*/
    protected void processWindowEvent(WindowEvent e) {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
            System.exit(0);
        }
    }

    /**
    *  This method sets the buttons sizes and texts and also adjusts the
    *  alignment
    */
    private void setButtonTexts(javax.swing.JButton b, String tip, int width, int height) {
        b.setBorderPainted(true);
        b.setAlignmentX(0.5f);
        b.setAlignmentY(0.5f);
        b.setActionCommand(b.getText());
        b.setText("");
        b.setToolTipText(tip);
        b.setPreferredSize(new java.awt.Dimension(width,height));
        b.setMinimumSize(new java.awt.Dimension(width,height));
        b.setMaximumSize(new java.awt.Dimension(width,height));
    }
}


Comments
WORK AROUND When shrinking a button, the margin needs to be reduced. I suggest adding the following to the user's code (in method setButtonTexts): b.setMargin(new Insets(0, 0, 0, 0)); leif.samuelsson@Eng 2001-01-02
02-01-2001

EVALUATION The JButton class has had margins of size (2, 14, 2, 14) since JDK 1.2, but there was a bug in JToolBar which caused this margin to be ignored. See bug 4264764 for more info. When shrinking a button, the margin needs to be reduced. I suggest adding the following to the user's code (in method setButtonTexts): b.setMargin(new Insets(0, 0, 0, 0)); Not a bug. leif.samuelsson@Eng 2001-01-02
02-01-2001