473,395 Members | 1,649 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Gaining control over objects not referenced by variables

daiyen
9
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

Expand|Select|Wrap|Line Numbers
  1.     private TextField createTextField(String text, String name, int cols)
  2.     {
  3.         TextField nextTextField = new TextField(text, cols);
  4.         nextTextField.setName(name); //for me to have something simple to refer to this object later on
  5.  
  6.         return nextTextField;            
  7.     }
for creating the textfields, and for getting to them:
Expand|Select|Wrap|Line Numbers
  1.     private Component getByName(String componentName, Container cont)        
  2.     {
  3.         for(Component c : cont.getComponents())
  4.         {
  5.             System.out.println(c.getName()); //to be able to see where the search is going
  6.             if(c.getName() == componentName)
  7.             {
  8.                 return this.getComponentAt(c.getLocation());
  9.             }
  10.         } 
  11.         return null;
  12.     }
Expand|Select|Wrap|Line Numbers
  1. getByName("Nickname", leftPanel).setForeground(Color.orange);
does nothing. In fact setBackground does nothing as well.. setEnabled(false) 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:)
Apr 23 '10 #1

✓ answered by jkmyoung

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("Nickname", leftPanel) == null) System.out.println("Nickname is null!");

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

Is the TextField obscured by any other object?

2 1177
jkmyoung
2,057 Expert 2GB
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("Nickname", leftPanel) == null) System.out.println("Nickname is null!");

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

Is the TextField obscured by any other object?
Apr 23 '10 #2
daiyen
9
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.. :)
Apr 23 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Rogier | last post by:
Hi All, I have a problem with referenced data updates using Remoting. I can access and update all data obtained from a server's existing object from any client. However, client can only see...
5
by: Colin Anderson | last post by:
I discovered, with great excitement, this article http://www.davison.uk.net/vb2notes.asp when researching methods for emailing from Access via Notes. Unfortunatly, when I run this I get a...
2
by: Michael Søndergaard | last post by:
I can't seem to figure this one out. I have a list of objects containing objects like A, B, C. I also have another object D which referer object A. At some point in my code I need to delete the...
2
by: Mel | last post by:
This may be a stupid question, but here goes... I have created a NameValueCollection in my website's application state. If, during a page request, I add a string key and string value to the...
3
by: Brent McIntyre | last post by:
Good evening all, I am going crazy with a problem that I am sure has a simple solution. I need to add items to a ComboBox on a Form. I can do it simply when I code on form.vb, but when go to...
66
by: Mike Meyer | last post by:
It seems that the distinction between tuples and lists has slowly been fading away. What we call "tuple unpacking" works fine with lists on either side of the assignment, and iterators on the...
6
by: Fuzzyman | last post by:
Hello all, I'm trying to extract the code object from a function, and exec it without explicitly passing parameters. The code object 'knows' it expects to receive paramaters. It's 'arg_count'...
3
by: =?Utf-8?B?VG9tKys7?= | last post by:
When I create an object in VB.NET I define both properties (data) and methods (code). If I have 1,000 instances of this object stored in a Dictionary as Dictionary(of int32, object) then I am...
8
by: Brad Walton | last post by:
Hello. First post, but been doing a bit of reading here. I am working on a project in Java, but decided to switch over to C# after seeing some of the additional features I can get from C#. One of...
41
by: =?Utf-8?B?VGltIE1hcnNkZW4=?= | last post by:
Hi, I am after suggestions on the best practice declaring and destroying objects. some example code: Private Sub MySub Dim frmMyForm As MyForm Try
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.