[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 320
  • Last Modified:

Getting selected tabs in a jTabbedPane

I have a tabbed pane within a tabbed pane. If that makes sense.

Essentiall it looks like this

|HTTP| |TELNET| |Results|


and then if http or telent is selected, that tab has another jtabbedpane inside it with tabs going down the side, so it looks like:

|HTTP|

_____________
Tab 1
_____________
_____________
Tab 2
_____________
_____________
Tab 3
_____________
_____________
Tab 4
_____________
_____________
Tab 5
_____________

and the same for telent, now, I am trying to get the selected tabs and the accompanying child tab:

    public String getParentTab() {
        String parentTab = null;
        int parIndex = jTabbedPane2.getSelectedIndex();
        parentTab = jTabbedPane2.getTitleAt(parIndex);
        return parentTab;
    }
   
    public String getChildTab(Container c) {
        String childTab = null;
        Component[] component = c.getComponents();

        for (int i = 0; i < component.length; ++i)
        {
            if (component[i] instanceof JTabbedPane)
            {
                int childTabIndex = ((JTabbedPane)component[i]).getSelectedIndex();
                childTab = ((JTabbedPane)component[i]).getTitleAt(childTabIndex);
            }
        }
        return childTab;
    }

jTabbedPane2 is the upper tabbed pane that has HTTP, TELENT, and Results.

Underneath those is:

jTabbedPane for the HTTP sub Pane,

and

jTabbedPane1 for the TELNET Sub pane.

When I try to call the above methods, I get the HTTP correct, but then the childPane is coming back as tab1 when tab3 is actually selected.

When I call the getChildTab() I pass through the jTabbedPane2 component, is this right?

thanks


0
k41d3n
Asked:
k41d3n
  • 4
  • 3
  • 2
2 Solutions
 
zzynxSoftware engineerCommented:
Why don't you pass jTabbedPane or jTabbedPane1 to getChildTab?

Like

    public String getChildTab(JTabbedPane pane) {
         return pane.getTitleAt( pane.getSelectedIndex() );
    }
0
 
TimYatesCommented:
>> When I call the getChildTab() I pass through the jTabbedPane2 component, is this right?

No, you should pass through the component which is the selected tab.. ie:

jTabbedPane2.getSelectedComponent() ;
0
 
k41d3nAuthor Commented:
zzynx,

I can't do that, because I need to know which pane I am looking for.

I could do it, if I knew what pane I was passing through.

I think I need to find a way to get the pane, either jTabbedPane1 or jTabbedPane based on what tab is selected in jTabbedPane2.

if jTabbedPane2 is 0 then it's jTabbedPane, and if it's 1 then it's jTabbedPane1

hmmmmm, let me try that.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
TimYatesCommented:
Did you try my suggestion?
0
 
zzynxSoftware engineerCommented:
>> if jTabbedPane2 is 0 then it's jTabbedPane, and if it's 1 then it's jTabbedPane1
That's the hard coded way. Will surely work.

But you could ask the selected tab [ jTabbedPane2.getSelectedComponent(); ]
for the sub tab pane:

      jTabbedPane2.getSelectedComponent().getSubTabbedPane()

Of course you have to forsee that getSubTabbedPane() method yourself
 

0
 
TimYatesCommented:
>> But you could ask the selected tab [ jTabbedPane2.getSelectedComponent(); ] for the sub tab pane:

isn't that what I said?

/me worries his posts have become invisible

;-)
0
 
k41d3nAuthor Commented:
   public String getParentTab() {
        String parentTab = null;
        int parIndex = jTabbedPane2.getSelectedIndex();
        parentTab = jTabbedPane2.getTitleAt(parIndex);
        return parentTab;
    }
   
    public int getParentTabIndex() {
        int parentTabIndex;
        parentTabIndex = jTabbedPane2.getSelectedIndex();
        return parentTabIndex;
    }
   
    public String getChildTab(int pt) {
        String childTab = null;
        JTabbedPane pane = null;
        if (pt == 0) {
            pane = jTabbedPane;
        } else if (pt == 1) {
            pane = jTabbedPane1;
        }
        int childTabIndex = pane.getSelectedIndex();
        childTab = pane.getTitleAt(childTabIndex);
        return childTab;
    }

getParentTab() returns the jTabbedPane2 selected tab, and getChildTab(getParentTabIndex()) gets the sub tab :)

Thanks guys.
0
 
zzynxSoftware engineerCommented:
:)
0
 
TimYatesCommented:
Glad you got it sorted :-)

Tim
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now