while loop: keep all values


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: while loop: keep all values

  1. #1
    Join Date
    Apr 2005
    Posts
    10

    while loop: keep all values

    Hi. I use java swing to make a GUI. I have a while loop that run as long as there is something to read, and creates a JButton in each loop, pluss it incrases a value that will be sent to a file when the user press the button.

    while(file is not empty)
    {
    create button;
    add actionlistener
    button_id=i++;
    }
    let's say there now is 3 buttons on the panel

    then..

    if (source = button)
    {
    send value button_id to another file
    }

    but only the last value is remembered, and only the last button created, is remembered, so how can i remember them all, and not only the last one? I donno if i explained propperly, but plz ask if something aint clear (i'm not english, so..:/)

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    Well it's hard to tell exactly how you are creating the buttons from your post because you wrote it in pseudo code. If you want to keep references to the buttons you'll have to store them in an array or arraylist etc. Anything that you declare in a while loop will go out of scope once the current itteration of the while loop is finished, so you'll lose your reference to it if you don't store it somewhere.

    When you say only the last value is remembered, i'm not sure which value you mean. Do you mean the button_id value? If so, there is only one button_id variable so it can only ever have one value.

  3. #3
    Join Date
    Apr 2005
    Posts
    10
    Here is how i create the buttons: (all buttons are created ok)

    String filename = "myFile.txt";
    String s;
    int i = 0;

    try
    {
    BufferedReader buf = new BufferedReader(new FileReader(filename));
    String read = null;

    while ((s= buf.readLine()) != null)
    {
    myImg= new ImageIcon(s);
    myButton = new JButton(myImg);

    sPanel.add(myButton);
    myButton.addActionListener(this);

    buttonID = i++;
    }
    buf.close();
    } catch (IOException ex) {
    System.err.println ("Error reading from file" + ex);
    }

    Then i have a action listener that listens to the buttons. I'm not sure how it should look, since i dont know how many buttons there will be in advace. could be 2 could be 20.. The value buttonID has to be sent to the file when the button is clicked on... I'm not sure how to connect the id to the particular button.

    So.. any ideas on how to make these buttons work?

  4. #4
    Join Date
    Feb 2004
    Posts
    541
    Buttons have what is known as an action command, which is a string that is stored in the button. You can set this string using the method setActionCommand("someString"). When a button is pressed this String will be sent in the ActionEvent, and in your ActionPerformed method you can retrieve this string from the Event.

    If you want to associate a particular ID with a particular button you can add it's ID as its action command, and when the button is pressed you'll know which button it was when you retrieve the string from the event.

    You still won't have a reference to the button though, as your reference will have gone out of scope when the while loop itteration finished. If you need a reference to the button when the button is clicked you'll have to store the buttons somehow. You could use an ArrayList, and store the buttons in the location that matches the ID you give them. You could also use a HashTable, and use a String representation of the ID as a key.

  5. #5
    Join Date
    Apr 2005
    Posts
    10
    thanks man, thats a realy good ida. But the JButton, that has to be stored in the arraylist, is an object, and I can't store objects in a arraylist.. or..? if not, is it possible to convert it into a string, store it, an then convert it back when needed?
    ..
    try
    {
    BufferedReader buf= new BufferedReader(new FileReader(filename));
    String s = null;

    while ((s= buf.readLine()) != null)
    {
    myImg = new ImageIcon(s);
    myButton = new JButton(myImg);

    arrayList.add(myButton); //this is not accepted -> program crash

    }

    for(Iterator iter = arrayList.iterator(); iter.hasNext()
    {
    String erg = (String)iter.next();
    thePanel.add(pillButton);
    myButton.setActionCommand(erg);
    myButton.addActionListener(this);

    }
    ..
    }

    //event handler
    public void actionPerformed(ActionEvent event)
    {
    Object source = event.getSource();

    if (source == myButton)
    {
    //do blah
    }


    As u can see i havent figured out how to send over the "erg" value, but i'll figure it out when the object problem is gone (hopefully)

  6. #6
    Join Date
    Apr 2005
    Posts
    10
    Nope, the problem is not that i cant store the buttons in an arraylist, cuz i can. but i get a classcastexeption on the String erg = (String)iter.next();??

  7. #7
    Join Date
    Apr 2005
    Posts
    10
    okey, changed String to Object. Works. but how to get the value of the button to the action handler? I know its Saturday, so am I talking to my self here? heeeeelp. plllllllease

  8. #8
    Join Date
    Feb 2004
    Posts
    541
    To get the value of the button to the event handler you need to set the action command of each button as I mentioned before. The action command will be passed to the ActionEvent object, so when it reaches your event handler you can extract the action command and know which button was pressed.

    And about ArrayList...everything you put in the ArrayList will be stored as type Object. When you extract it from the ArrayList you have to cast it back into its original type.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center