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 what I don't know how to make String value to call existed JButton variable name dynamically; is it even possible?
Let's say, I have 4 JButton called btn1, btn2, btn3 and btnFillNumber;
I create a String 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 there's no JButton.parseJButton(), I just don't want to make complicated explaination, I simple want a convertion from String to call JButton's variable name dynamically.
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 a String with
- a fixed String value (btn) and
- increment number after it (1, 2, 3...) and
- make use to call a JButton.
I can just simply do this, but what if I got a 25 or more? So a loop what I wanted...
btn1.setText("1");
btn2.setText("2");
btn3.setText("3");
Note that the value of these JButtons are not necessarily incremental in some purpose.
Output:
My real development:
P.S. I use to make JFrame in NetBeans Design (just click and drag the objects in palette window like JPanel, JButton, etc., so I don't type the code manually except making my own logical Method; and I can't edit the code in grey background in Source View that was made automatically by Design View but in Design View itself. If you have tips and guide, I'll be happy to).
Use a Map:
private Map<String,JButton> buttonMap = new HashMap<String,JButton>();
In your constructor add your buttons:
buttonMap.add("btn1", btn1);
buttonMap.add("btn2", btn2);
buttonMap.add("btn3", btn3);
buttonMap.add("btn4", btn4);
And in your action Listener / Loop whatever make:
String buttonName = "btn1"; //should be parameter or whatever
JButton button = buttonMap.get(buttonName);
As an alternative you could just as well set up an array of 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
And access it
JButton but = button[Integer.parseInt(buttonString)-1];
Or by utilizing the possibility of adding custom properties to UI elements (you'll need a JComponent for that)
getContentPane().putClientProperty("btn1", btn1);
and later retrieving whith
JButton but = (JButton)getContentPane().getClientProperty("btn1");
I agree with Kevin's comment. The best concrete Map<K,V>
class is probably Hashtable<K,V>
. You don't even need to create the button name, just associate an integer with it if they are all numbered (and if btnFillNumber
can be 0
).
Hashtable<Integer,JButton> buttonTable = new Hashtable<>();
// Fill buttonTable with buttons
JButton button = buttonTable.get(num);
if (button != null) {
// Do something with button
}
Because of autoboxing, you don't need to create Integer
objects to query the hashtable, num
can be an int
primitive.
来源:https://stackoverflow.com/questions/34633414/how-to-make-string-value-to-call-specific-existed-jbutton-variable-name-in-java