Results 1 to 12 of 12
Thread: Renaming Buttons Problem
- 07-28-2011, 11:02 PM #1
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
-
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.
- 07-29-2011, 12:06 AM #3
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
-
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.
- 07-29-2011, 12:17 AM #5
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?
- 07-29-2011, 12:20 AM #6
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"?
-
- 07-29-2011, 12:31 AM #8
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.
-
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.
- 07-29-2011, 12:41 AM #10
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.
- 07-29-2011, 12:53 AM #12
Similar Threads
-
Renaming a file
By sunde887 in forum New To JavaReplies: 9Last Post: 06-14-2011, 08:23 AM -
File renaming help
By themulator in forum New To JavaReplies: 3Last Post: 04-01-2011, 05:24 AM -
need help with package renaming
By Madz in forum New To JavaReplies: 2Last Post: 11-25-2009, 09:39 AM -
Renaming a class
By mew in forum EclipseReplies: 2Last Post: 12-06-2007, 11:29 PM -
Renaming a class in Eclipse
By Java Tip in forum Java TipReplies: 0Last Post: 12-04-2007, 10:54 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks