Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6562337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:41:51+00:00 2026-05-25T13:41:51+00:00

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

  • 0

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,41×26, (skipped)]
(clicked on button 2)
permanentFocusOwner changed from: javax.swing.JButton[,0,18,41×26, (skipped)]
permanentFocusOwner changed to : null
permanentFocusOwner changed from: null
permanentFocusOwner changed to : javax.swing.JButton[,41,18,41×26, (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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:41:51+00:00Added an answer on May 25, 2026 at 1:41 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.