Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reference an item embedded within a jPanel 1

Status
Not open for further replies.

TheObserver

Programmer
Mar 26, 2002
91
US
I have a jPanel that I create numerous labels and jTextFields within. Later in the program's execution, I have need to set and retrieve data from the jTextFields I've created. Is there an index based way to reference the jTextFields that are within the jPanel?

They seem to have an index when I add them to the jPanel, but I don't know which method to use to pull a reference to a particular jTextField back out.

They are not uniquely named and don't really have a way of uniquely referencing them (unless I'm missing something), which is by design.

 
You need an instance-variable to acess the field later.
Usually you do this with method-calls.
A very lean demo:
Code:
class Demo
{
    private JTextField useLater;

    public String getTheValue ()
    {
          return (useLater.getText ());
    }
    public Demo ()
    {
          useLater = new JTextField ();
          JPanel jp = new JPanel ();
          jp.add (useLater);
          // ... much more stuff
    }
}

seeking a job as java-programmer in Berlin:
 
What if all of the jTextFields are named the same? I thought of trying to create an array of them and access the array, but I wasn't able to get that to work correctly, which is why I'm naming them all the same, now. Plus, I'm trying to be as abstract as possible with this code so it's not tied to directly to the data it's accessing. It's aimed towards being completely dynamic. Which is why being able to reference the components via an index would be very helpful.
 
Array:
Code:
JTextField [] jtfArray = new JTextField [17];

for (i = ...
   JTextField jtf = new JTextField ();
   jtfArray[i] = jtf;
...
or this way:
Code:
class Dpanel extends JPanel 
...
   int index;
   ArrayList al;

   public void add (JTextField jt)
   {
       al.add (jt); 
       ++index;
   }
   public JTextField getTF (int idx)
   {
        return (JTextField) al.get (i);
   }

(both: very raw drafts of code)

seeking a job as java-programmer in Berlin:
 
For some reason I couldn't get ArrayLists to work. I kept getting NullPointerExceptions. I switched to a Vector and after a little rework things seem to be working better.

Thanks for the lead.
 
public JTextField getTF (int idx)
{
return (JTextField) al.get (i);
}

there was an error, needs to be al.get (idx); of course.

When you fell of a horse, you have to get on top again immediately, or you will start fearing of ArrayLists - ehh - horses.

Get the ArrayList to work.
The usage of Vector is discouraged by sun, so you will need an ArrayList in most cases.


seeking a job as java-programmer in Berlin:
 
Vectors and ArrayLists are mostly the same, except that Vector is synchronized and allows adding elements in an exact position.

Cheers.

Dian
 
I actually wasn't having the problems with the ArrayList "get" method, I was having problems adding objects to the ArrayList. It would consistently produce the NullPointerException I mentioned. I tried casting them, I tried adding with an index, just about everything I could think to do, but nothing worked.

I'm pretty sure I had the right classes/libs imported, so I don't think that was the problem...plus it wasn't choking on the ALs when I'd compile the class.

 
Code:
ArrayList al;
al = new ArrayList ();
al.add (new String ("foo"));
al.add ("bar");
al.add (1, "_foobar_");
System.out.println ((String) al.get (1));
al.remove (0);
...
Where is the problem?

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top