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

Two Level Focus: Provide a way to set internal focus to control

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 8u40
    • javafx
    • Windows 7

      My application makes use of the Two Level Focus behavior as it is controlled by remote (no keys for navigating between controls). However, sometimes there are areas in the application that only have a single focusable control, like a ListView. It is annoying to have to select the control first before it will respond to the other navigation keys.

      Currently I use this hack to work around the problem:

      {noformat}
          tableView.requestFocus();
          TwoLevelFocus.setInternalFocus(tableView);
      {noformat}

      Together with this support class:

      {noformat}
      package hs.mediasystem.util.javafx;

      import java.lang.reflect.Field;

      import javafx.scene.control.Control;

      import com.sun.javafx.scene.control.behavior.BehaviorBase;
      import com.sun.javafx.scene.control.behavior.ListViewBehavior;
      import com.sun.javafx.scene.control.behavior.TableViewBehavior;
      import com.sun.javafx.scene.control.behavior.TwoLevelFocusBehavior;
      import com.sun.javafx.scene.control.skin.BehaviorSkinBase;

      public class TwoLevelFocus {

        public static void setInternalFocus(Control control) {
          BehaviorBase<?> behavior = ((BehaviorSkinBase<?,?>)control.getSkin()).getBehavior();

          if(behavior instanceof ListViewBehavior || behavior instanceof TableViewBehavior) {
            try {
              Field field = behavior.getClass().getDeclaredField("tlFocus");

              field.setAccessible(true);
              TwoLevelFocusBehavior twoLevelFocusBehavior = (TwoLevelFocusBehavior)field.get(behavior);

              if(twoLevelFocusBehavior != null) {
                twoLevelFocusBehavior.setExternalFocus(false);
              }
            }
            catch(Exception e) {
              throw new IllegalStateException(e);
            }
          }
        }
      }
      {noformat}

            Unassigned Unassigned
            jhendrikx John Hendrikx
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Imported: