Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Focus owner temporary changes to null

Tags:

java

focus

swing

I'm pretty new to Swing development, hope my question is not a stupid one.

I've got a following problem. I am tracking the focus using KeyboardFocusManager, listening for property permanentFocusOwner changes. However when the focus changes from one control to another, I get intermediate change of permanentFocusOwner property to null.

My current UI logic is making some changes to the controls when the focus is inside one of the panels or its child panels. However getting intermediate null breaks this logic.

I searched in Google for information about this problem, didn't find anything relevant.

The question is, whether this behaviour is by design, and if there is some way to workaround intermediate nulls.

Here is the minimal application reproducing the said behaviour:

import java.awt.*;
import java.beans.*;
import javax.swing.*;

public class FocusNullTest extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                FocusNullTest self = new FocusNullTest();
                self.setVisible(true);
            }
        });
    }

    public FocusNullTest() {
        setSize(150, 100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container contentPane = getContentPane(); 
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));

        contentPane.add(new JButton("1"));
        contentPane.add(new JButton("2"));

        KeyboardFocusManager focusManager =
            KeyboardFocusManager.getCurrentKeyboardFocusManager();
        focusManager.addPropertyChangeListener(
                "permanentFocusOwner",
                new PropertyChangeListener() {
                    @Override
                    public void propertyChange(PropertyChangeEvent e) {
                        System.out.println("permanentFocusOwner changed from: "
                                           + e.getOldValue());
                        System.out.println("permanentFocusOwner changed to  : "
                                           + e.getNewValue());
                    }
                });
    }
}

The log output is:

(program start, focus sets to button 1 automatically)
permanentFocusOwner changed from: null
permanentFocusOwner changed to : javax.swing.JButton[,0,18,41x26, (skipped)]
(clicked on button 2)
permanentFocusOwner changed from: javax.swing.JButton[,0,18,41x26, (skipped)]
permanentFocusOwner changed to : null
permanentFocusOwner changed from: null
permanentFocusOwner changed to : javax.swing.JButton[,41,18,41x26, (skipped)]


(optional part, on the code intention)
My goal is to make something looking like a list view, where the entries expand and display more information when they get focus (and collapse back when they lose it). The expanded view contains some additional buttons.

JList doesn't seem to be the appropriate control, because (1) it doesn't allow clicks on the buttons, and (2) its entries have constant height, whereas I want the entries to expand dynamically on focus. JTable with its edit mode seems to be not an appropriate solution as well, at least because of constant entry size.

So I am using plain JPanel with a vertical box layout as a container, and subscribe to model changes and update the visuals manually. The problem is that when I click on a button, the containing list item loses focus. I could detect that the focus still stays within the list item if the focus wouldn't change to null temporarily.

like image 848
Vlad Avatar asked Sep 10 '11 14:09

Vlad


2 Answers

KeyboardFocusManager is firing two events for most properties (as of beans spec, it shouldn't - never found out the reason, just guessing that the asynchrous nature of focus somehow might be the reason)

   firePropertyChange(someProperty, oldValue, null)
   firePropertyChange(someProperty, null, newValue)

for doing stuff depending on newVaue, wait for the second

like image 172
kleopatra Avatar answered Sep 28 '22 10:09

kleopatra


As workaround, store the last "real" previous focus owner as a member in your event handler.

if ((e.getOldValue() != null) && (e.getNewValue() == null))
 prev_owner = e.getOldValue();

Then you'll have a handle to that object when you focus actually lands on the target. Handle the highlighting changes only when a real component actually receives focus (i.e. when getNewValue() is non-null).

(The behavior seems consistent with what is described in The AWT Focus Subsystem, in the sens that the previous component loses its focus first, then the target component gets it. It's not atomic, so there is a period of time where nothing actually has focus. But I'm no expert, so this may vary.)

like image 29
Mat Avatar answered Sep 28 '22 08:09

Mat