A little newbie syntax problem
Now I have a few variables named var1, var2, var3 etc. Now i wonder how can i get to them in an easy manner.
example of what i mean
Code:
for(int x=1;x<10;x++)
{
if(var(x).isEnabled()==true)
}
So how do i write it to essentially get
if(var1.isEnabled()==true)
if(var2.isEnabled()==true) etc.
but in a way that it acctually works. Or am i forced to use tables?
Thanks
Re: A little newbie syntax problem
Quote:
Originally Posted by
Atkonis
Now I have a few variables named var1, var2, var3 etc. Now i wonder how can i get to them in an easy manner.
example of what i mean
Code:
for(int x=1;x<10;x++)
{
if(var(x).isEnabled()==true)
}
The == true part is unnecessary and clutters up your syntax.
Cleaner is to change this:
Code:
if (x == true) {
// ...
}
to this:
Quote:
So how do i write it to essentially get
if(var1.isEnabled()==true)
if(var2.isEnabled()==true) etc.
but in a way that it acctually works. Or am i forced to use tables?
The statement above confuses me since you don't give us any information as to how or why the code isn't working. Please remember that we can't comment on code not seen.
Re: A little newbie syntax problem
Hmm i tried to be as clear as possible hmm here i go again variables are javax.swing.JCheckBox but that's irrelevant because what i wanted to know what is the proper way of writing
a loop that gets me if(var1.isEnabled()==true) {do something},if(var2.isEnabled()==true) {do something}, if(var3.isEnabled()==true) {do something} etc
So
Code:
for(int x=1;x<10;x++)
{
if(var/*what do i write here to get 1, 2 ,3 etc*/.isEnabled()==true)
{ smth}
}
And make it work
Re: A little newbie syntax problem
Use an array (of JCheckBoxes) and you can iterate over the array and manipulate each JCheckBox in the array.
kind regards,
Jos
Re: A little newbie syntax problem
Yeah thanks but i still wonder if there's a syntax that makes it possible to do this the way i explained (im really curious cause i tried for like 0,5h )
Re: A little newbie syntax problem
Quote:
Originally Posted by
Atkonis
Yeah thanks but i still wonder if there's a syntax that makes it possible to do this the way i explained (im really curious cause i tried for like 0,5h )
Don't you have a textbook that talks about arrays? Something like this will do:
Code:
// declare and populate an array with JCheckBoxes
JCheckBox[] var= { new JCheckBox(), new JCheckBox(), new JCheckBox() };
// set them all selected
for (x= 0; x < var.length; x++)
var[x].setSelected(true);
kind regards,
Jos
Re: A little newbie syntax problem
Ah yes, now I see what you're getting at. Yep, arrays or collections such as ArrayList<JCheckBox> are the way to go. The pseudo-code that you're trying to work just doesn't work for Java because variable names don't really exist (for the most part) in compiled code. It's all about references and getting a handle on them.
Re: A little newbie syntax problem
Thanks again but that's not what I wanted to know (im successfully done(thanks)) but what I want to know is it is possible to do so the way i described ;)
Ahh just read Fuburable's answer thanks guys
Re: A little newbie syntax problem
It is "technically" possible to do with class fields using reflection, not local variables, but this would be a terrible and ugly kludge and is not recommended. Again, use an array or a List.