Gaining control over objects not referenced by variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • daiyen
    New Member
    • Apr 2008
    • 9

    Gaining control over objects not referenced by variables

    I've learned that a simple way of creating MenuItems is to create a method for it in which you add listeners to them and set their properties. Even back then, I couldn't find out a way to later on set any property of them. Now I'm trying to create TextFields just the same way, and I've almost found a way to set things after I've created them, but still, I fail.

    Here are the methods I'd created so far

    Code:
        private TextField createTextField(String text, String name, int cols)
        {
        	TextField nextTextField = new TextField(text, cols);
        	nextTextField.setName(name); //for me to have something simple to refer to this object later on
        	
        	return nextTextField;    		
        }
    for creating the textfields, and for getting to them:
    Code:
        private Component getByName(String componentName, Container cont)    	
        {
    		for(Component c : cont.getComponents())
    		{
    			System.out.println(c.getName()); //to be able to see where the search is going
    			if(c.getName() == componentName)
    			{
    				return this.getComponentAt(c.getLocation());
    			}
    		} 
    		return null;
        }
    Code:
    getByName("Nickname", leftPanel).setForeground(Color.orange);
    does nothing. In fact setBackground does nothing as well.. setEnabled(fals e) gets to somehow freeze the whole application. It's like by calling getByName method I'd get the whole JFrame or something - which is not the case.

    The question is, what do i do wrong?
    Any clue would be much appreciated!

    Thank you:)
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    This could be a number of things. Starting with the most basic:

    Have you added the TextField to the container object?

    Are you actually getting the objects from the method? eg do you have a debug stmt, like
    if( getByName("Nick name", leftPanel) == null) System.out.prin tln("Nickname is null!");

    Why do you use return this.getCompone ntAt(c.getLocat ion());
    and not return c? Is it possible that there are multiple components at this location?

    Is the TextField obscured by any other object?

    Comment

    • daiyen
      New Member
      • Apr 2008
      • 9

      #3
      I've always returned at least some component according to that println just before the return statement:)

      You're totally right about the needlessness of the getComponentAt, instead I'll return c immediately.

      And yes, I've added the TextField to the container:)

      Is this even the right way to do such thing or could you suggest a better way?:)

      Edit: returning c by itself has helped the case, I can now setEnable the right component:)

      I'm wondering however, if I could get the content of the specific component that I've just returned.. :)

      Comment

      Working...