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

BasicButtonUI uses wrong FontMetrics to Layout JButtons text

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: 6
    • Fix Version/s: None
    • Component/s: client-libs

      Description

      FULL PRODUCT VERSION :
      java version "1.6.0_07"
      Java(TM) SE Runtime Environment (build 1.6.0_07-b06)
      Java HotSpot(TM) Client VM (build 10.0-b23, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      javax.swing.plaf.basic.BasicButtonUI uses a wrong FontMetrics object to layout the text on a JButton. The paint(Graphics, JComponent) method of BasicButtonUI uses the FontMetrics object of the passed Graphics object without setting the buttons font before. If the buttons font varies from the currenly set font of the passed graphics object, the font metrics do not fit the metrics of the expected font leading to missplaced text on the button.

      If you now try to paint the button on an intermediate image (BufferedImage) due to performance reasons or to manipulate its design, the layout of the button may become corrupt if the passed graphics objec will not have the buttons font set before painting the button. Actually the buttons paint method shoud care for this.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the atached source code an click on the two buttons in the frame. The Buttons will both be disabled by pressing them, whereas the first button will be painted blurred using an intermediate image to paint the button on, blurr it an paint the image to the frame. During this action the Text "Test" of the button will not only be blurred too, but also be cut of to "T..." indicating that it does not fit complete into the button. On the second button the text will not be cut of, thus this button will not be painted onto an intermediate image.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      See Steps to Reproduce.
      Both buttons should be disabled whereas the first button schould be painted blurred showing the same Text "Test" as before.
      ACTUAL -
      See Steps to Reproduce.
      Both Buttons appear disabled whereas the text of the first button is cut of.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      no error message. See description above.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package swinghacks.blurbutton;

      import java.awt.Graphics;
      import java.awt.Graphics2D;
      import java.awt.GridBagConstraints;
      import java.awt.GridBagLayout;
      import java.awt.Insets;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.awt.image.BufferedImage;
      import java.awt.image.ConvolveOp;
      import java.awt.image.Kernel;

      import javax.swing.JButton;
      import javax.swing.JComponent;
      import javax.swing.JFrame;
      import javax.swing.SwingUtilities;
      import javax.swing.UIManager;
      import javax.swing.WindowConstants;

      public class BlurButton extends JButton {

        public BlurButton(String text) {
          super(text);
        }

        @Override
        public void paintComponent(Graphics g) {
          if (isEnabled()) {
            super.paintComponent(g);
            return;
          }
          Graphics2D g2 = (Graphics2D) g.create();
          BufferedImage image = new BufferedImage(getWidth(), getHeight(),
              BufferedImage.TYPE_INT_RGB);
          Graphics imgGraphics = image.getGraphics();

          // uncomment this to see the workaround running
          // imgGraphics.setFont(getFont());
          super.paintComponent(imgGraphics);
          float[] kernel = { 0.1f, 0.1f, 0.1f, 0.1f, 0.2f, 0.1f, 0.1f, 0.1f, 0.1f };
          ConvolveOp op = new ConvolveOp(new Kernel(3, 3, kernel),
              ConvolveOp.EDGE_NO_OP, null);
          image = op.filter(image, null);
          g2.drawImage(image, 0, 0, null);
        }

        public static void main(String... args) throws Exception {
          Runnable run = new Runnable() {
            public void run() {
              try {
                UIManager
                    .setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                JFrame frame = new JFrame();
                frame.setLayout(new GridBagLayout());
                frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                BlurButton button1 = new BlurButton("Test");
                button1.addActionListener(new DisableListener());
                JButton button2 = new JButton("Test");
                button2.addActionListener(new DisableListener());
                frame.add(button1, new GridBagConstraints(0, 0, 1, 1, 0, 0,
                    GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(
                        10, 10, 0, 10), 0, 0));
                frame.add(button2, new GridBagConstraints(0, 1, 1, 1, 0, 0,
                    GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0,
                        10, 10, 10), 0, 0));
                frame.pack();
                frame.setVisible(true);
                System.out.println(button1.getUI());
              } catch (Exception e) {
                e.printStackTrace();
              }
            }
          };
          SwingUtilities.invokeAndWait(run);
        }

        static class DisableListener implements ActionListener {
          public void actionPerformed(ActionEvent e) {
            ((JComponent) e.getSource()).setEnabled(false);
          }
        }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Set the font of the passed Graphics object manually before painting the button. (see the marked (commented) line in the source code).

      The real solution to the problem would be to set the font of the buttons passed graphics object before achieving the current FontMetrics.

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Imported:
              Indexed: