jesric1029
jesric1029

Reputation: 718

Adding entire GUI to JFrame after the fact

Today I have another project that involves the program I have written for work and been working on for well over 6 months. There is a lot of code and classes so I'm going to try to explain the best that I can so you can (hopefully) help me.

Currently my program works by reading a file and allowing a user to make some modifications to that file, then a new file is written. This entire process involves a GUI that is better explained as a series of JOptionPanes, some with imbedded panels.

Here is my goal: Each file uploaded comes with a given number of "batches". My program loops through once for each batch. During a loop each of the relevant JOptionPane GUI's is displayed. When all batches are read the program ends and the file is complete.

I have been asked to add a feature where the entire "project" is inside of a JFrame with a new "upload" button. This would allow the user to run the program multiple times without having to open the JAR over and over again. If they select "Upload" they essentially start the program over.

Here is my main class:

package nachamultifive;

import java.util.ArrayList;

import javax.swing.JFileChooser;

import nachamultifive.Buffered_Reader_Writer.BatchCounter;
import nachamultifive.Buffered_Reader_Writer.FileValidation;
import nachamultifive.Buffered_Reader_Writer.MainWriter;
import nachamultifive.GUIs.FileHandling;
import nachamultifive.GUIs.ReturnBuilderGUI;

public class NachaMain 
{

    public static JFileChooser saveFile;//The output file save location.
    public static JFileChooser uploadFile;//The uploaded NACHA file. 

    public static int batchTotal;//The total number of batches in the file. 
    public static ArrayList<String> batchHeaders;//An array of all batch headers.
    public static int batchCounter;//The counter that displays the current batch number in sequence.

    public static String location;

    public static void main(String args[]){

        FileHandling fHandling = new FileHandling();//The class that handles upload/save of files. 

        fHandling.getFile();//Allows the user to upload a file.
        fHandling.setDirectory();//Allows the user to choose the save location. 
        saveFile=fHandling.saveFile;//Sets the file save location as static with the main class.
        uploadFile=fHandling.uploadFile;//Sets the uploaded file as static with the main class.

        BatchCounter bCounter = new BatchCounter();//The class that handles counting the batches. 

        bCounter.getBatches();//Counts the total number of batches.
        batchTotal=BatchCounter.BatchTotal;//Sets the total number of batches as static with the main class.
        batchHeaders=bCounter.batchHeaders;//Sets the batch header array as static with the main class. 

        MainWriter mWriter = new MainWriter();//The class that handles all writing functions for the new file. 

        mWriter.writeNacha();//Writes the output file. 

        location = MainWriter.location;
        System.out.println("NachaMain Location=" + location);

        FileValidation fValidation = new FileValidation();//The class that handles validating the output ACH file. 

        fValidation.validateNacha();//Method to validate the ACH file. 

        ReturnBuilderGUI gui = new ReturnBuilderGUI();//Class used for GUI's.

        gui.displayFileOption();//Method used to display the ACH output and error report name. 

        gui.showSavedErrors();//Method to display the error report. 

    }   
}

Essentially each of the classes calls modifies the input file. Inside of the mWriter class you will see this bit of code:

ReturnBuilderGUI gui = new ReturnBuilderGUI();//The GUI class.
                    ReturnBuilderGUI.displayGUI();//Calls the GUI to display the initial double list GUI. 

Calling that class calls the entire GUI for that loop. (The mWriter class loops for each batch). When the ReturnBuilder class is called this is the basic code layout:

public static void displayGUI(){//Method to display the GUI. 

        final JButton createReturnButton = new JButton("Create Return");
        createReturnButton.addActionListener(new ActionListener(){

            public void actionPerformed(final ActionEvent ae){

                if(verifyBatch==true){

                        initialScreenDecisions="NONE";//The user did not choose to add any entry details to the output list.
                        MainWriter.finishedCounter=true;//The boolean counter to trigger that the return is finished goes to true.
                        while(MainWriter.entryDetails.size()>0){//Removes all entry details from the input list.
                            MainWriter.entryDetails.remove(0);
                        }

                        while(output.size()>0){//Removes all entry details from the output list..
                            output.remove(0);
                        }

                        JOptionPane.getRootFrame().dispose();

                }else {

                    JOptionPane.showMessageDialog(null, "No batches have been completed!"); 
                }
            }
        });

        final Object[] createR = new Object[] { "Confirm",createReturnButton }; 

            int result = JOptionPane.showOptionDialog(null, getPanel(),"Return Builder", JOptionPane.OK_CANCEL_OPTION, 
                JOptionPane.PLAIN_MESSAGE, null, createR, "default");

        System.out.println(verifyBatch);


        //Creates a JOptionPane for the first GUI featuring 7 buttons and 2 lists.. 
    }

The getPanel() method inside of that JOptionsPane calls the panel that has some buttons and lists. Depending on what the user chooses some more JOptionPane's will appear that give the user more options. When they are finished the initial mWriter class will loop again (assuming there are more batches in the input file) and the ReturnBuilder class will be called again restarting the process.

Now, I can't for the life of me figure out a way to make all this happen inside of a JFrame that remains before and after all these other things happen without having to restructure my code.

I don't know if I've given you guys enough information to work with. My only idea right now is that I feel like I need to create a JFrame in the ReturnBuilder class and add the JOptionsPane to it, but then when the ReturnBuilder class is called again later I'm sure the JFrame would just open again and be duplicate.

Any ideas?

Upvotes: 0

Views: 54

Answers (1)

user1803551
user1803551

Reputation: 13427

It really looks to me that you need to use Cardlayout and flip to the next card at each step. When you are done, reset by flipping to the first card. CardLayout is cyclic, so it will flip to the first card automatically.

public class CardExample extends JFrame {

    CardExample() {

        JPanel main = new JPanel(new BorderLayout());
        CardLayout cl = new CardLayout();
        main.setLayout(cl);
        for (int i = 0; i < 4; i++)
            main.add(new StepPanel(i));

        JButton next = new JButton("Next");
        next.addActionListener(e -> cl.next(main));

        add(main);
        add(next, BorderLayout.PAGE_END);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    class StepPanel extends JPanel {

        StepPanel(int i){

            add(new JLabel("Step " + i));
        }
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> new CardExample());
    }
}

All of this is instead of JOptionPanes, which is usually more comfortable for a step-by-step user interaction (see, for example, installers). Just customize each of what I called StepPanels and at the end you can use a "load and reset" button instead of "next".

Upvotes: 3

Related Questions