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?
You can add an
ActionListener
to theJButton
and then anActionPerformed
that opens the other GUI window. If you need documentation you can find it here --> event handler