0

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?

1ac0
  • 2,875
  • 3
  • 33
  • 47
  • you're looking for a ChangeListener on the tabbed pane – xyz Feb 08 '12 at 09:15
  • thanks, doing some examples with this listener. have small problem: it should return tab name or index. tab name is not good (app is multilingual). when i will be depend on tab index, can it change? - for example when i add button to tab and then close second tab from four tabs and then i add this tab back (from menu), tab will be last. will index change? – 1ac0 Feb 08 '12 at 09:30
  • ok, after some elaborating final (?) decision: tab's order is based on aplication startup order - order of the "jTabbedPane.addTab("Test tab 1", null, panelTest1, null)" in app gui init. this won't change even if tab is closed and then reopened during app live cycle. so if the tab has been on position 0, then closed and then programaticaly reopened at position 4, index stil be 0. – 1ac0 Feb 08 '12 at 14:21

2 Answers2

2

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

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • yes, i now this, data retrieval will be in background task and while talking with database there will be modal jdialog "wait please, data loading..." – 1ac0 Feb 08 '12 at 09:54
0

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
                            }

        }
    });
Prateek Sharma
  • 346
  • 3
  • 12
  • sure, after some elaboration i checked that if i have 4 tabs on jtabbedpane and then programaticaly close and reopen tab, index will be same, doesn't matter if position changed. index is based on app first gui launch and won't change. so each jpanel will know it's index and in main app class should be public static variable so app know actual opened tab. and each jpanel will check this property so it can decide if reload data or not. – 1ac0 Feb 08 '12 at 14:46