1ac0
1ac0

Reputation: 2939

jtable view event

I'm lost: I have JFrame, in JFrame is JTabbedPane and in JTabbedPane are 4 JPanels (tabs). On each JPanel (tab) is JScrollPane with JTable (total: 4 JTable's). Each JTable I'm populating with data from DB at the moment of start app - works perfect.

Now I would like populate data for each JTable at the moment when I click on JTabbedPane (tab). I was trying: "public class MyTest1Tab extends JPanel implements FocusListener" with "this.addFocusListener(this);" in constructor and I have implemented focusGained(FocusEvent e) and focusLost(FocusEvent e) methods. But I feel, that this solution isn't corrrect.

From my point of view I need something like listening for click on tabs on JTabbedPane or some event which is fired when JTable is shown.

Does someone know how I can listen for specific JTable is shown?

Upvotes: 0

Views: 409

Answers (2)

Prateek Sharma
Prateek Sharma

Reputation: 346

i think you should register an event handler with JTabbedPane. It will give you the selected Tab and then based on that you can populate the Jtable specific to that JTab.

for example:

                    tabbedPane.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent arg0) {
            // TODO Auto-generated method stub
            if((((JTabbedPane)arg0.getSource()).getSelectedIndex())==0)
                            {
                                 //Load Jtable associated with first JTab
                            }

        }
    });

Upvotes: 0

mKorbel
mKorbel

Reputation: 109813

Now I would like populate data for each JTable at the moment when I click on JTabbedPane (tab).

don't do that this way, because Swing GUI would be un_responsive or freeze untill heavy and long task ended, you have to redirect this taks to the Background task, For Swing GUI there are two ways

or

  • Runnable#Thread, notice all output to the Swing GUI must be wrapped inside invokeLater(), example

then this way should be user_non_friendly, let's user update JTable's contents from JButton

Upvotes: 2

Related Questions