I am stuck at a problem and I have been googling around and read quite few resources and feel direction right now.
HERE is my problem. I am working on an application which will act like CRUD application for a MySQL database with variable tables and structures, I cannot have fixed no. of tables and structures.
I am directionless at creating form for variable table structure. What i want to achieve is when a user selects the table name from combobox relevant no. of fields should be created after retrieving the table structure of that particular table.
Achievement so far: -I have been able fetch structure/schema, data types, data of the table. -Can write queries to create, update, delete the same.
can't succeed in: -I want to create JTextField with unique object names of JTextField relevant to associated column name in the table. -Column names are String type how can I covert them to Object of JTextField.
For example
String f1 = "fieldname_1"; // In reality these string values will be fetched from database resultset containing column names as identifier.
String f2 = "fieldname_2"; // In reality these string values will be fetched from database resultset containing column names as identifier.
JTextField f1 = new JTextField ();
or
String[] f = {"fieldname_1", "fieldname_2"};
JTextField f[0] = new JTextField ();
but it gives error such as f1 is already defined
Note : I want to have input field names to be unique so that i will be easier to access there data after input.
Corrections to the question are welcome, If my approach to handle the problem is wrong give a brief idea/approach to pursue it in right direction.
Regards
You can't create unique variable names based on the column name.
Instead you could create a HashMap
to map the column name to the text field used in the form:
HashMap<String, JTextField> textFields = new HashMap<String, JTextField>();
Then you have a method that adds the text field to the form for each column name. Something like:
provide void addTextField(JPanel form, String columnName)
{
Jlabel label = new JLabel( columnName );
form.add( label );
JTextField textField = new JTextField(10);
form.add( textField );
textFields.put(columnName, textField);
}
Of course you would need to modify for the proper layout you want to use.
Then when you want to access the text field for the given column you can use:
JTextField textField = textFields.get(columnName);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.