I am just curious if anyone knows how to BEGIN to start the coding for telling the program to have a GUI with typical fields, labels, buttons, panels, etc in a GUI, and then when a user presses a button, it goes to a complete different GUI with brand new panels with fields, labels, buttons etc. Just like clicking on a link and the website refreshing to load a brand new website.

How would I go about doing that? Would it have to be a new program that extends the original GUI? I just wanna be able to press a button, and have the window change COMPLETELY to something else. And then when I finished there, go back to the original GUI by clicking a 'back' button or something.

I'm not talking about a website, i'm talking about a java program. No applets either.
The reason I ask is I'm looking at my code, and seeing there's no way I can have the program close this GUI, then open up a new GUI, preferably the same program file, or maybe a new java file, i don't know how it would work, then have the new GUI close and open up the original GUI with nothing cleared or reset, all the information still there.

Anyone have an idea on where I could start?

Another quick question I have is about JTextFields

When a user first sees a JTextField, inside the field will be text to help the user know what needs to be filled in there, and once the user clicks or presses the tab key into the field, the text will disappear. What is the code for that? I know it has to do with a KeyBoardAction or a MouseListener or something. Input?

Edited 6 Years Ago by musikluver4: n/a

anyone? If the first question is too confusing, what about the 2nd question I have? anyone?

When your submit button in frame1 is clocked, do

frame1.setVisible(false);
frame2.setVisible(true);

In frame2, when BACK button is clicked,

frame2.setVisible(false);
frame1.setVisible(true);

Hope this helps.

that definitely helps. So say frame1 and frame2, is this all in the same java file? All my getContentPane() is custom located on my frame with me locating them by pixels, not by the default .NORTH .SOUTH .EAST .WEST etc locations. So yes, my question is frame1 frame2 have to be in the same GUI java file?

Mine was in the same java Class.

First, try coding it with frame1 and frame2 in the same java class. If that works then you can experiment with the frames residing in different java classes.

Thanks! And what about my inquiry about the JTextFields? Have text already be in them, then when the user mouse clicks or tabs into that JTextField, the text disappears?

How do you make instances of the two frames? I have a layout called

LoanLayout customLayout = new LoanLayout();

and then i have the method that sets up the layout

public void setupLayout(Container pane)

and then further down i have the constructor of the java class

public MortCalcUIV121()
	{
		setupLayout(getContentPane());
		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
	}

and then the main method

public static void main(String[] args)
	{

		JFrame nFrame = new MortCalcUIV121();
		nFrame.setTitle("Mortgage Loan Calculator");
		nFrame.setSize(730,1000);
		nFrame.setLocation(0,0);
		nFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		nFrame.setVisible(true);
		nFrame.pack();					// added for better fit of the window
		loanText.requestFocus();
		
	}

Then I have the loanLayout method, the instance of customLayout, the very first code above, that tells the program where I want all of my stuff located on the JFrame

//Layout manager
	class LoanLayout implements LayoutManager 
	{
		public LoanLayout() 
		{
		}
		public void addLayoutComponent(String name, Component comp) 
		{
		}
		public void removeLayoutComponent(Component comp) 
		{
		}
		
		// layout dimensions
		public Dimension preferredLayoutSize(Container parent) 
		{
			Dimension dim = new Dimension(0, 0);
				Insets insets = parent.getInsets();
				 dim.height = 730 + insets.top + insets.bottom;    //height of window
				 dim.width = 1000 + insets.left + insets.right;     // width of window
			return dim;
		}
		
		public Dimension minimumLayoutSize(Container parent) 
		{
			Dimension dim = new Dimension(0, 0);
			return dim;
		}
		
		// arrangement of all components (JButton, JTextField, and JLabel) in window as listed above
		public void layoutContainer(Container parent) 
		{
			Insets insets = parent.getInsets();
			Component w = parent.getComponent(0); // array for components (JButton, JTextField, and JLabel)
			if (w.isVisible()) {w.setBounds(insets.left+50,insets.top+80,220,35);} //title label
			w = parent.getComponent(1);
			if (w.isVisible()) {w.setBounds(insets.left+130,insets.top+125,190,35);} //title2 label
			w = parent.getComponent(2);
			if (w.isVisible()) {w.setBounds(insets.left+355,insets.top+5,175,25);} //launchCalc button
			w = parent.getComponent(3);
			if (w.isVisible()) {w.setBounds(insets.left+650,insets.top+10,130,20);} //required label

			ETC ETC ETC
                }
	}

