How can I make this program multi windowed?

49 views Asked by At

I am learning Java and I though as good practice I could make a simple GUI program.

I'm hoping my program will be able to search read and write a text file.

I got all my components looking how I want them too. I have 4 panels searchPanel, readPanel, writePanel, and the mainMenuPanel they are displayed on the mainframe JFrame.

Now what I want to do (this is where I could really use your guys' help) is how to make this program multi windowed?

i.e if I press the read button in the mainMenuPanel it will display the readPanel. and so on with the other buttons.

I hope my question makes senses and that you guys can help me.

This is my code:

package MyGUIStuff;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Scanner;

public class multiWinDemo {

    public static void main (String args []) {
        /* Search Panel Ingredients
         * Label : File Name
         * TextField : look for ... 
         * Button - Search
         * Button - Back
         * TextArea - Results found ...
         */
        JLabel fileNameStorage1 = new JLabel ("File Name");
        JTextField searchFor = new JTextField (20);
        JButton searchBtnPanel = new JButton ("Search:");
        JButton backButton1 = new JButton ("Back");
        JTextArea searchedArea = new JTextArea ("Text Area Here!\nName : Misael\nIf this worked\nthen i should\nbe able to\nuse JScrollPane\ncorrectly", 5,20);
        searchedArea.setEditable(false);
        //searchedArea.setPreferredSize(new Dimension(100,100) );
        JScrollPane searchedAreaScoller = new JScrollPane (searchedArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        /* Write Panel Ingredients
         * Label : File Name
         * TextField : write this text to file ...
         * Button : Write
         * Button : Back
         */
        JLabel fileNameStorage2 = new JLabel ("File Name:");
        JTextField writeThis = new JTextField (20);
        JButton writeButton = new JButton ("Write");
        JButton backButton2 = new JButton ("Back");

        /* Read Panel Ingredients
         * Label : File Name
         * Button - Back
         * TextArea - File That I Chose
         */
        JLabel fileNameStorage3 = new JLabel ("File Name Here");
        JButton backButton3 = new JButton ("Back");
        JTextArea readTextArea = new JTextArea ("Text Area Here" ,5 , 20);
        readTextArea.setEditable(false);
        JScrollPane readScrollPane = new JScrollPane (readTextArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED  );

        /* Main Menu Panel Ingredients
         * Label : "File Name"
         * TextField : File Absolute Path
         * Button - Browse
         * Button - Search
         * Button - Write
         * Button - Read
         * Button - Exit
         */
        JLabel lbl1 = new JLabel ("File Name:");
        JTextArea howTo = new JTextArea ("Search: Search a text file.\nWrite: Write to a text file.\nRead: Read a text file.", 3, 20);
        howTo.setEditable(false);
        JTextField filePath = new JTextField (20);
        JButton browseBtn = new JButton ("Browse");
        JButton searchBtn = new JButton ("Search");
        JButton writeBtn = new JButton ("Write");
        JButton readBtn = new JButton ("Read");
        JButton exitBtn = new JButton ("Exit");

        //Search Panel
        JPanel searchPanel = new JPanel ();
        //searchPanel.setLayout(new GridLayout (3,9,5,5) );
        searchPanel.setVisible(false);
        searchPanel.add(fileNameStorage1);
        searchPanel.add(searchFor);
        searchPanel.add(backButton1);
        searchPanel.add(searchBtnPanel);
        searchPanel.add(searchedAreaScoller);

        //Write Panel
        JPanel writePanel = new JPanel ();
        //writePanel.setLayout(new GridLayout (3,9,5,5) );
        writePanel.setVisible(false);
        writePanel.add(fileNameStorage2);
        writePanel.add(writeThis);
        writePanel.add(backButton2);
        writePanel.add(writeButton);

        //Read Panel
        JPanel readPanel = new JPanel ();
        //readPanel.setLayout(new GridLayout (3,9,5,5) );
        readPanel.setVisible(false);
        readPanel.add(fileNameStorage3);
        //readPanel.add(readTextArea);
        readPanel.add(readScrollPane);
        readPanel.add(backButton3);

        //Main Menu Panel
        JPanel blank1 = new JPanel ();
        JPanel mainMenuPanel = new JPanel ();
        mainMenuPanel.setVisible(true);
        //mainMenuPanel.setLayout(new GridLayout (3,9,5,5) );
        mainMenuPanel.add(lbl1);
        mainMenuPanel.add(filePath);
        mainMenuPanel.add(browseBtn);
        mainMenuPanel.add(searchBtn);
        mainMenuPanel.add(writeBtn);
        mainMenuPanel.add(readBtn);
        mainMenuPanel.add(howTo);
        mainMenuPanel.add(exitBtn);

        //Program Frame
        JFrame mainFrame = new JFrame ("File Control");
        mainFrame.setSize(300,235);
        mainFrame.setLayout(new CardLayout() );
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setVisible(true);

        mainFrame.add(mainMenuPanel);
        mainMenuPanel.add(searchPanel);
        mainMenuPanel.add(writePanel);
        mainMenuPanel.add(readPanel);


    }
}

Note: what I've tried so far is: since all 4 panels are being displayed on the frame (frame has a card layout). what I did was try and add action listeners to the searchBtn and set the mainMenuPanel Visible false and searchPanel true, but nothing happened. How can I make it multi windowed?

2

There are 2 answers

0
AudioBubble On

You can add an ActionListener to the JButton and then an ActionPerformed that opens the other GUI window. If you need documentation you can find it here --> event handler

0
Kody Evangelist Dibble On

From what I'm reading what you are trying to do is when you press a button you want another panel to start up. There are various ways to do this, in my experience you'd want to use an actionListener with your button. Here is some code that I have written based on a simple finance calculator that you can use as a reference. Notice it's using a Combobox and calling the method getselecteditems() to get each selection from a combo box on my main panel.

I then use the add() to add the panel and removeall() and remove() to remove it.

  private class financeBoxListener implements ActionListener
           {
              public void actionPerformed(ActionEvent e)
              {


                  String selection = (String) financeBox.getSelectedItem();
                  if(selection.equals("Present value")){
                      //financeFinderPanel.removeAll();




                      presentValuePanel();
                      add(presentValuePanel, BorderLayout.NORTH);


                      financeFinderPanel.removeAll();
                      remove(financeFinderPanel);
                      pack();

                  }



                 else if(selection.equals("Simple interest")){



                     simpleInterestPanel();
                     add(simpleInterestPanel, BorderLayout.SOUTH);

                     financeFinderPanel.removeAll();
                     remove(financeFinderPanel);
                     pack();                      
                  }
                 else if(selection.equals("Doubling time")){
                     doublingTimePanel();
                     add(doublingTimePanel, BorderLayout.NORTH);

                     financeFinderPanel.removeAll();
                     remove(financeFinderPanel);
                     pack();
                 }
                 else if(selection.equals("Compound interest")){

                    compoundInterestPanel();
                    add(compoundInterestPanel, BorderLayout.EAST);
                    financeFinderPanel.removeAll();
                    remove(financeFinderPanel);
                    pack();
                 }

                 else if(selection.equals("Future value"))
                 {
                     futureValuePanel();
                     add(futureValuePanel, BorderLayout.WEST);
                     financeFinderPanel.removeAll();
                     remove(financeFinderPanel);
                     pack();

                 }
                 else if(selection.equals("Choose a formula")){

                      setSize(200, 125);
                      financeFinderPanel();
                      add(financeFinderPanel, BorderLayout.NORTH);
                      NONEMessage = new JLabel("PICK A SELECTION");


                  }







              }
           }

There are many other ways to do this, and this might not be the best way in your case. There is something called actionCommand and source which get the actually command that has been called, "you can use this with buttons."

like this...

if(actionCommand.equals("Calc Simple Interest")){


                        try{
                        double principal = Double.parseDouble(principalText.getText());
                        double yearlyRate = Double.parseDouble(yearlyRateText.getText());
                        double termYears = Double.parseDouble(termYearsText.getText());

                        double interestRate = financeFormula.simpleInterest(principal, yearlyRate, termYears);
                        //double interestRate = (principal * yearlyRate * termYears);

                        String msg = "Simple interest is: $" + dc.format(interestRate);
                        JOptionPane.showMessageDialog(null, msg);




                        simpleInterestPanel.removeAll();
                        remove(simpleInterestPanel);
                        financeFinderPanel();
                        add(financeFinderPanel, BorderLayout.NORTH);
                        pack();



}

In this example the name of the button I clicked is Calc Simple Interest.