Hello :)

This is my first post. I haven't done a Java type program in a while (2-3 years). I am trying to write a simple program which displays a window with 5 buttons. When you click either the first or second button it should open another window within which stuff will be done by the user ie. text entered etc.

I would like to somehow add "Back" buttons to those new windows so they can return to the original 'menu' with the 5 buttons. I have used 1 JFrame and 'attempted' to use multiple JPanels in order to do this.

An expert programmer from another java forum has said it is important to use just one JFrame and play with JPanels rather than using many different JFrames. Is this true? I dont know. Here is my program. Please feel free to criticize my poor coding practise as I have forgotten how to play with constructors and classes to do my dirty work :) Also, I have used the validate() and removeAll() methods in order to somehow manipulate Containers and start new windows but Im stuck now :( I dont know how to add the "back" buttons.

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class MyProgram {
	public static void main(String args[]) {
		final JFrame myJFrame = new JFrame("Tax Program");
		final JPanel panel = new JPanel();
		final Container cp = myJFrame.getContentPane();

		JButton button1 = new JButton("First window");
		JButton button2 = new JButton("Second window");
		JButton button3 = new JButton("button 3");
		JButton button4 = new JButton("button 4");
		JButton button5 = new JButton("button 5");

		panel.setLayout(new GridLayout(5,1));

		ActionListener al = new ActionListener() {
		         public void actionPerformed(ActionEvent e) {
					//myJFrame.setVisible(false);
		            cp.removeAll();
		            myJFrame.setSize(700,400);
		            JPanel Panel1 = new JPanel();
					cp.add(Panel1);

					cp.validate();
		         }
    	};

    	ActionListener al_2 = new ActionListener() {
		         public void actionPerformed(ActionEvent e) {
					 cp.removeAll();
					 myJFrame.setSize(700,600);
					 JPanel Panel2 = new JPanel();
					 cp.add(Panel2);
					 cp.validate();


        		 }
    	};

		button1.addActionListener(al);
		button2.addActionListener(al_2);

		cp.add(panel);

		panel.add(button1);
		panel.add(button2);
		panel.add(button3);
		panel.add(button4);
		panel.add(button5);

		myJFrame.setSize(600,600);

		//Puts the window in the middle of the screen.
		myJFrame.setLocationRelativeTo(null);
		myJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		myJFrame.setVisible(true);

	}
}

What you MIGHT want to do is, create a class that extends JPanel, it is not so hard, forget about the paint and update methods, keep them default. In the constructor of the new JPanel class, send your JFrame's handle, and add a BACK button, which calls on a function in the JFrame, called maybe setTop();

Forgot to say, make sure that the variable or instance of the custom JPanel class in the main JFrame class is private but defined in the CLASS's body, not in the main() function's body. Then in the setTop, just set that instance of JPanel to be not visible, and then just set (this.setVisible(true)) to the JFrame. It should work, but I mean it takes a bit of work anyways :D. Good lucK!

I dont understand what you mean by 'send your JFrame's handle'. Could you please explain with some sample code?

By sending the handle of the JFrame, you are literally passing the JFrame itself and all its children / components to the JPanel. You can then display this JFrame within the JPanel, without having to have multiple windows. There is a tutorial on Sun's website about this, which I believe is called JFrame's within JFrame's. Basically, its doing something like this:

JFrame mainFrame = new JFrame("Main Frame");
JPanel jPanel1 = new JPanel();
mainFrame.add(jPanel1);
JFrame nestedFrame = new JFrame("Nested Frame");
jPanel1.add(nestedFrame);

It is nothing complicated, what I would suggest is that you actually try it :)

:O Also, by SENDING the handle, it would be like the above code, but like this:

class Demo extends JPanel
{
   public Demo(JFrame nestedFrameHandle)
   {
     this.add(nestedFrameHandle);
   }
}

Do not take this the wrong way, but you should know what I mean because these are standard principles of OOP, know that you are not restricted to one class, nor one instance of the same object. Objects can be manipulated in various classes and methods, all affecting the same internal instance of a variable <- I hope that makes sense :) . Good luck!

great work .......thank you

Hi avudai, welcome to Daniweb.
Your thanks are welcome, but if you like a post you should show that by giving it an up-vote. Don't make a new post unless you have a relevant contribution to the actual topic.
You should also be aware that this is a six year old thread, and the code it includes is not how you would do things in the current versions of a Java. In general you should be cautious about anything you find on the web about Java that's not been updated in the last few years.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.