Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Referencing JComponents programatically  RSS feed

 
J.D. Thompson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My application has approximately 23 JTextFields whose values will change based upon user input. I have only found two ways to change these values programatically:

1. Declare components globally within the class so they can be referenced directly by the class methods
2. Use the getComponents()method of the parent container to "find" the component

With some of the apps I am writing, option#1 involves over 50 "global" class variables, option #2 is tedious to sort through each time a value needs to change.

Any better ideas?

Example Code:

public class Foo
{
public JLabel globalLbl; //class global declaration

public Foo()
{
Container content=getContentPane();
JPanel owner=new JPanel();
owner.setLayout(new FlowLayout());

globalLbl=new JLabel("global");
owner.add(globalLbl);

//local Label declaration
JLabel localLbl=new JLabel("local");
owner.add(localLbl);

setVisible(true);
}

private void changeLabel()
{
globalLbl.setText("changedByMethod");

//here is where I can't find an easier way than described above for
//changing the 'localLbl' text
}
}

50 global class variables seems like alot to me, is it really?

Any and all replies/insights are deeply appreciated!!

JD
 
Ben Zung
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would using an array of JTextField help? ie,

And initialize each when creating. And to help identify them, you can assign them different names.

Good luck.

[ March 15, 2006: Message edited by: Ben Zung ]
[ March 15, 2006: Message edited by: Ben Zung ]
 
J.D. Thompson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben,

Thank you very much for your prompt reply.

I like how I can declare all of the variables quickly with your array idea.

I am guessing you intend for them to be declared as a class global variable and then in the buildGui portion assign something like:

//global level
JTextField [] fields=new JTextField[50];


//then within the build gui method:
JTextField localField=fields[1];

Effectively this is a similiar approach as option#1 with but not having to deal with 50 lines of declaration.

Thanks!
 
Ben Zung
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
It varies depending how you code your app.
But one thing I'd like to point out is that in between
//global level
JTextField [] fields=new JTextField[50];

and
//then within the build gui method:
JTextField localField=fields[1];

You need to initialize each of of the fields, otherwise you'll get a NullPointerException -- which had got me bad couple of times before.
It'll be something like this:

Good luck.
 
J.D. Thompson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Ben,

Right you are, again I would have probably done exactly that at least once or twice.

Take care!

JD
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!