Results 1 to 7 of 7
  1. #1
    javanewbie is offline Member
    Join Date
    Jun 2008
    Location
    Australia
    Posts
    43
    Rep Power
    0

    Question [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?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    addChangeListener(...)

    db

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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);
        }
    }

  4. #4
    javanewbie is offline Member
    Join Date
    Jun 2008
    Location
    Australia
    Posts
    43
    Rep Power
    0

    Default

    I was expecting like:

    Java Code:
     	private void managementTab_mouseClicked(MouseEvent e) 
     	{ 
     		System.out.println("\nmanagementTab_mouseClicked(MouseEvent e) called."); 
     		// TODO: Add any handling code here 
      
     	}
    Is there a way I could 'manipulate' other stuff like the buttons, textfields, etc. outside the pane where the tabs are located?

    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);
    			}
    		}
    	}

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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
    Java 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.
        }
    }
    becomes something like this
    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.
        }
    }

  6. #6
    javanewbie is offline Member
    Join Date
    Jun 2008
    Location
    Australia
    Posts
    43
    Rep Power
    0

    Default

    Quote Originally Posted by hardwired View Post
    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.

    Thanks, Sir. I forgot to edit this thread as 'SOLVED'. I managed to manipulate them pretty well. Thanks again.

  7. #7
    deeparanbhor is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default

    hi,

    Pls provide your code snippet which you changed, for both tabbed pane and other components (like button in your case) can be seen on frame.

Similar Threads

  1. JTree and Tabbed Pane
    By paluee in forum AWT / Swing
    Replies: 1
    Last Post: 03-30-2009, 07:03 AM
  2. Mouse Event + Image Thresholding
    By ojmayolebron in forum AWT / Swing
    Replies: 0
    Last Post: 03-27-2009, 01:17 AM
  3. [SOLVED] Jtabbed pane hide a tab...?
    By prabhurangan in forum AWT / Swing
    Replies: 5
    Last Post: 08-26-2008, 09:31 AM
  4. Tabbed pane using struts 2.x......?
    By prabhurangan in forum Web Frameworks
    Replies: 1
    Last Post: 07-19-2008, 07:48 AM
  5. call exe in Desktop Pane
    By smartsubroto in forum New To Java
    Replies: 4
    Last Post: 06-16-2008, 08:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •