Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What changed in JTextPane getMinimumWidth between Java 6 and Java 7

I just upgraded an application from Java 6u13 to Java 7u25 and I'm finding that the width returned by getMinimumWidth for JTextPanes is very different, which has caused a big problem in the user interface. I'd like to understand more about the change. Can anyone point me to a specific bug ID that would account for this. The output from my test program below is:

Java version: 1.6.0_13 Minimum size: java.awt.Dimension[width=6,height=25]

Java version: 1.7.0_25 Minimum size: java.awt.Dimension[width=33,height=25]

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

public class JTextPaneTest extends JTextPane
{
    private static void createAndShowGUI() 
    {
        System.out.println("Java version: " + System.getProperty("java.version"));

        // Create and set up the window.
        JFrame frame = new JFrame("ScaledJTextPane using BufferedImage");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JTextPaneTest textPane = new JTextPaneTest();
        StyledDocument doc = textPane.getStyledDocument();
        Style defaultStyle = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);

        textPane.setFont(new Font("Dialog", Font.PLAIN, 14));
        String plainText = "This is test.";
        try 
        {
            doc.insertString(doc.getLength(), plainText, defaultStyle);
        } 
        catch (BadLocationException ble) 
        {
            System.err.println("Couldn't insert text into text pane.");
        }

        // Force size calculation
        textPane.getPreferredSize();

        System.out.println("Minimum size: " + textPane.getMinimumSize().toString());

        final JPanel panel = new JPanel();
        panel.add(textPane, BorderLayout.CENTER);

        // Add content to the window.
        panel.setOpaque(true);
        panel.setBackground(Color.WHITE);
        frame.getContentPane().add(panel, BorderLayout.CENTER);
        frame.setSize(400, 300);

        //Display the window.
        frame.setVisible(true);
    }

    public static void main(String[] args) 
    {
        SwingUtilities.invokeLater(new Runnable() 
        {
            public void run() 
            {
                createAndShowGUI();
            }
        });
    }
}
like image 495
jht Avatar asked Feb 18 '26 05:02

jht


1 Answers

From the Oracle forums.. seems to be related. Link

like image 154
Yannick Avatar answered Feb 19 '26 20:02

Yannick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!