Ii am working on a module of java swing in which i want to return data into java swing textboxes and labels.
The text boxes and JLabel
should be varies dynamically according to data retrieve and I don't know about the properties name and their values...but i have to retrieve data from properties file without knowing the properties name and its value into Jlabel
and Jtextboxes
.
...and they should be varies according to data...like property name should comes into Jlabel
and its value should come into Jtextboxes
...
I used set interface of collection framework so i got all data from properties file into its key and values of key...but i don't know how to show it in Jlabel
And JTextBox
s
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 were 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 does 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 from 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 is :
property1 : value1
property2 : value2
property3 : value3
property4 : value4
i want to show output in Jlabel and Jtext field dynamically without know properies data ..so if properties increase JLabels and Textboxes also increse according to properties*/
Okay, so you have a Properties in key/value pairs, assuming that the key represents the label and the value the text, you can use propertyNames to get an Enumeration and iterate over the list ... that will allow you to create the 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();
for (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 you will probably want another Map which maps the keys/labels to the fields, assuming you want to save the values back again...
public class ConfigSwingDemo extends JFrame {
private Map<String, JTextField> fieldsMap = new HashMap<>(25);
//...
for (Object k : keys) {
JLabel label = new JLabel(k.toString());
JTextField field = new JTextField(configProps.getProperty(k.toString()), 10);
fieldsMap.put(k.toString(), field);
Then, we you want to save the values, you could use something like...
configProps.clear();
for (String key : fieldsMap.keySet()) {
JTextField field = fieldsMap.get(key);
configProps.setProperty(key, field.getText());
}
To copy the values back to the Properties
and save it.
Now, having said all that, I'd recommend using a JTable
, as you already have a basic model (key/value pairs in the Properties
), it would be a lot simpler
See How to Use Tables for more details