So I am teaching myself Java by working on a project for work (I initiated the project on my own). I have a lot of the program working, thanks to the wonderful users of this site already.
My issue: I have Simple GUI with multiple check boxes that can be selected. The users will select one or more and then click the button that starts the code that will build the ini file needed for another program. So far, I can iterate through each check boxes and get the name of the ones checked. Basically, right now, I know the code that will be below is wrong in the fact that it is using if and else if statements, which simply overwrite each other.
Basically, I want the users to be able to select any check boxes on the GUI, and have this class check which ones are check, assign the actual network path value for each selection and append them together. From what I have read already, I am thinking I need to use a case statement to accomplish this action.
EDIT: Question is, how can I iterate through each selected check box, grab the text, convert the text string into the combined string of multiple selections
if (strRaw.equals("Site1")){
strRef = "source=NetworkPath1,";
}
else if (strRaw.equals("Site2")){
strRef = "source=NetworkPath2";
}
else if (strRaw.equal("Site3")){
strRef = "source=NetworkPath3";
}
Example Checkbox1 is checked
Checkbox2 is not checked
Checkbox3 is checked
Checkbox1.text = Something
Checkbox3.text = another thing
output string = Something, another thing
Any Help would be appreciated, like I said I am still learning Java.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
public class MyActionListener implements ActionListener {
private final List<JCheckBox> checkBoxes = new LinkedList<JCheckBox>();
public void addCheckBox(JCheckBox checkBox) {
this.checkBoxes.add(checkBox);
}
public void actionPerformed(ActionEvent evt) {
// Iterate over each JCheckBox
for (JCheckBox checkBox : checkBoxes) {
if (checkBox.isSelected()) {
String strRaw = new String(checkBox.getText());
String strRef = new String();
try {
PrintWriter writer = new PrintWriter("C:\\Control\\test1.ini", "UTF-8");
writer.println("[TifSource]");
if (strRaw.equals("Site1")){
strRef = "source=NetworkPath1,";
}
else if (strRaw.equals("Site2")){
strRef = "source=NetworkPath2";
}
else if (strRaw.equal("Site3")){
strRef = "source=NetworkPath3";
}
writer.println(strRef + "\n");
writer.println("[Division]");
writer.println("division=divisionarea\n");
writer.println("[Area]");
writer.println("area=6\n");
writer.println("[TifIndex]");
writer.println("Updated=0");
writer.println("Version=81008");
writer.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JOptionPane.showMessageDialog(null, strRef);
};
}
}
}