Jump to content

Working with many objects

- - - - -

  • Please log in to reply
8 replies to this topic

#1
Reshi

Reshi

    Newbie

  • Members
  • Pip
  • 4 posts
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.

#2
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
What do you mean at the same time? What do you need to do exactly?

#3
Reshi

Reshi

    Newbie

  • Members
  • Pip
  • 4 posts
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:

#4
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
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
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 763 posts
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
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
Yes but he has to get rid of redundant code, and the only way to go are arrays.

#7
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 763 posts
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. :)

#8
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts

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. :)

Oh yes, there's not doubt about that!

#9
Reshi

Reshi

    Newbie

  • Members
  • Pip
  • 4 posts
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