Hi, i would like to ask about one problem that i tried to resolve in java. I have a lot of objects (textFields in this case) and i would like to read from them (calling method getText()) at the same time, or better explained, in one or few commands.
I have already tried adding them into the container but it didnt work. Maybe ihave done it wrong. Is there any method how to work with them as a group and calling their methods simultaneosly(in one or few command)? I thought, it could work using Java Message Service but i havent worked with it yet. Sending the message to all these objects that they have to do somethin.
8 replies to this topic
#1
Posted 05 December 2010 - 01:22 PM
|
|
|
#2
Posted 05 December 2010 - 02:27 PM
What do you mean at the same time? What do you need to do exactly?
#3
Posted 06 December 2010 - 01:44 AM
i need to have several objects(of the same type) in a group, so that i can work with them.
For example i have 4 textFields and i want to read from them. Now i have to do this
String s1 = jTextField1.getText();
String s2 = jTextField2.getText();
String s3 = jTextField3.getText();
String s4 = jTextField4.getText();
Because if i have 20 text fields its a little bit huge code :c-smile:
For example i have 4 textFields and i want to read from them. Now i have to do this
String s1 = jTextField1.getText();
String s2 = jTextField2.getText();
String s3 = jTextField3.getText();
String s4 = jTextField4.getText();
Because if i have 20 text fields its a little bit huge code :c-smile:
#4
Posted 06 December 2010 - 02:08 AM
int SIZE=20;
JTextField [] fields = new JTextField[SIZE];
String [] strings = new String[SIZE];
for(int i=0;i<SIZE;i++)
strings[i]=fields[i].getText();
Here you go ;)
#5
Posted 06 December 2010 - 02:52 AM
I would suggest to add the objects into a collection class, and do the "work" with the collection class. I mean by adding some method to the collection class. This way it's easier to maintain and to improve.
#6
Posted 06 December 2010 - 02:53 AM
Yes but he has to get rid of redundant code, and the only way to go are arrays.
#7
Posted 06 December 2010 - 03:13 AM
Hi eafkuor,
My suggestion is for the real implementation. There is no problem with your code, however it's better use your code inside a collection class for the reasons I mentioned. :)
My suggestion is for the real implementation. There is no problem with your code, however it's better use your code inside a collection class for the reasons I mentioned. :)
#8
Posted 06 December 2010 - 03:14 AM
LuthfiHakim said:
Hi eafkuor,
My suggestion is for the real implementation. There is no problem with your code, however it's better use your code inside a collection class for the reasons I mentioned. :)
My suggestion is for the real implementation. There is no problem with your code, however it's better use your code inside a collection class for the reasons I mentioned. :)
Oh yes, there's not doubt about that!
#9
Posted 10 December 2010 - 04:26 AM
thank you guys for help :) now it works and looks better:)
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









