Hello,
I am developing a simple graph drawing application where each graph could be painted in internal frame.
I want to call a specified function whenever an internal frame got focus. I’ve tried to add “addFocusListener” to the internal frame but it didn’t work.
Multiple workspace (internal frame) can be created from file>New.
here is the newMenuIemAction

public void newGraph() {

        NewGraphDialog dialog = new NewGraphDialog(GraphGUIApp.getApplication().getMainFrame(), true);
        dialog.setLocationRelativeTo(GraphGUIApp.getApplication().getMainFrame());
        dialog.setVisible(true);
        if (dialog.isConfirmed()) {
 
            PaintPanel panel = new PaintPanel(width, height); // extends JPanel

            PaintFrame internalFrame = new PaintFrame(title, panel);
            try {
                internalFrame.setSelected(true);
            } catch (PropertyVetoException ex) {
                Logger.getLogger(GraphGUIView.class.getName()).log(Level.SEVERE, null, ex);
            }

            viewDesktopPane.add(internalFrame);
            viewDesktopPane.setDragMode(javax.swing.JDesktopPane.OUTLINE_DRAG_MODE);
            internalFrame.setVisible(true);

            internalFrame.addFocusListener(new java.awt.event.FocusAdapter() {
                @Override
                public void focusGained(java.awt.event.FocusEvent e) {

                    System.out.println("focus");
                }

                @Override
                public void focusLost(java.awt.event.FocusEvent e) {
                }
            });

        }
    }

I' ve spent two days trying to figure out a solution, but I couldn't.
Any hint or help is very appreciated.

Ok, just found that the focus listener works if you add

internalFrame.setFocusable(true);

It works! thank you :)
Just one more question:
Which one do you think is better? using internal frames or tabbedPane?

It just depends on how the users will work with the separate panels. With tabs, all will be layered on the screen and only one can be active at any one time, whereas with the internal frames you can show as many or as few of the collection as you wish and they can be arranged in any manner you choose to view simultaneously.

I'll stick to internal frames.
Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.