Results 1 to 7 of 7
- 06-01-2009, 08:14 AM #1
Member
- Join Date
- Jun 2008
- Location
- Australia
- Posts
- 43
- Rep Power
- 0
[SOLVED] Mouse event in JTabbed Pane
Hi,
Is there a way that I clicked a certain tab coming from a tabbed pane, a method/event will happen.
This is the scenario:

If I clicked the Tab #1, the generate button will show up.
If I clicked the Tab #2, it will hide.
If I clicked the Tab #3, another button will show up.
That's the idea. Take note that the generate button is outside the pane of the JTabbedPane. so let's say that the generate button is on the 'main' pane.
Any suggestions?
- 06-01-2009, 08:23 AM #2
addChangeListener(...)
db
- 06-01-2009, 08:41 AM #3
The JTabbedPane class api Method Summary section shows an addChangeListener method.
If you look down lower to the section of methods inherited from superclasses you'll find more choices.
Java Code:import java.awt.*; import javax.swing.*; import javax.swing.event.*; public class TabAware { private JTabbedPane getContent() { final JTabbedPane tabbedPane = new JTabbedPane(); Color[] colors = { Color.red, Color.green.darker(), Color.blue }; for(int i = 0; i < colors.length; i++) { JPanel panel = new JPanel(); panel.setBackground(colors[i]); tabbedPane.addTab("Tab " + (i+1), panel); } tabbedPane.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { int selectedIndex = tabbedPane.getSelectedIndex(); String s; switch(selectedIndex) { case 0: s = "show report button"; break; case 1: s = "hide report button"; break; case 2: s = "another button will show up"; break; default: s = "unexpected tabIndex"; } System.out.println(s); } }); return tabbedPane; } public static void main(String[] args) { TabAware test = new TabAware(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(test.getContent()); f.setSize(400,175); f.setLocation(100,100); f.setVisible(true); } }
- 06-01-2009, 10:08 AM #4
Member
- Join Date
- Jun 2008
- Location
- Australia
- Posts
- 43
- Rep Power
- 0
I was expecting like:
Is there a way I could 'manipulate' other stuff like the buttons, textfields, etc. outside the pane where the tabs are located?Java Code:private void managementTab_mouseClicked(MouseEvent e) { System.out.println("\nmanagementTab_mouseClicked(MouseEvent e) called."); // TODO: Add any handling code here }
Here is my current code/s:
Java Code:public void mouseClicked(MouseEvent e) { Object swingObject = e.getSource(); if (swingObject.equals(managementTab.getSelectedIndex())){ if (managementTab.getSelectedIndex() == 0){ generateButton.setEnabled(true); }else if (managementTab.getSelectedIndex() == 1){ generateButton.setEnabled(false); }else{ generateButton2.setEnabled(false); } } }
- 06-01-2009, 05:53 PM #5
Is there a way I could 'manipulate' other stuff like the buttons, textfields, etc. outside the pane where the tabs are located?
Yes. Just make the reference to the components you want to manipulate available to your event code. This could be as simple as moving the declarations from inside the method, where they are local variables, out into the class where they become member variables.
In java, something like this
becomes something like thisJava Code:class Pseudo { void methodThatCreatesComponents() { // Local variable declarations have scope // limited to this method body, viz, inside // curley braces. JButton button = new JButton("hello"); JTextField textField = new JTextField(9); // Configure and add to gui... } public void someEventMethod(SomeEvent e) { // Unable to access components. } }
Java Code:class Pseudo { // Member variables have class scope. JButton button; JTextField textField void methodThatCreatesComponents() { // Instantiate member variable declarations. button = new JButton("hello"); textField = new JTextField(9); // Configure and add to gui... } public void someEventMethod(SomeEvent e) { // Now we have access to components declared as // member variables and can manipulate them. } }
- 06-09-2009, 09:46 AM #6
Member
- Join Date
- Jun 2008
- Location
- Australia
- Posts
- 43
- Rep Power
- 0
- 06-10-2009, 08:50 AM #7
Member
- Join Date
- Apr 2009
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
JTree and Tabbed Pane
By paluee in forum AWT / SwingReplies: 1Last Post: 03-30-2009, 06:03 AM -
Mouse Event + Image Thresholding
By ojmayolebron in forum AWT / SwingReplies: 0Last Post: 03-27-2009, 12:17 AM -
[SOLVED] Jtabbed pane hide a tab...?
By prabhurangan in forum AWT / SwingReplies: 5Last Post: 08-26-2008, 08:31 AM -
Tabbed pane using struts 2.x......?
By prabhurangan in forum Web FrameworksReplies: 1Last Post: 07-19-2008, 06:48 AM -
call exe in Desktop Pane
By smartsubroto in forum New To JavaReplies: 4Last Post: 06-16-2008, 07:49 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks