How to show Data in JLabel and Jext Boxes java swing dynamically from properties file Without knowing data in properties file -
ii working on module of java swing in want return data java swing textboxes , labels.
the text boxes , jlabel should varies dynamically according data retrieve , don't know properties name , values...but have retrieve data properties file without knowing properties name , value jlabel , jtextboxes.
...and should varies according data...like property name should comes jlabel , value should come jtextboxes...
i used set interface of collection framework got data properties file key , values of key...but don't know how show in jlabel , jtextboxs
public class configswingdemo extends jframe { private file configfile = new file("momark.properties"); private properties configprops; private jbutton buttonsave = new jbutton("save"); static list<jlabel> listoflabels = new arraylist<jlabel>(); static list<jtextfield> listoftextfields = new arraylist<jtextfield>(); public configswingdemo() { super("properties configuration demo"); setlayout(new gridbaglayout()); gridbagconstraints constraints = new gridbagconstraints(); constraints.gridx = 0; constraints.gridy = 0; constraints.insets = new insets(10, 10, 5, 10); constraints.anchor = gridbagconstraints.west; constraints.gridy = 1; constraints.gridx = 0; constraints.gridwidth = 2; constraints.anchor = gridbagconstraints.center; add(buttonsave, constraints); buttonsave.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent arg0) { try { saveproperties(); joptionpane.showmessagedialog(configswingdemo.this, "properties saved successfully!"); } catch (ioexception ex) { joptionpane.showmessagedialog(configswingdemo.this, "error saving properties file: " + ex.getmessage()); } } }); setdefaultcloseoperation(jframe.exit_on_close); pack(); setlocationrelativeto(null); setvisible(true); try { loadproperties(); } catch (ioexception ex) { joptionpane.showmessagedialog(this, "the momark.properties file not exist, default properties loaded."); } set<object> keys = configprops.keyset(); for(object k:keys){ string key = (string)k; system.out.println(key+": "+configprops.getproperty(key)); } } ///////////////// private void loadproperties() throws ioexception { properties defaultprops = new properties(); // sets default properties configprops = new properties(defaultprops); // loads properties file inputstream inputstream = new fileinputstream(configfile); configprops.load(inputstream); inputstream.close(); } private void saveproperties() throws ioexception { //configprops.setproperty("server.url", texturl.gettext()); outputstream outputstream = new fileoutputstream(configfile); configprops.store(outputstream, "properties setttings"); outputstream.close(); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { @override public void run() { new configswingdemo(); } }); } } /*output console :
property1 : value1 property2 : value2 property3 : value3 property4 : value4 i want show output in jlabel , jtext field dynamically without know properies data ..so if properties increase jlabels , textboxes increse according properties*/
okay, have properties in key/value pairs, assuming key represents label , value text, can use propertynames enumeration , iterate on list ... allow create labels/fields.
gridbagconstraints constraints = new gridbagconstraints(); constraints.gridx = 0; constraints.gridy = 0; constraints.insets = new insets(10, 10, 5, 10); constraints.anchor = gridbagconstraints.west; set<object> keys = configprops.keyset(); (object k : keys) { jlabel label = new jlabel(k.tostring()); jtextfield field = new jtextfield(configprops.getproperty(k.tostring()), 10); constraints.gridx = 0; add(label, constraints); constraints.gridx++; add(field, constraints); constraints.gridy++; } constraints.gridx = 0; constraints.gridwidth = 2; constraints.anchor = gridbagconstraints.center; add(buttonsave, constraints); but want map maps keys/labels fields, assuming want save values again...
public class configswingdemo extends jframe { private map<string, jtextfield> fieldsmap = new hashmap<>(25); //... (object k : keys) { jlabel label = new jlabel(k.tostring()); jtextfield field = new jtextfield(configprops.getproperty(k.tostring()), 10); fieldsmap.put(k.tostring(), field); then, want save values, use like...
configprops.clear(); (string key : fieldsmap.keyset()) { jtextfield field = fieldsmap.get(key); configprops.setproperty(key, field.gettext()); } to copy values properties , save it.
now, having said that, i'd recommend using jtable, have basic model (key/value pairs in properties), lot simpler
see how use tables more details
Comments
Post a Comment