8079640: GroupLayout incorrect layout with large JTextArea
authorssadetsky
Tue, 26 May 2015 08:33:32 +0300
changeset 30950 2e6d6c55d109
parent 30949 5e54ad419c64
child 30951 0383ab2b6d21
8079640: GroupLayout incorrect layout with large JTextArea Reviewed-by: serb, alexsch, azvegint
jdk/src/java.desktop/share/classes/javax/swing/GroupLayout.java
jdk/test/javax/swing/GroupLayout/8079640/bug8079640.java
--- a/jdk/src/java.desktop/share/classes/javax/swing/GroupLayout.java	Mon May 25 16:10:12 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/javax/swing/GroupLayout.java	Tue May 26 08:33:32 2015 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 2015, 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
@@ -218,6 +218,9 @@
 
     private static final int UNSET = Integer.MIN_VALUE;
 
+    // Maximum spring size constrain to avoid integer overflow
+    private static final int INFINITE = Integer.MAX_VALUE >> 1;
+
     /**
      * Indicates the size from the component or gap should be used for a
      * particular range value.
@@ -1389,7 +1392,7 @@
         }
 
         int constrain(int value) {
-            return Math.min(value, Short.MAX_VALUE);
+            return Math.min(value, INFINITE);
         }
 
         int getBaseline() {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/GroupLayout/8079640/bug8079640.java	Tue May 26 08:33:32 2015 +0300
@@ -0,0 +1,99 @@
+/*
+ * Copyright (c) 2015, 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.
+ */
+
+/* @test
+   @bug 8079640
+   @summary GroupLayout incorrect layout with large JTextArea
+   @author Semyon Sadetsky
+  */
+
+
+import javax.swing.*;
+import java.awt.*;
+
+public class bug8079640 {
+
+    private static JFrame frame;
+    private static JComponent comp2;
+
+    public static void main(String[] args) throws Exception {
+
+        try {
+            SwingUtilities.invokeAndWait(new Runnable() {
+                @Override
+                public void run() {
+                    frame = new JFrame("A Frame");
+                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+                    frame.setUndecorated(true);
+                    setup(frame);
+                    frame.setVisible(true);
+                }
+            });
+
+            test();
+            System.out.println("ok");
+
+        } finally {
+            SwingUtilities.invokeLater(new Runnable() {
+                @Override
+                public void run() {
+                    frame.dispose();
+                }
+            });
+        }
+    }
+
+    private static void test() throws Exception {
+        SwingUtilities.invokeAndWait(new Runnable() {
+            @Override
+            public void run() {
+                if(comp2.getLocation().getY() > frame.getHeight())
+                    throw new RuntimeException("GroupLayout fails: comp2 is out of the window");
+            }
+        });
+    }
+
+
+    static void setup(JFrame frame)  {
+        JPanel panel = new JPanel();
+        JComponent comp1 = new JLabel("Test Label 1");
+        comp1.setMinimumSize(new Dimension(1000, 40000));
+        comp1.setPreferredSize(new Dimension(1000, 40000));
+        JScrollPane scroll = new JScrollPane(comp1);
+        comp2 = new JLabel("Test Label 2");
+        GroupLayout layout = new GroupLayout(panel);
+        layout.setHorizontalGroup(
+                layout.createParallelGroup(GroupLayout.Alignment.LEADING)
+                        .addComponent(scroll)
+                        .addComponent(comp2));
+        layout.setVerticalGroup(
+                layout.createSequentialGroup()
+                        .addComponent(scroll)
+                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
+                        .addComponent(comp2));
+        panel.setLayout(layout);
+        frame.getContentPane().add(panel, BorderLayout.CENTER);
+        frame.setSize(800, 600);
+    }
+
+}