Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Gui objects and listeners

Options
  • 21-03-2005 11:19pm
    #1
    Closed Accounts Posts: 1,325 ✭✭✭


    Hey people,

    I have something along the lines of the following:
    
    Collection GUICollection = new GUICollection;
    
    for(int x = 0; x < profiledMethods.size(); x++)
     {			
    	
            //adds a different name to the label each iteration			
    	Label methodName = new Label(profiledMethods[x].getName());	
    				
    	methodName.addMouseListener(
            	new MouseListener()
    		{
    		   public void mouseDoubleClicked(MouseEvent me)
    		   {								
    			System.out.println("name is:" + methodName.getText());													
    	  	   }
    	           public void mouseReleased(MouseEvent me)
    	           {}
    	           public void mousePressed(MouseEvent me)
    		   {}
    		});
    				
    
    		GUICollection.add(methodName);		
    					
    }
    
    //iteratre through the GUICollection and add to Content Pane
    
    

    My problem is that when a user double clicks on any of the labels it only prints to the console the last value of methodName.getText() and none of the other names.

    e.g.

    profiledMethods = [method1, method2, method3]

    I get three labels with the text method1, method2 and method 3 on them.

    If a user double clicks on any of the labels, only the text method3 is printed to the console.

    Where am I going wrong? ( oh and if the above looks a tad different to normal Java graphics its because I am using Eclipse's draw2d and it does some things a little different)

    A.


Comments

  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    Erm, what are you trying to DO, precisely?


  • Closed Accounts Posts: 1,325 ✭✭✭b3t4


    rsynnott,

    I want to add a listener to each methodName label.

    A.


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    Ah, okay, the problem is that the MouseListener references methodName, and by the time it's called, methodName always points to the last label. I THINK if you replace methodName.getText() with ( (Label) me.getComponent() ).getText() it should work. This looks at what has actually been clicked (me.getComponent() returns the component that was clicked on.)


  • Closed Accounts Posts: 324 ✭✭madramor


    your mixing presentation and control.

    create the listener seperately.

    then add the single listener to each label

    Collection GUICollection = new GUICollection;
    MyListener myl = new MyListener();

    for(int x = 0; x < profiledMethods.size(); x++)
    {

    //adds a different name to the label each iteration
    Label methodName = new Label(profiledMethods[x].getName());
    methodName.addMouseListener(myl);
    UICollection.add(methodName);

    }
    //iteratre through the


Advertisement