开发者

Is there a way to convert a String to JTextField type?

开发者 https://www.devze.com 2023-02-13 12:57 出处:网络
How do i convert a string da开发者_开发百科ta 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 conten

How do i convert a string da开发者_开发百科ta 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);  
    }  
}


You can create a new textfield or use an existing one, and simply set its value to the string.


JTextField newTextField = new JTextField(stringVariable);


jTextField.setText(stringValue);

Here is an example.

EDIT:

instead of doing stuff like

text1 = (JTextField) metricss; 

try

text1 = new JTextField();
text1.setText(metricss);

And be sure to add text1 to a container (like JPanel or JFrame)


I have solved the problem by adding list array of type JTextField and then adding the text boxes to the list. then using loop i retrieved the list and made it work!! viola!!! Here is the code block.

List<JTextField> list = new ArrayList<JTextField>();
list.add(value1);
list.add(value2);
list.add(value3);
list.add(value4);
list.add(value5);
list.add(value6);
list.add(metrics1);
list.add(metrics2);
list.add(metrics3);
list.add(metrics4);
list.add(metrics5);
list.add(metrics6);
list.add(metrics2_1);
list.add(metrics2_2);
list.add(metrics2_3);
list.add(metrics2_4);
list.add(metrics2_5);
list.add(metrics2_6);

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 = 0;
while (rs.next()) {
JTextField text1 = list.get(i+6);
JTextField text2 = list.get(i);

            JTextField text3= list.get(i+12);

            text1.setText(rs.getString("pos_metrics1"));

            text2.setText(rs.getString("pos_value"));
             text3.setText(rs.getString("pos_metrics2"));
            i++;
        }
    } catch (SQLException ex) {
        Logger.getLogger(unit_builder.class.getName()).log(Level.SEVERE, null, ex);
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号