-1
public void createButton(int x, int y, String s) {
    try {
        JButton btn1 = new JButton();
        jPanel1.add(btn1);
        btn1.setLocation(x, y);
        btn1.setSize(50, 50);
        btn1.setVisible(true);
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println(e);
    }
}

That is the method which I use to create random buttons. But I can't give the button a name manually every time. I want name button object with what my String variable contains.

Laurel
  • 5,965
  • 14
  • 31
  • 57
Silver
  • 33
  • 5

2 Answers2

3

From your comment on another answer:

I don t want change button name.i want button variable name (in this case it is btn1) change to what string s brings.

Java is not designed that way, if you want to keep a reference for each JButton you can create an array:

JButton buttonsArray[] = new JButton[5]; //Or any amount of buttons

Then use them like this:

for (int i = 0; i < buttonsArray.length; i++) {
    buttonsArray[i] = createButton(text[i]);
}

Where text[i] is a String array where you have all the text for your JButtons

Or probably with an ArrayList of JButton:

ArrayList <JButton> buttonsList = new ArrayList <JButton>();

And then use it like:

for (int i = 0; i < text.length; i++) {
    JButton button = createButton(text[i]);
    buttonList.add(button);
}

Then you can select each button with:

buttonArray[i].setText("Hello"); //Or whatever method you want to call
buttonList.get(i).setText("Hello");
jPanel1.add(buttonsArray[i]);
jPanel1.add(buttonsList.get(i));

Now your createButton() method should look like this:

public JButton createButton(String s) {
    JButton btn1 = new JButton(s);
    //Add more code here
    return btn1;
}

In this question you can see an similar example using an array of JRadioButton


IMPORTANT NOTE

Finally I need to add this to my answer, don't set location /bounds of each JComponent manually instead use a proper Layout Manager that does the job for you and Empty Borders to create space between them if needed.

I bet you're using a null layout and while that might seem like the best and easiest way to create a GUI, the more GUIs you create, you're gonna get more errors due to this and more troubles trying to maintain it.

See: Null layout is evil and Why is it frowned upon to use a null layout in swing?.

I hope that helps

Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • Thank you a looooooot for your help my friend.that is exatelly what i need :) – Silver Dec 05 '16 at 17:38
  • @Silver I'm glad it helped, don't forget to [accept](http://stackoverflow.com/help/accepted-answer) the answer then :) – Frakcool Dec 05 '16 at 17:39
2
public void createButton(int x, int y, String s) {
    try {
        JButton btn1 = new JButton(s);
        jPanel1.add(btn1);
        btn1.setLocation(x, y);
        btn1.setSize(50, 50);
        btn1.setVisible(true);
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println(e);
    }
}

The constructor JButton(text) internally calls setText(text). So btn1.setText(text) later on would be the same. From the code:

/**
 * Creates a button with initial text and an icon.
 *
 * @param text  the text of the button
 * @param icon  the Icon image to display on the button
 */
public JButton(String text, Icon icon) {
    // Create the model
    setModel(new DefaultButtonModel());

    // initialize
    init(text, icon);
}

Keep in mind: setText() and setName() are not the same thing!

piegames
  • 975
  • 12
  • 31
  • I don t want change button name.i want button variable name (in this case it is btn1) change to what string s brings. – Silver Dec 05 '16 at 16:13