I have a Jframe and i need to link it to another Jframe if button is clicked it goes to another Jframe screen on click and closes old screen. accully i want if user click login register then it goes to login screen for getting login i am newbie and a bigginner kindly help me P.S its my first Qestion at Stack so ignore mistakes.
Double Click the Register Button in the NETBEANS or add the Event Listener on Click Event (ActionListener)
btnLogin.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
this.setVisible(false);
new FrmMain().setVisible(true); // Main Form to show after the Login Form..
}
});
Related
I will try to be really specific with my question.So I have an application in a JFrame which contains 3 buttons and each of them open another JFrame and they are doing some animation inside.So what i have to do is close just one frame but not the main frame with it.And in this 3 frames there is SwingWorker who is processing some data,so when I close specific frame I want this application to stop and not doing in a background.
You can add an actionListerner on the click of the button..Once the button is clicked you set the visibility of a specific frame to false the user the dispose function on that specific frame and cancel its background activities
like below
cancelbtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame1.setVisible(false);
frame1.dispose();
I have designed two JFrames in NetBeans.
When I click the "rules" button (i.e placed on JFrame1) then it opens a second JFrame (but JFrame2 opens over JFrame1's window, that's what I dont want).
In the second JFrame there is a "close" button. But when I click this button, I want JFrame1 to be opened and it is working too, but JFrame2 is actually not closed and JFrame1 is appearing over JFrame2.
In short the main form is JFrame1. When I click the "rules" button from JFrame1 it opens JFrame2 over JFrame1, and in JFrame2 there is a "close" button when it gets clicked the main form (i.e JFrame1) is lauched but it is launched over JFrame2.
The scenerio is JFframe1 -> JFrame2 -> JFrame1
Now my question is after clicking the "rules" button, JFrame1 should be closed and JFrame2 displayed on the screen and vice versa.
Assuming your button has an actionListener, after clicking the "rules button" put in:
JFrame1.dispose(); //Remove JFrame 1
JFrame2.setVisible(true) //Show other frame
And then reverese them for the opposite reaction
Somethig like this should be on the constructor or method which create JFrame2:
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//call another method in the same class which will close this Jframe
CloseFrame();
}
});
It's method which should close JFrame2
public void CloseFrame(){
super.dispose();
}
Well, if you already have a actionListener, you should add this:
JFrame1.dispose(); // This will close the frame
The JFrame1 is the name of your frame.
And if you want to open another frame that you have, add this:
JFrame2.setVisible(true); // This will put the other frame visible
I'm not an expert by any means, however, I ran into this problem as well. If you set your second JFrame to hidden, when you hit "Cancel", it will close the second JFrame.
//this is the code for the "cancel" button action listener
public void actionPerformed(ActionEvent e) {
setVisible(false);//hides the second JFrame and returns to the primary
this worked for me (Frame1 Called RegScreen and Frame2 Called MainScreen):
RegScreen.this.setVisible(false);
new MainScreen().setVisible(true);
Hope that this helps :) Regscreen was the original frame open at startup.
If this doesn't work, try this
JFrame1.dispose(); //Remove JFrame 1
JFrame2.setVisible(true) //Show other frame
JFrame2.setVisible(true);
this.dispose();
Have a MainClass with a main() method.
Have the MainClass that has the main() method encapsulate your JFrame1 and JFrame2 reference variables. Don't have JFrame1 or JFrame2 contain main() unless you have a specific reason.
After something is clicked in one of the JFrame objects, instantiate/make visible the other JFrame object and dispose of itself via your MainProgram.JFrame object methods.
Example:
//btn event inside 1st JFrame/window
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
MainProgram.openResultsForm(); //MainProgram opens 2nd window
MainProgram.queryEntryForm.dispose(); //MainProgam closes this,
//the 1st window
}
I have the following situatation:
I have a Java Swing application.
In the class that implement my GUI I have a button named Log Out tath is binding to an event listener that handle the click event, something like it:
JButton logOutButton = new JButton("LogOut");
header.add(logOutButton);
Then in the same class that implement my GUI I have declared the ActionListener that handle this event using an inner class:
logOutButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.out.println("logOutButton clicked !!!");
System.exit(0);
}
});
In this moment when I click the logOutButton button the program end. I would that instead exit it is restarted by running a specific class called LoginForm (the class that implement the login form GUI)
What can I do to do this thing?
Tnx
Andrea
You don't really need to close/open window junky approach at all. Just use Card Layout:
set Frame's content pane's layout to card layout.
getContentPane().setLayout(new CardLayout());
Put your different Form's content code inside different panel and
add them to the content pane with their corresponding name, for example:
getContetnPane().add(logInFormPanel, "logIn Form");
Now you can simulate the card to appear whenever necessary by calling CardLayout.show(Container parent, String name). For example:
logOutButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.out.println("logOutButton clicked !!!");
CardLayout cl = (CardLayout)(getContentPane().getLayout());
cl.show(getContentPane(), "logIn Form");
}
});
Check out a CardLayout demo from my another answer.
I have a JFrame class and it was made in the design section on Netbeans. I am trying to make a log in button that takes closes the current frame and opens another, is there anyway I can do that?
I have tried:
JFrame frame = new JFrame();
But I want it to be editable in the design section!
Double Click the Login Button in the NETBEANS or add the Event Listener on Click Event (ActionListener)
btnLogin.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
this.setVisible(false);
new FrmMain().setVisible(true); // Main Form to show after the Login Form..
}
});
new SecondForm().setVisible(true);
You can either use setVisible(false) or dispose() method to disappear current form.
This link works with me: video
The answer posted before didn't work for me until the second click
So what I did is Directly call:
new NewForm().setVisible(true);
this.dispose();//to close the current jframe
JFrame.setVisible(true);
You can either use setVisible(false) or dispose() method to disappear current form.
I am creating a GUI in which my home page has a button labelled "Welcome to the Panel"
The point is that when you press on this button, it will navigate to a new page where I will have other functions. My only problem is that I dont know the syntax or how that when clicking a button, it will navigate to new page.
JButton btn = new JButton("Welcome to the Panel");
btn.setActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
// Here you open the other window. You can use JFrame, JOptionPane or JDialog
}
});
button.addActionListener(new ActionListner()
{
public void actionPerformed(ActionEvent ae)
{
//code to show pane
}
});
You need to register an ActionListener on your button and inside that action listener you make that panel (the page) visible.
How you do that depends on your layout, i.e. with a CardLayout you'd show the corresponding card (here's the doc). Using other layouts you might have to replace a component, e.g. if you use a BorderLayout and your content is placed in the center, replace the center component with the panel you want to show.
Note that if you're not familiar with layout managers yet, you should first have a look at those before doing dynamic changes to the ui (like navigation etc.).