0
votes

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);
        };
    }
  }
}
2
What exactly isn't working? You haven't really asked a question. - Paul Samsotha
Tried fixing the question, sorry I am doing these posts while at work, fixing clients computer issues... - Ragnoth
What doesn't work? Code looks fine, given you want to print all those things line by line. - Paul Samsotha

2 Answers

0
votes

You can do something like this. Use a StringBuilder and append to it each iteration

StringBuilder sb = new StringBuilder();

for (JCheckBox checkBox : checkBoxes) {
  if (checkBox.isSelected()) {

      String strRaw = new String(checkBox.getText());
      String strRef = new String();

      sb.append("[TifSource]");
      if (strRaw.equals("Site1")){
          sb.append("source=NetworkPath1, ");
      }
      else if (strRaw.equals("Site2")){
          sb.append("source=NetworkPath2, ");
      }
      else if (strRaw.equal("Site3")){
         sb.append("source=NetworkPath3, ");
      }
      sb.append(strRef + "\n");
      sb.append("[Division]");
     sb.append("division=divisionarea\n");
      sb.append("[Area]");
      sb.append("area=6\n");
      sb.append("[TifIndex]");
      sb.append("Updated=0");
      sb.append("Version=81008");
  }
}

try {

   PrintWriter writer = new PrintWriter("C:\\Control\\test1.ini", "UTF-8");
   writer.println(sb.toString());

} catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
   e.printStackTrace();
} catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
   e.printStackTrace();
}
0
votes

Sortof confused on what you want. You can create a String, and append to it, and then write that to the file? Would that work for you? Again, I'm sort of confused what you need.

String x;
x += "\nSome Text"; // \n is a new line
x += "Other stuff";

writer.println(x);

Would this work for you?