AndroidDev
AndroidDev

Reputation: 21237

Detecting change of focus on jpanel

I have a JTabbedPane. On this component are four JPanels. On each of those panels there is a scrollPanel. Finally, on each scrollPanel is another JPanel. I need to keep track of which of these final JPanels is displayed at any moment.

So I created a class variable called activePanel. For each of the final JPanels, I have a focus event method set so that when the panel gets the focus, this variable is set accordingly. For example:

private void pnlAdditionFocusGained(java.awt.event.FocusEvent evt) {
        activeTab = "pnlAddition";
        System.out.println(activeTab);
    }

Unfortunately, this isn't working as I had hoped. Can somebody suggest what I should do to keep track of this? Thank you.

Upvotes: 0

Views: 560

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347204

JPanel is not focusable by default.

Instead you should keep track of the selected tab

Check out How to use Tabbed Panes for more information

Upvotes: 1

Related Questions