Results 1 to 12 of 12
  1. #1
    Huskies's Avatar
    Huskies is offline Member
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Thumbs down Renaming Buttons Problem

    Hey guys, i am having trouble figuring this one out. I am creating an application, and inside a window there is a text field. In here the user inputs the number of button they want, and then hits another button, and a second window with the indicated number of button. This part i can do. I am using a for statement, but i want to later change the text on the button. Here is some code (Im actually not at my regular computer so i dont have the exact code) from the project.


    int x = 5 //variable i made up that will later be the # user inputs

    for(int i = 0; i < x ;i++ ){

    button = new JButon();
    frame.getContentPane().add(BorderLayout.WEST, button);

    }

    I know that isn't complete code, but i know how to make the buttons. That's not my problem. Now i want to change the Text displayed on only the second button. How do i set the variable name of the second button to button2, instead of just button. Ideally i would like to have a separate variable name for all the buttons, that way i can individually change only one button's text. This may not be possible.

    Sorry that was really long winded, and i probably didn't even get my point across. Thanks for the help!

    Huskies

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Speaking for myself, I'd like to see your actual compilable code, not something that is sort-of kind-of the code you think you might be using. There are several significant problems with the code you've posted but we have no idea if it is anything like your current code, and I don't think you or I want to waste time discussing non-issues.

  3. #3
    Huskies's Avatar
    Huskies is offline Member
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default

    Here is a test i quickly typed up. This just makes a frame with a panel, and populates it with a certain number of buttons. the number will eventually be determined by the User.

    Java Code:
    package testpopulate;
    import javax.swing.*;
    import java.awt.*;
    
    public class TestPopulate {
        
        public static void main(String[] args) {
            JFrame frame1 = new JFrame("This is a test window");
            JPanel panel1 = new JPanel();
            frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame1.setSize(500,500);
            frame1.setVisible(true);
            frame1.getContentPane().add(BorderLayout.WEST, panel1);
            
            panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
            
            int userInput = 5; //eventually will be some int from user
            JButton button;
            for(int i = 0; i < userInput; i++ ){
                button = new JButton("Unchanged Text");
                panel1.add(button);
                
            }
         
        }
    }

    Now i want to know how i can change the text of just the second button.
    Last edited by Fubarable; 07-29-2011 at 12:08 AM. Reason: code tags added

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Huskies View Post
    Now i want to know how i can change the text of just the second button.
    OK, this we can help with. The question I have for you is when will you decide that you want to change the second button's text? On button creation (in the for loop above)? Or after the buttons have been created and displayed? The answer will of course depend on this.

    If the former, and you know that you want to change the 2nd button's text on creation, then it's a trivial thing to put an if block inside the for loop and test if i is the right number, then change the button's text.

    If the latter, then you'll have to decide what will trigger the behavior. If you will change the text of a button that's been pressed, then you can add an ActionListener to all the buttons as you create them and have that ActionListener act on the pressed button (the Object returned by the ActionEvent's getSource() method). If there's a different trigger, then you'll likely want to put your buttons into a JButton array and iterate through that array on whatever event you choose, changing the appropriate button again with an if block within the for loop that is iterating through the array.

  5. #5
    Huskies's Avatar
    Huskies is offline Member
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default

    but how do i tell the difference between the first button with the variable name "button" and the last button with the same variable name?

  6. #6
    Huskies's Avatar
    Huskies is offline Member
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default

    eventually there will be TextFields next to them, and when the button is pushed, it will save Whatever is in the TextField to some String, and converted to int for Calculations. I have the same problem with the TextFields. How can i say, "save the text to some String, but from the 2nd TextField, not just the last"?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Huskies View Post
    but how do i tell the difference between the first button with the variable name "button" and the last button with the same variable name?
    Again it depends on when you will change the text. You haven't answered my questions yet above, and as I mentioned above I can't really answer this without that information.

  8. #8
    Huskies's Avatar
    Huskies is offline Member
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default

    sorry, they will be changed after its creation. The button will be created, and then when another trigger happens i want the text in the second button to change.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Then there can be several ways of going about this. Probably the easiest would be to declare an array of JButton and to fill it with your displayed JButtons. Then when the trigger occurs, loop through your button array, and when i, the loop index == 1, you've got your second button and you can change its text.

    It can get more complicated if you want to link the JButton with a JTextField and there are a number of ways to solve this including using a JTable with buttons and fields, or using parallel arrays (two separate arrays) of JButtons and JTextFields, or creating a class that holds one JButton and one JTextField and creating an array of that, or using a Map<JButton, JTextField> and use this to get the field associated with the pressed button. Each has its pluses and minuses, and I've used each one at least once before.

  10. #10
    Huskies's Avatar
    Huskies is offline Member
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default

    ok, thanks! so it is most simple to use an array. Finally i have one more question. Is it possible to have the variable name increase with each time. I know this code is wrong, but hopefully you can understand what i want to do. Is there a way to do this?

    Java Code:
    int number = 0
    for(int i = 0; i < 10, i++){
         String button+number = "hello";
         number++
    }

    ideally I would have multiple strings with the names button1, button2, button3 and so on. did that make sense?

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Huskies View Post
    ok, thanks! so it is most simple to use an array. Finally i have one more question. Is it possible to have the variable name increase with each time. I know this code is wrong, but hopefully you can understand what i want to do. Is there a way to do this?

    Java Code:
    int number = 0
    for(int i = 0; i < 10, i++){
         String button+number = "hello";
         number++
    }

    ideally I would have multiple strings with the names button1, button2, button3 and so on. did that make sense?
    I'm not sure if you're coming from another programming language such as PHP, but one thing you need to know about Java is that variable names are not as important as you think, and are many times not the best way to get a handle on an object reference. For another what you are trying to do above is not possible nor is it necessary. If you have a collection of objects that you want to get a reference to based on a number as you describe above, you should use an array, not different variable names. If you have a collection of objects and you want to get a reference to each based on a String, then you would use a Map, such as a HashMap<String, JButton>. If you want to get a reference to an object based on its position in a queue, then you'd use a LinkedList.

  12. #12
    Huskies's Avatar
    Huskies is offline Member
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default

    ok, cool. That answers my question totally. Thanks for all your help!

Similar Threads

  1. Renaming a file
    By sunde887 in forum New To Java
    Replies: 9
    Last Post: 06-14-2011, 08:23 AM
  2. File renaming help
    By themulator in forum New To Java
    Replies: 3
    Last Post: 04-01-2011, 05:24 AM
  3. need help with package renaming
    By Madz in forum New To Java
    Replies: 2
    Last Post: 11-25-2009, 09:39 AM
  4. Renaming a class
    By mew in forum Eclipse
    Replies: 2
    Last Post: 12-06-2007, 11:29 PM
  5. Renaming a class in Eclipse
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-04-2007, 10:54 AM

Posting Permissions

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