This code in the main method

JFrame nFrame = new MortCalcUIV121();

Makes an instance of the java class for all the main method typical stuff such as setting the location of the JFrame etc, but how do I make the loanLayout PLUS the Frame2 that I would like to create with what you helped me out with earlier about the setVisible(true)(false)????
I try to make an instance of the loanLayout, or even the customLayout, but Eclipse tells me it cannot do that.

Edited 5 Years Ago by musikluver4: n/a

JFrame nFrame = new MortCalcUIV121();

is my JFrame, how do I NOT make the java class or constructor, whatever it's called, the JFrame and have just the LoanLayout the JFrame keep it separate from the java class file? Same with an additional JFrame i would like to add as well.

nFrame is currently the whole java class file, I don't want that, if I want to make two different Frames visible or invisible.

Hope this will solve your problem. I'm giving you the code of two programs (frames). First one id the MainFrame. When you click the button on this MainFrame it will take you to another Frame name MainMenu. Try this out

This is the code for MainFrame.java

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

public class MainFrame extends JFrame implements ActionListener{

	JPanel panel;
	JButton btnNextFrame;

	public MainFrame(){

		super("Main Frame");
		panel = new JPanel();
		btnNextFrame = new JButton("Next Frame >>");

		panel.setLayout(null);

		btnNextFrame.setBounds(10,10, 150,25);
		panel.add(btnNextFrame);
		btnNextFrame.addActionListener(this);

		getContentPane().add(panel);

		setSize(200,80);
		setLocation(250,350);
		setVisible(true);

		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	public void actionPerformed(ActionEvent evt){

		if(evt.getSource()==btnNextFrame){
	
			MainMenu mm = new MainMenu();
			mm.setVisible(true);
			this.setVisible(false);
		}
	}
	public static void main(String args[]){

		MainFrame mf = new MainFrame();
	}
}

The next is the code for MainMenu.java . When you click the back button, it'll take you back to the MainFrame. Click and Enjoy!!

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

public class MainMenu extends JFrame implements ActionListener{

	JPanel panel;
	JButton btnAdd, btnEdit, btnDelete, btnBack;

	public MainMenu(){
		
		super("Main Menu");
		panel = new JPanel();
		btnAdd = new JButton("Add");
		btnEdit = new JButton("Edit");
		btnDelete = new JButton("Delete");
		btnBack = new JButton("Back");

		panel.setLayout(null);

		btnAdd.setBounds(10,10, 150,25);
		btnEdit.setBounds(10,55, 150,25);
		btnDelete.setBounds(10,100, 150,25);
		btnBack.setBounds(10,145, 150,25);

		btnBack.addActionListener(this);


		panel.add(btnAdd);
		panel.add(btnEdit);
		panel.add(btnDelete);
		panel.add(btnBack);

		setDefaultCloseOperation(EXIT_ON_CLOSE);

		getContentPane().add(panel);

		setSize(180,220);
		setLocation(250,350);
		setVisible(true);
	}
	public void actionPerformed(ActionEvent evt){

		if(evt.getSource()==btnBack){

			MainFrame mm= new MainFrame();
			mm.setVisible(true);
			this.setVisible(false);
		}
	}
}

For the JTextField doubt, what you have to do is, first create an object of the JTextField. For example:

JTextField txtName = new JTextField(20);
txtName.setText("Enter your name here");

so when the text field appears on your frame it will come with the text written it double quotes on it. But when you click on the Text Field that text must disappear. For that what you have to do is to write the coide for the focusGained() method of the JTextFiled. For that you have to use the FocusListener interface. the write the code:

public void focusGained(FocusEvent fe){

    if(fe.getSource()==txtName){

          txtName.setText("");
    }
}

This will do the things for you

Edited 5 Years Ago by sincerelibran: n/a

thank you sooo much!! That definitely helps a lot believe me. Now that examples were drawn up here, I can solve the problem.

the FocusGained() method only works AFTER you're in the TextField.
Example: I tab or mouse click into that TextField, and the pre text stays there, it's only after I press tab or mouse Click OUT OF IT, is when the text disappears.

Thoughts?

This question has already been answered. Start a new discussion instead.