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.