Question

I'm building a tool to update a txt file. In the JOptionPane, I want the file to update IF the Radio button "Add Charm" is selected and the JOptionPane.Ok_Option button is clicked. The code compiles fine, but when the program is ran, no changes occur in the txt file.

        import java.io.File;
        import java.io.BufferedReader;
        import java.io.BufferedWriter;
        import java.io.FileReader;
        import java.io.FileFilter;
        import java.io.FileWriter;
        import java.io.IOException;
        import java.io.PrintWriter;
        import java.awt.Color;
        import java.awt.BorderLayout;
        import java.awt.GridLayout;
        import java.awt.Container;
        import java.awt.event.ActionEvent;
        import java.awt.event.ActionListener;
        import javax.swing.SwingUtilities;
        import javax.swing.border.Border;
    import javax.swing.*;


    public class test extends JFrame implements ActionListener {


        public static void main (String[]args) {
            test gui=new test();
            }

        JPanel pnl=new JPanel(new BorderLayout());
        JButton addBtn;
        String path=null;
        Container contentPane=getContentPane();
        JPanel grid=new JPanel(new GridLayout(1,1));
        Border border1=BorderFactory.createLineBorder(Color.BLACK,5);
        Border border2=BorderFactory.createLineBorder(Color.GRAY,5);
        ImageIcon add=new ImageIcon("add.jpg");
        String username=(System.getProperty("user.name"));
        JRadioButton radd;
        JRadioButton rrem;
        JTextField charmnum;
        JTextField charmn;
        JTextField charmdd;

        public test() {
            super("TEST");
            setSize(200,200);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            add(grid);
            setVisible(true);
            setAlwaysOnTop(false);
            setResizable(false);
            addBtn=new JButton("ADD",add);
            addBtn.addActionListener(this);
            grid.add(addBtn);
            grid.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
            contentPane.add("North",grid);
            grid.revalidate();
            }

        public void actionPerformed(ActionEvent event) {

            if(event.getSource()==addBtn) {
                try {
                    JPanel acpnl = new JPanel(new GridLayout(5,1));
                    /*JRadioButton */radd=new JRadioButton("Add Charm");
                    /*JRadioButton */rrem=new JRadioButton("Remove Charm",false);
                    //rrem.setEnabled(false);
                    /*JTextField */charmnum = new JTextField(12);
                    /*JTextField */charmn = new JTextField(12);
                    /*JTextField */charmdd = new JTextField(12);
                    ButtonGroup radios=new ButtonGroup();
                    charmnum.setEditable(false);
                    charmn.setEditable(false);
                    charmdd.setEditable(false);
                    radd.addActionListener(this);
                    rrem.addActionListener(this);
                    radios.add(radd);
                    radios.add(rrem);
                    add(acpnl);
                    acpnl.add(radd);
                    acpnl.add(rrem);
                    acpnl.add(Box.createHorizontalStrut(15)); // a spacer
                    acpnl.add(new JLabel("Charm #:"));
                    acpnl.add(charmnum);
                    acpnl.add(Box.createHorizontalStrut(15)); // a spacer
                    acpnl.add(new JLabel("Charm Name:"));
                    acpnl.add(charmn);
                    acpnl.add(Box.createHorizontalStrut(15)); // a spacer
                    acpnl.add(new JLabel("Charm DueDate:"));
                    acpnl.add(charmdd);
                    boolean test=radd.isSelected();

                    int result = JOptionPane.showConfirmDialog(null,acpnl,"Fill in the Charm Information",JOptionPane.OK_CANCEL_OPTION);
                    if (result==JOptionPane.OK_OPTION)while(test) {

                        String strfile, midfile, finalfile;
                        File batchfile=new File("test.txt");
                        BufferedReader br=new BufferedReader(new FileReader(batchfile));
                        File newfile=new File("temp.txt");
                        newfile.createNewFile();
                        PrintWriter pw=new PrintWriter(new BufferedWriter(new FileWriter(newfile)));
                        while ((strfile = br.readLine()) !=null) {
                            String[]fileinput=new String[2];
                            String A=(charmnum.getText());
                            fileinput[0]=(A);
                            String B=(charmn.getText());
                            fileinput[1]=(B);

                            String PART1NEW=
                                "If exist \"C:%desktop%\\"+fileinput[0]+" Testing\" GOTO copy"+fileinput[0]+"   >> %log%.txt\r\n"+
                                "set charm#="+fileinput[0]+"    >> %log%.txt\r\n"+
                                "GOTO errormissing1 >> %log%.txt\r\n"+
                                ":"+fileinput[0]+"check >> %log%.txt\r\n"+
                                "::p1end";
                            /*String PART1REM=
                                "If exist \"C:%desktop%\\"+fileinput[0]+" Testing\" GOTO copy"+fileinput[0]+"   >> %log%.txt\r\n"+
                                "set charm#="+fileinput[0]+"    >> %log%.txt\r\n"+
                                "GOTO errormissing1 >> %log%.txt\r\n"+
                                ":"+fileinput[0]+"check >> %log%.txt\r\n"; */
                            String PART2NEW=
                                ":copy"+fileinput[0]+"  >> %log%.txt\r\n"+
                                "echo.  >> %log%.txt\r\n"+
                                "echo "+fileinput[0]+"  >> %log%.txt\r\n"+
                                "xcopy \"C:%desktop%\\"+fileinput[0]+" Testing\" \"X:%mycharmsopen%\\"+fileinput[0]+" - "+fileinput[1]+"\\\" /D /E /C /R /I /K /Y   >> %log%.txt\r\n"+
                                "GOTO "+fileinput[0]+"check >> %log%.txt\r\n"+
                                "::p2end";
                            /*String PART2REM=
                                ":copy"+fileinput[0]+"  >> %log%.txt\r\n"+
                                "echo.  >> %log%.txt\r\n"+
                                "echo "+fileinput[0]+"  >> %log%.txt\r\n"+
                                "xcopy \"C:%desktop%\\"+fileinput[0]+" Testing\" \"X:%mycharmsopen%\\"+fileinput[0]+" - "+fileinput[1]+"\\\" /D /E /C /R /I /K /Y   >> %log%.txt\r\n"+
                                "GOTO "+fileinput[0]+"check >> %log%.txt\r\n"; */

                            String PART1END="::p1end";
                            String PART2END="::p2end";
                            midfile=(strfile.replace(PART1END,PART1NEW));
                            finalfile=(midfile.replace(PART2END,PART2NEW));
                            if (finalfile != null) {
                                    pw.println(finalfile);
                                }
                            }
                        pw.close();
                        br.close();
                        batchfile.delete();
                        newfile.renameTo(batchfile);
                        }
                    }


                catch (Exception e6) {
                    JOptionPane.showMessageDialog(this,"Error has occurred in \"addBtn\" actionEvent.","Error",JOptionPane.ERROR_MESSAGE);
                    e6.printStackTrace();
                    }
                }
            if (radd.isSelected()) {
                charmnum.setEditable(true);
                charmn.setEditable(true);
                charmdd.setEditable(false);
                }
            if (rrem.isSelected()) {
                charmnum.setEditable(true);
                charmn.setEditable(false);
                charmdd.setEditable(false);
                }
            }
        }

The txt file is located in the same dir as the program, and it contains the following (before updated):

        null
        null
        null
        null
        ::p1end
        null
        null
        null
        null
        ::p2end
        null
        null
        null
        null

I've narrowed down the issue at line 92:

        if (result==JOptionPane.OK_OPTION)while(test) {

When while(test) is commented out, the program successfully makes the changes to the txt file, but without consideration of which radio button is selected. I know I can create another Jbutton in the main JFrame to replace having 2x radio buttons in the JOptionPane, but I'm really trying to make this setup work.

What is there something to replace while(test) to make this program work as desired?

(I apologize for the poorly nested code and class organization in advance.)

Was it helpful?

Solution

Swap the lines:

int result = JOptionPane.showConfirmDialog(null,acpnl,"Fill in the Charm Information",JOptionPane.OK_CANCEL_OPTION);
boolean test=radd.isSelected();

The way you currently have it, you store in test whether radd is selected before its parent panel is displayed to the user, so it will always be false.

Also, use if (result == JOptionPane.OK_OPTION && test).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top