0

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..

Mike
  • 19,267
  • 11
  • 56
  • 72
Deepak
  • 6,684
  • 18
  • 69
  • 121
  • Not totally clear here... are you trying to rename the JTextField variables? What is 'text1 = (JTextField) metricss;' intended to do? – Owen Feb 25 '11 at 00:53
  • actually Object metricss = "metrics1" + i; in this line i am constructing the textbox name.. metrics11 wil be the name for first loop so then i want to put the value from database field into this 'metrics11' textbox.. i have a textbox with metrics11 already.. – Deepak Feb 25 '11 at 01:01

5 Answers5

2

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);  
    }  
}
MattLBeck
  • 5,701
  • 7
  • 40
  • 56
  • no this is not i want. i have 12 textboxes already in a frame. i want to put contents into them. for that i need their textbox name rite ? i dont want to change name. i just want to get the name thats it.. and put the value in it.. the loop i am doing is to put the value in different textboxes.. got it ? – Deepak Feb 25 '11 at 01:14
  • 1
    I think I understand now. In which case you should either stick your JTextFields in three different arrays (`metricss[]`, `metricss2[]`, `values[]`) and loop over these arrays (e.g. `values[i]`) to get the correct instance. Or, to achieve this in exactly the way you are trying, I think `Class.forName()` would work. I would much prefer the 1st option though. – MattLBeck Feb 25 '11 at 01:23
  • is it the workign code ? i think its not working...not compiling also.. i will try the other way though... – Deepak Feb 25 '11 at 01:36
  • It might help if you show where you first declare those 12 text fields? – MattLBeck Feb 25 '11 at 01:40
  • I checked my code with an IDE. It turns out I'm using `Class.forName()` incorrectly and it's a bit more complicated than what I first thought. The array method is the standard way of doing this sort of thing, however. – MattLBeck Feb 25 '11 at 01:47
1

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

rayman86
  • 1,385
  • 10
  • 9
  • Yup that be the one. Just didn't write the code down off the top of my head been a while since I coded in java. – rayman86 Feb 25 '11 at 01:47
1
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)

Bala R
  • 107,317
  • 23
  • 199
  • 210
  • but this wil create a new text box rite ? i dont want that.. i have created the textboxes already. all i want is just to get its name.. look at my edit.. – Deepak Feb 25 '11 at 00:50
  • No, it won't create a new one. jTextField is assumed to be a local field. I think this is what you want. – Mike Feb 25 '11 at 00:53
  • see i have 12 text boxes and i have to assign values to that 12 text boxes from the database. For doing that i need to uset setText() as you said. but from that 12 text boxes how will it put things in place when i use jTextField.setText(). Which textbox will be treated as local among tat 12 ?? – Deepak Feb 25 '11 at 00:58
1
JTextField newTextField = new JTextField(stringVariable);
Owen
  • 1,541
  • 1
  • 14
  • 12
1

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);
    }
Deepak
  • 6,684
  • 18
  • 69
  • 121