I am trying to create dynamic JButton and JLabel based on the number of URLs defined in the properties file.
What I have tried so far is:
AppResources.properties file
urls=http://google.com,http://stackoverflow.com,http://gmail.com
urlLabels=Internet Users,MPLS Users,Physical Access (Not Advised)
In my Java program I am reading the properties file and based on the comma separator splitting the string and now i need to generate the buttons and labels accordingly. Like the first URL Label --> first URL as Button and so on.
The way I have done this so far is:
String url = properties.getProperty("urls");
String urlLabel = properties.getProperty("urlLabels");
String[] jButton = url.split(",");
String[] jLabel = urlLabel.split(",");
for (int i = 0; i < jLabel.length; i++) {
JLabel labels = new JLabel(jLabel[i]);
panel.add(labels);
for (int j = 0; j < jButton.length; j++) {
JButton button = new JButton(jButton[j]);
panel.add(button);
}
}
But it prints the button three times for a label. How to fix this? Also how to write the action listeners for these buttons?
If you want to implement action listeners for your buttons, you can simply create and add a new ActionListener while creating the button.
Example :