12 Replies - 1373 Views - Last Post: 10 March 2011 - 11:22 PM Rate Topic: -----

#1 oncecupatatime  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 05-February 11

[Q] java swing tabs help

Posted 10 March 2011 - 05:04 PM

Hi, i'm wondering if there's a way to access different tabs other than using the tab buttons on the top. I.e. having an extra button that can switch the tabpane to tab 2. Thanks!
Is This A Good Question/Topic? 0
  • +

Replies To: [Q] java swing tabs help

#2 bmcginnis  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 130
  • Joined: 21-February 09

Re: [Q] java swing tabs help

Posted 10 March 2011 - 05:23 PM

I'm not sure exactly what you're talking about. Do you mean switching tabs in a JTabbedPane component?

This post has been edited by bmcginnis: 10 March 2011 - 05:24 PM

Was This Post Helpful? 0
  • +
  • -

#3 oncecupatatime  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 05-February 11

Re: [Q] java swing tabs help

Posted 10 March 2011 - 05:26 PM

i'm talking about JTabbedPanes. and the pane itself. is there anyway to access it through other methods other than the TABS themselves?

View Postbmcginnis, on 10 March 2011 - 05:23 PM, said:

I'm not sure exactly what you're talking about. Do you mean switching tabs in a JTabbedPane component?


yea switching between tabs in the JTabbedPane component without using the tabs that came with them. Thanks for rewording your response. it was very noble of you
Was This Post Helpful? 0
  • +
  • -

#4 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: [Q] java swing tabs help

Posted 10 March 2011 - 05:28 PM

Perhaps look at the tutorial written by Moogoo found here. Also have a read over at Oracle Java website it states the following

Quote

To Switch to Specific Tabs

There are three ways to switch to specific tabs using GUI.
Using a mouse. To switch to a specific tab, the user clicks it with the mouse.
Using keyboard arrows. When the JTabbedPane object has the focus, the keyboard arrows can be used to switch from tab to tab.
Using key mnemonics. The setMnemonicAt method allows the user to switch to a specific tab using the keyboard. For example, setMnemonicAt(3, KeyEvent.VK_4) makes '4' the mnemonic for the fourth tab (which is at index 3, since the indices start with 0); pressing Alt-4 makes the fourth tab's component appear. Often, a mnemonic uses a character in the tab's title that is then automatically underlined.
To switch to a specific tab programmatically, use the setSelectedIndex or the setSelectedComponent methods.


Found here
Was This Post Helpful? 2
  • +
  • -

#5 oncecupatatime  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 05-February 11

Re: [Q] java swing tabs help

Posted 10 March 2011 - 05:30 PM

View PostDaneAU, on 10 March 2011 - 05:28 PM, said:

Perhaps look at the tutorial written by Moogoo found here. Also have a read over at Oracle Java website it states the following

Quote

To Switch to Specific Tabs

There are three ways to switch to specific tabs using GUI.
Using a mouse. To switch to a specific tab, the user clicks it with the mouse.
Using keyboard arrows. When the JTabbedPane object has the focus, the keyboard arrows can be used to switch from tab to tab.
Using key mnemonics. The setMnemonicAt method allows the user to switch to a specific tab using the keyboard. For example, setMnemonicAt(3, KeyEvent.VK_4) makes '4' the mnemonic for the fourth tab (which is at index 3, since the indices start with 0); pressing Alt-4 makes the fourth tab's component appear. Often, a mnemonic uses a character in the tab's title that is then automatically underlined.
To switch to a specific tab programmatically, use the setSelectedIndex or the setSelectedComponent methods.


Found here


thanks for those links but i've gone through them. I've already done those. I'm trying to look for a method to override the switching of the tabs. i.e. accessing that through a button. (for GUI purposes). my tabbedpane right now has hidden tabs and i just need to implement something to switch tabs.
Was This Post Helpful? 0
  • +
  • -

#6 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: [Q] java swing tabs help

Posted 10 March 2011 - 06:03 PM

Look the link provided you with the functions to use.

Say you have a GUI class with your JTabbedPane and two JButtons moveLeft, moveRight. All you need to do is addActionListener(this) to both of them and then use the two functions mentioned above or their compliments. their get methods.

