How do i convert a string data type to JTextField type. I have set of text boxes in a window and i want to fill it from database using a loop. I am constructing a string with the content same as text box name and now i want to change the data type to JTextField.
---EDIT----
private void formWindowOpened(java.awt.event.WindowEvent evt) {
try {
int p_id = made_up.p_id;
String sql = "SELECT * FROM pos_metrics WHERE p_id='" + p_id + "'";
ResultSet rs = mysql_query.execute_mysql(variables.con.conn, sql);
int i = 1;
while (rs.next()) {
Object metricss = "metrics1" + i;
Object metricss2 = "metrics2_" + i;
Object values = "value" + i;
JTextField text1;
JTextField text2;
JTextField text3;
text1 = (JTextField) metricss;
text2 = (JTextField) metricss2;
text3 = (JTextField) values;
text1.setText(rs.getString("pos_metrics1"));
text2.setText(rs.getString("pos_metrics2"));
text3.setText(rs.getString("pos_value"));
i++;
}
} catch (SQLException ex) {
Logger.getLogger(unit_builder.class.getName()).log(Level.SEVERE, null, ex);
}
}
This is my code and i am getting error on the line where i change the type..
I think this is what you are trying to achieve:
private void formWindowOpened(java.awt.event.WindowEvent evt) {
try {
int p_id = made_up.p_id;
String sql = "SELECT * FROM pos_metrics WHERE p_id='" + p_id + "'";
ResultSet rs = mysql_query.execute_mysql(variables.con.conn, sql);
int i = 1;
while (rs.next()) {
String metricss = "metrics1" + i;
String metricss2 = "metrics2_" + i;
String values = "value" + i;
JTextField text1;
JTextField text2;
JTextField text3;
text1.setName(metricss);
text2.setName(metricss2);
text3.setName(values);
text1.setText(rs.getString("pos_metrics1"));
text2.setText(rs.getString("pos_metrics2"));
text3.setText(rs.getString("pos_value"));
i++;
}
} catch (SQLException ex) {
Logger.getLogger(unit_builder.class.getName()).log(Level.SEVERE, null, ex);
}
}
setName()
sets the name of the JTextField
, which will be the current local instance. I don't know if it has a visible effect on the text field though. You might have to use a border.
--edit--
In light of your comment, this is an example of option 2:
--edit 2--
This code does not compile. Class.forName() is being used for the wrong purpose...
private void formWindowOpened(java.awt.event.WindowEvent evt) {
try {
int p_id = made_up.p_id;
String sql = "SELECT * FROM pos_metrics WHERE p_id='" + p_id + "'";
ResultSet rs = mysql_query.execute_mysql(variables.con.conn, sql);
int i = 1;
while (rs.next()) {
String metricss = "metrics1" + i;
String metricss2 = "metrics2_" + i;
String values = "value" + i;
JTextField text1 = Class.forName(metricss);
JTextField text2 = Class.forName(metricss2);
JTextField text3 = Class.forName(values);
text1.setText(rs.getString("pos_metrics1"));
text2.setText(rs.getString("pos_metrics2"));
text3.setText(rs.getString("pos_value"));
i++;
}
} catch (SQLException ex) {
Logger.getLogger(unit_builder.class.getName()).log(Level.SEVERE, null, ex);
}
}