Where is my "focus lost" event?

Hello all,

when leaving a docking frame to some other frame (or even leaving the VM) I get no “focus lost” event from the Dockable.

The following example shows two frames: clicking the “docking”-frame I get a “focus gained” event, then clicking the “normal” frame there is no “focus lost” event of the dock.


    public static void main(String[] args) {
        JFrame frame = new JFrame("docking frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        CControl control = new CControl(frame);
        CContentArea contentarea = control.getContentArea();
        frame.add(contentarea);

        DefaultSingleCDockable d1 = new DefaultSingleCDockable("d1", "dock");
        d1.addFocusListener(new MyFL());
        CGrid grid = new CGrid(control);
        grid.add(0, 0, 1, 1, d1);
        contentarea.deploy(grid);

        frame.setBounds(100, 100, 300, 300);
        frame.setVisible(true);

        JFrame frame2 = new JFrame("normal frame");
        frame2.setBounds(500, 100, 300, 300);
        frame2.setVisible(true);

    }

    public static class MyFL implements CFocusListener {

        @Override
        public void focusGained(CDockable dockable) {
            System.out.print("focus gained");
        }

        @Override
        public void focusLost(CDockable dockable) {
             System.out.print("focus lost");
       }

    }

}

Thanks for your help,
Thilo

DockingFrames only listens to “mouse clicked” events, and then calculates which Dockable must have gained or lost the focus. When you click on something outside of DockingFrames, then DF does not get that “focus gained” event, and thus does not do anything (the code responsible for that behavior can be found in DefaultMouseFocusObserver).

The reason DF is listening for the mouse is, that actually listening for the “focus events” did not work very well. In Swing “focus” somehow works asynchronously, and I got some funny effects when I tried to work directly with the focus events, for example the focus starting to jump from one Component to another and back. Also listening to the “focus” events would lead to no action when clicking on a disabled Component.

thanks for the straight and honest explanation. Then I’ll ignore the CFocusListener and go back listening to the active Swing components.