6741526: KeyboardFocusManager.setDefaultFocusTraversalPolicy(FocusTraversalPolicy) affects created components
authordav
Fri, 04 Feb 2011 17:32:33 +0300
changeset 8136 d81295a8ece1
parent 8135 5143a1ab97f4
child 8137 efd894717b2c
child 8367 6b8967bcbe45
6741526: KeyboardFocusManager.setDefaultFocusTraversalPolicy(FocusTraversalPolicy) affects created components Reviewed-by: ant, dcherepanov
jdk/src/share/classes/java/awt/Window.java
jdk/test/java/awt/KeyboardFocusmanager/DefaultPolicyChange/DefaultPolicyChange_AWT.java
jdk/test/java/awt/KeyboardFocusmanager/DefaultPolicyChange/DefaultPolicyChange_Swing.java
--- a/jdk/src/share/classes/java/awt/Window.java	Tue Feb 01 17:44:47 2011 +0000
+++ b/jdk/src/share/classes/java/awt/Window.java	Fri Feb 04 17:32:33 2011 +0300
@@ -504,6 +504,8 @@
         }
 
         modalExclusionType = Dialog.ModalExclusionType.NO_EXCLUDE;
+
+        SunToolkit.checkAndSetPolicy(this, false);
     }
 
     /**
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/awt/KeyboardFocusmanager/DefaultPolicyChange/DefaultPolicyChange_AWT.java	Fri Feb 04 17:32:33 2011 +0300
@@ -0,0 +1,44 @@
+/*
+  @test
+  @bug 6741526
+  @summary KeyboardFocusManager.setDefaultFocusTraversalPolicy(FocusTraversalPolicy) affects created components
+  @library ../../regtesthelpers
+  @build Sysout
+  @author Andrei Dmitriev : area=awt-focus
+  @run main DefaultPolicyChange_AWT
+*/
+
+import java.awt.*;
+import test.java.awt.regtesthelpers.Sysout;
+
+public class DefaultPolicyChange_AWT {
+    public static void main(String []s) {
+        DefaultPolicyChange_AWT.runTestAWT();
+    }
+
+    private static void runTestAWT(){
+        KeyboardFocusManager currentKFM = KeyboardFocusManager.getCurrentKeyboardFocusManager();
+        FocusTraversalPolicy defaultFTP = currentKFM.getDefaultFocusTraversalPolicy();
+        ContainerOrderFocusTraversalPolicy newFTP = new ContainerOrderFocusTraversalPolicy();
+
+        Frame frame = new Frame();
+        Window window = new Window(frame);
+
+        FocusTraversalPolicy resultFTP = window.getFocusTraversalPolicy();
+        Sysout.println("FocusTraversalPolicy on window = " + resultFTP);
+        /**
+         * Note: this call doesn't affect already created components as they have
+         * their policy initialized. Only new components will use this policy as
+         * their default policy.
+         **/
+        Sysout.println("Now will set another policy.");
+        currentKFM.setDefaultFocusTraversalPolicy(newFTP);
+        resultFTP = window.getFocusTraversalPolicy();
+        if (!resultFTP.equals(defaultFTP)) {
+            Sysout.println("Failure! FocusTraversalPolicy should not change");
+            Sysout.println("Was: " + defaultFTP);
+            Sysout.println("Become: " + resultFTP);
+            throw new RuntimeException("Failure! FocusTraversalPolicy should not change");
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/awt/KeyboardFocusmanager/DefaultPolicyChange/DefaultPolicyChange_Swing.java	Fri Feb 04 17:32:33 2011 +0300
@@ -0,0 +1,69 @@
+/*
+  @test
+  @bug 6741526
+  @summary KeyboardFocusManager.setDefaultFocusTraversalPolicy(FocusTraversalPolicy) affects created components
+  @library ../../regtesthelpers
+  @build Sysout
+  @author Andrei Dmitriev : area=awt-focus
+  @run main DefaultPolicyChange_Swing
+*/
+
+import java.awt.*;
+import javax.swing.*;
+import javax.swing.table.DefaultTableModel;
+import test.java.awt.regtesthelpers.Sysout;
+
+public class DefaultPolicyChange_Swing {
+    public static void main(String []s) {
+        EventQueue.invokeLater(new Runnable(){
+            public void run (){
+                DefaultPolicyChange_Swing.runTestSwing();
+            }
+        });
+    }
+    private static void runTestSwing(){
+        KeyboardFocusManager currentKFM = KeyboardFocusManager.getCurrentKeyboardFocusManager();
+        FocusTraversalPolicy defaultFTP = currentKFM.getDefaultFocusTraversalPolicy();
+        ContainerOrderFocusTraversalPolicy newFTP = new ContainerOrderFocusTraversalPolicy();
+
+
+        JFrame jf = new JFrame("Test1");
+        JWindow jw = new JWindow(jf);
+        JDialog jd = new JDialog(jf);
+        JPanel jp1 = new JPanel();
+        JButton jb1 = new JButton("jb1");
+        JTable jt1 = new JTable(new DefaultTableModel());
+
+        jf.add(jb1);
+        jf.add(jt1);
+        jf.add(jp1);
+        System.out.println("FTP current on jf= " + jf.getFocusTraversalPolicy());
+        System.out.println("FTP current on jw= " + jw.getFocusTraversalPolicy());
+        System.out.println("FTP current on jd= " + jd.getFocusTraversalPolicy());
+
+        if (!(jf.getFocusTraversalPolicy() instanceof LayoutFocusTraversalPolicy) ||
+            !(jw.getFocusTraversalPolicy() instanceof LayoutFocusTraversalPolicy) ||
+            !(jd.getFocusTraversalPolicy() instanceof LayoutFocusTraversalPolicy))
+        {
+            throw new RuntimeException("Failure! Swing toplevel must have LayoutFocusTraversalPolicy installed");
+        }
+
+        jf.setVisible(true);
+
+        System.out.println("Now will set another policy.");
+        currentKFM.setDefaultFocusTraversalPolicy(newFTP);
+
+        FocusTraversalPolicy resultFTP = jw.getFocusTraversalPolicy();
+
+        System.out.println("FTP current on jf= " + jf.getFocusTraversalPolicy());
+        System.out.println("FTP current on jw= " + jw.getFocusTraversalPolicy());
+        System.out.println("FTP current on jd= " + jd.getFocusTraversalPolicy());
+
+        if (!resultFTP.equals(defaultFTP)) {
+            Sysout.println("Failure! FocusTraversalPolicy should not change");
+            Sysout.println("Was: " + defaultFTP);
+            Sysout.println("Become: " + resultFTP);
+            throw new RuntimeException("Failure! FocusTraversalPolicy should not change");
+        }
+    }
+}