Below is an actionPerformed that will do what is required.
public void actionPerformed(ActionEvent evt) {
	
	// check there is more than zero tabs
	if (tabbedPane.getTabCount() == 0) {
		System.err.println("No Tabs In Pane");
		return;
	}
	if (evt.getSource() == moveLeft) {
		if(tabbedPane.getSelectedIndex() == 0)
			tabbedPane.setSelectedIndex(tabbedPane.getTabCount()-1);
		else {
			tabbedPane.setSelectedIndex(tabbedPane.getSelectedIndex()-1);
		}
	}
	if (evt.getSource() == moveRight) {
		if(tabbedPane.getSelectedIndex() == tabbedPane.getTabCount()-1)
			tabbedPane.setSelectedIndex(0);
		else {
			tabbedPane.setSelectedIndex(tabbedPane.getSelectedIndex()+1);
		}
		
	}
}


You can tidy it up or improve its efficiency i may supplement with a tutorial on this.

This post has been edited by DaneAU: 10 March 2011 - 06:04 PM

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: [Q] java swing tabs help

Posted 10 March 2011 - 06:08 PM

tabbedPane.setSelectedIndex(index);
What else do you need ?
Note that this method will ignore if the Tab pane is enabled or not
Was This Post Helpful? 0
  • +
  • -

#8 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: [Q] java swing tabs help

Posted 10 March 2011 - 06:14 PM

pbl you do need the getSelectedIndex() method as if you try to keep moving to the right past the number of panels it will throw an IndexOutOfBounds exception, you should check that the intended index is there before trying to move to it, you should also check that there are tabs available prior also as the same exception is likely to occur. :)

This post has been edited by DaneAU: 10 March 2011 - 06:29 PM

Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: [Q] java swing tabs help

Posted 10 March 2011 - 06:24 PM

View PostDaneAU, on 10 March 2011 - 08:14 PM, said:

pbl you do need the checkSelectedIndex() method as if you try to keep moving to the right past the number of panels it will throw an IndexOutofBounds exception, you should check that the intended index is there before trying to move to it, you should also check that there are tabs available prior also as the same exception is likely to occur. :)

Where did I talked about checkSelectedIndex() ? if ever this method exists ?
getTabCount() should allow you to validate your index
Was This Post Helpful? 2
  • +
  • -

#10 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: [Q] java swing tabs help

Posted 10 March 2011 - 06:28 PM

Woops sorry pbl that was a typo, i meant to say getSelectedIndex(), my whole post was about checking things first so i mashed the two together lol.

Sorry for that :surrender:)

As you mentioned getTabCount() is also useful to validate the index :)
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: [Q] java swing tabs help

Posted 10 March 2011 - 06:31 PM

View PostDaneAU, on 10 March 2011 - 08:28 PM, said:

Woops sorry pbl that was a typo, i meant to say getSelectedIndex(), my whole post was about checking things first so i mashed the two together lol.

Sorry for that :surrender:)

As you mentioned getTabCount() is also useful to validate the index :)

Don't neither see any getSelectedIndex() in my posts :)
Was This Post Helpful? 0
  • +
  • -

#12 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: [Q] java swing tabs help

Posted 10 March 2011 - 06:47 PM

That is true, i only see setSelectedIndex() :donatello:
Was This Post Helpful? 0
  • +
  • -

#13 oncecupatatime  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 05-February 11

Re: [Q] java swing tabs help

Posted 10 March 2011 - 11:22 PM

i actually resorted to using CardLayout. I have a problem though. it seems like my buttons aren't working. i.e. when i press a button thats supposed to trigger card2, it doesn't do anything. Here's a rough idea of what i'm doing


here is how i setup my buttons so far
  JButton menuButtons = new JButton("CARD1");
		menuButtons.setActionCommand("CARD1");
		menuButtons.addActionListener(this);



i used this following line to add my cards in the main panel

displaypanelcards.add(card1,CARD1);


public void actionPerformed(ActionEvent e) {
       cardManager.show(displaypanelcards, (String)e.getActionCommand());
        
        if (e.getSource() == CARD1)
            cardManager.show(displaypanelcards, "CARD1");
            
          if (e.getSource() == CARD2)
        	 System.out.print("hey");
            //cardManager.show(displaypanelcards, "CARD2");
        	  cardManager.show(displaypanelcards, "CARD2");            
    }


what do i need to change so that clicking Button CARD2 will invoke the card CARD2? Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1