How to make a String value to call a specific existing JButton variable name in java?

So, I know there is this:

int number = Integer.parseInt("5");
String numtxt = Integer.toString(12);
double number = Double.parseDouble("4.5");
String numbertxt = Double.toString(8.2);
String letter = Character.toString('B');
char letter = "stringText".charAt(0);

so on...

but that I do not know how to call the String value to call, there was a JButton variable name dynamically; is it possible?

Let's say I have 4 JButton called btn1, btn2, btn3 and btnFillNumber;

I create a line called buttonName;

package testing;

public class Testing extends javax.swing.JFrame {

String buttonName;
int num;

public Testing() {
    initComponents();
}

@SuppressWarnings("unchecked")
// Generated Code <<<-----

private void btnFillNumberActionPerformed(java.awt.event.ActionEvent evt) {                                              
    for(num = 1; num <= 3; num++){
        buttonName = "btn" + Integer.toString(num);
        JButton.parseJButton(buttonName).setText(num);
    }
}                                             

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    // Look and feel stteing code (optional) <<<-----

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Testing().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton btn1;
private javax.swing.JButton btn2;
private javax.swing.JButton btn3;
private javax.swing.JButton btnFillNumber;
// End of variables declaration                   
}

I know that there is no JButton.parseJButton (), I just do not want to make a complicated explanation, I just need a conversion from String to dynamically call the JButton name.

See this:

    for(num = 1; num <= 3; num++){
        buttonName = "btn" + Integer.toString(num);
        JButton.parseJButton(buttonName).setText(num);
    }

I want to make a loop using String with

  • fixed string value (btn) and
  • the number of increments after it (1, 2, 3 ...) and
  • use to call JButton.

I just can do it, but what if I have 25 or more? So the loop I wanted ...

btn1.setText("1");
btn2.setText("2");
btn3.setText("3");

, JButtons - .

:

Output example

:

enter image description here

P.S. JFrame NetBeans ( , JPanel, JButton .., , , , , . , ).

+4
2

:

private Map<String,JButton> buttonMap = new HashMap<String,JButton>();

:

buttonMap.add("btn1", btn1);
buttonMap.add("btn2", btn2);
buttonMap.add("btn3", btn3);
buttonMap.add("btn4", btn4);

Listener/Loop make:

String buttonName = "btn1"; //should be parameter or whatever
JButton button = buttonMap.get(buttonName);

JButton:

JButton[] buttons = new JButton[4];

button[0] = new JButton(); //btn1
button[1] = new JButton(); //btn2
button[2] = new JButton(); //btn3
button[3] = new JButton(); //btn4

JButton but = button[Integer.parseInt(buttonString)-1];

, ( JComponent)

getContentPane().putClientProperty("btn1", btn1);

whith

JButton but = (JButton)getContentPane().getClientProperty("btn1");
+2

. Map<K,V> , , Hashtable<K,V>. , , ( btnFillNumber 0).

Hashtable<Integer,JButton> buttonTable = new Hashtable<>();

// Fill buttonTable with buttons

JButton button = buttonTable.get(num);

if (button != null) {
    // Do something with button
}

- autoboxing Integer -, num int.

-1

Source: https://habr.com/ru/post/1623085/


All Articles