JDK-7170655 : Frame size does not follow font size change with XToolkit
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 7
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: linux
  • CPU: x86
  • Submitted: 2012-05-22
  • Updated: 2013-07-10
  • Resolved: 2012-06-13
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7 JDK 8
7u40Fixed 8 b43Fixed
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :


ADDITIONAL OS VERSION INFORMATION :
Ubuntu 12.04

A DESCRIPTION OF THE PROBLEM :
Frame size does not follow font size change with XToolkit.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the test case,
1), input 30 in 'font size' field and press ENTER
2), input 20 in 'font size' field and press ENTER
3), Press ENTER

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
After each ENTER key press, the font and window size change accordingly.
ACTUAL -
Only font size got changed, not windows size.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
/*
 * Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * Portions Copyright (c) 2012 IBM Corporation
 */

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

public class LbCharViewer {

    Font currentFont;

    final String str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

    final int ROWS = 5;

    final int COLS = 20;

    Label[] labels;

    public LbCharViewer() {
        final Frame frame = new Frame("Char viewer");
        currentFont = new Font("Dialog", Font.PLAIN, 12);
        frame.setLayout(new BorderLayout());
        frame.setBackground(Color.lightGray);

        Panel charPanel = new Panel();
        labels = new Label[ROWS * COLS];
        charPanel.setLayout(new GridLayout(0, COLS, 3, 3));
        for (int i = 0; i < ROWS * COLS; ++i) {
            Label label = new Label("");
            label.setBackground(Color.white);
            label.setForeground(Color.black);
            charPanel.add(label);
            labels[i] = label;
        }
        for (int i = 0; i < str.length(); i++) {
            labels[i].setText(str.substring(i, i + 1));
        }

        Panel fontPanel = new Panel();
        TextField txtFontsize = new TextField(3);
        txtFontsize.setText("" + currentFont.getSize());
        txtFontsize.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent event) {
                String command = event.getActionCommand();
                int size = Integer.parseInt(command);
                Font font = currentFont.deriveFont((float) size);
                for (int i = 0; i < ROWS * COLS; ++i) {
                    labels[i].setFont(font);
                }
                currentFont = font;
                frame.validate();
                frame.pack();
            }
        });
        fontPanel.add(new Label("Font Size:"));
        fontPanel.add(txtFontsize);

        frame.add(charPanel, "Center");
        frame.add(fontPanel, "South");
        frame.pack();
        frame.setVisible(true);

        frame.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    public static void main(String[] args) throws Exception {
        SwingUtilities.invokeAndWait(new Runnable() {

            @Override
            public void run() {
                LbCharViewer table = new LbCharViewer();
            }
        });
    }
}

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

Comments
SUGGESTED FIX http://hg.openjdk.java.net/jdk8/awt/jdk/rev/3c9adc88956d
13-06-2012

EVALUATION The main problem is that XLabelPeer updates its caches (cachedFontMetrics and oldfont) in the paintPeer(). So when we postpone the paint, frame.pack()->...->getMinimumSize() uses old caches, and when we paint in a place, getMinimumSize() uses correct values.
13-06-2012