Results 1 to 3 of 3
  1. #1
    Reshi is offline Member
    Join Date
    Dec 2010
    Posts
    4
    Rep Power
    0

    Default Working with many objects

    Hi im programming in java for 2 months and i have found one problem that i dont know how to resolve.
    I would like to work with many objects of the same type as a group. I tried adding them into a container but this solution didnt help me. Than i was thinking of using Java Message Service but i have never worked with that.
    Here is an example what i want to do:

    Java Code:
    ....
    
    String s1 = jTextField1.getText();
    String s2 = jTextField2.getText();
    String s3 = jTextField3.getText();
    String s4 = jTextField4.getText();
    .....
    Of course i have more text fields than 4, and if i had to do it this way the code would be really huge.
    Is there any solution for this? Or i can do it somehow using the containers? Is it possible send to these objects one message to call their methods?
    Thanks for responses :)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Reshi View Post
    Hi im programming in java for 2 months and i have found one problem that i dont know how to resolve.
    I would like to work with many objects of the same type as a group. I tried adding them into a container but this solution didnt help me. Than i was thinking of using Java Message Service but i have never worked with that.
    Here is an example what i want to do:

    Java Code:
    ....
    
    String s1 = jTextField1.getText();
    String s2 = jTextField2.getText();
    String s3 = jTextField3.getText();
    String s4 = jTextField4.getText();
    .....
    Of course i have more text fields than 4, and if i had to do it this way the code would be really huge.
    Is there any solution for this? Or i can do it somehow using the containers? Is it possible send to these objects one message to call their methods?
    Thanks for responses :)
    You can store those JTextFields in a List<JTextField> or even in a simple array if you know in advance how many JTextFields you need. Iterating over both a List or array is easy:

    Java Code:
    List<JTextField> list= new ArrayList<JTextField>();
    // or this:
    // JTextField[] list= new JTextField[ ... ];
    // ...
    for (JTextField field : list)
       // do something with field
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Reshi is offline Member
    Join Date
    Dec 2010
    Posts
    4
    Rep Power
    0

    Default

    Thanks so much :) Now i get it, i was thinking f more complicated solutioons and one simple array can help :)

Similar Threads

  1. Trouble working with/calling Objects! Any Help?
    By ramathews in forum New To Java
    Replies: 4
    Last Post: 03-24-2010, 02:50 PM
  2. Replies: 1
    Last Post: 01-22-2009, 04:25 PM
  3. Working with Vector objects + textfile
    By SGRocker in forum New To Java
    Replies: 5
    Last Post: 09-16-2008, 10:55 PM
  4. Replies: 8
    Last Post: 05-28-2008, 07:00 AM
  5. Help with Objects!
    By Shorinhio in forum New To Java
    Replies: 1
    Last Post: 07-10-2007, 09:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •