How to go back to previous frame that was invisible - java

I am very new to Java Swing and I am working on a login frame with swing that works like this.
After I login successfully in a frame, another new frame is opened while the login frame goes to invisible.
What I am trying to do is that when i close the another frame (after login frame) I want the previous login frame to show again from invisible to visible. please let me know how to do this..:)

Suppose your previous frame is myPreviousFrame
just write myPreviousFrame.setVisible(true); when you want to make visible.
Example:
currentFrame.dispose();
myPreviousFrame.setVisible(true);
Note: if you write code System.exit(0) it will close (terminate) your application. When your application goes terminate you can not make login frame as visible. You need to restart your application. So you need to write dispose().
UPDATED:
I suppose you have a method exitForm() which invokes when you click the Close (X).
Example:
private void exitForm(java.awt.event.WindowEvent evt) {
//System.exit(0); which was used
// to fullfill your requirement you need to write below code
this.dispose();// here [this] keyword means your current frame
//OR simply you can use this.setVisible(false); instead of this.dispose();
myPreviousFrame.setVisible(true); // this will displays your login frame
}

u may try like this:
public class jFrame1 extends javax.swing.JFrame{
// ur code
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jFrame2 f2 = new jFrame2(this);
f2.setVisible(true);
this.setVisible(false);
}
}
public class jFrame2 extends javax.swing.JFrame{
// ur code
private JFrame frame;
public jFrame2(JFrame frame) {
this.frame = frame;
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
this.frame.setVisible(true);
this.setVisible(false);
this.dispose();
}
// so on
}

Here i am just considering two frames and at present you are at second frame and wanna go back to first frame.
public class previous_action implements ActionListener{
public void actionPerformed(ActionEvent t){
Movieticket m;
m=new Movieticket();
m.display();
}
}
Here previous action is a class which will take you back to previous frame.Button frame is a class which sets frame where we are at present.Movie ticket is a public class containing display function which sets frame when the application started that is the first frame.
when the button is clicked it will take you to previous frame.

Related

JFrame appears blank after swapping JFrames twice (using Intellij GUI designer)

I'm relatively new to Java Swing in general and decided to use Intellij's GUI Designer. For those not familiar with it, Intellij creates 2 files for each GUI "screen". One is the "design" in the form of a .form file where you drag and drop UI components, and one is the "controller".
Anyway, I'm doing a 5-step questionnaire implemented on Java Swing. The first part asks the user what is their favourite fruit, and when a choice button is clicked, the choice is saved and the next JFrame appears with the next question and so on. JFrame1 transitioning to JFrame2 worked fine, and all UI components were shown. However, when I tried to transition from JFrame2 to JFrame3, JFrame3 showed up blank instead.
I've tried to call .pack() before setVisible(true), and then calling .toFront() after that, but that didn't help.
Below shows a section of my code. JFrame1, 2, and 3 all use the same exact code in its constructors and calling of the next JFrame. JFrame1 only differs by a single line which will be stated later in the code.
JFrame1.java
public class Frame1 extends JFrame {
private JPanel mainPanel;
private JLabel narrationLabel;
private JButton option1_btn;
private JButton option2_btn;
private JButton option3_btn;
public Frame1()
{
setTitle("Questionnaire");
setSize(1000, 1000);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Frame2 nextScreen = new Frame2 (); //declare the next screen to show
add(mainPanel); //this is only for the first starting JFrame
//JFrame2 and JFrame3 do not have this
//repeat this for buttons option2_btn and option3_btn
option1_btn.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
//swap screens to the next main filler selection screen
mainPanel.setVisible(false);
nextScreen.pack();
add(nextScreen.getPanel());
nextScreen.getPanel().setVisible(true);
}
});
}
public JPanel getPanel()
{
return mainPanel;
}
}
Main.java
public class Main
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame1 frame1 = new JFrame1(); //create start screen GUI
frame1.setVisible(true); //display GUI
}
});
}
}
My hunch tells me it might a concurrency issue, but I'm not sure if I am right, or where is the issue and how to resolve it. If a .form file is needed, I would gladly upload the code for it. Any help appreciated.

Handle event from another class (java)

i'm at a standstill developing a java application.
I have a series of JFrames that open each other, and once I open the next, the frame before shall close.
Now, all works fine, I used
setVisible(false);
to "close" the frame, but I have some problems with a specific form I generated:
The form has some panels inside, and the panels each have a button which opens the next form. Now, I wonder how can I apply setVisible(false) to the form that contains those panels.
ChooseForm is the name of the form that contains the panels;
And I have this event handling the button click of each panel's button
private void btn_scegliMouseClicked(java.awt.event.MouseEvent evt) {
Database.getAbitazioneByCodice(label_codice.getText());
MainForm.showFrame();
}
Thanks in advance for the help.
There are many ways to do this.
A Simple way you can do like this:
class YourFrame extends JFrame implements ActionListener {
private YourPanel panel;
public YourFrame() {
//...
panel.addButtonActionListener(this);
}
public void actionPerformed(ActionEvent e) {
//show other Frame and hide this Frame
}
}
class YourPanel extends JPanel {
private JButton yourButton;
public void addButtonActionListener(ActionListener listener) {
yourButton.addActionListener(listener);
}
}

Closing JFrame that opened a JDialog

Basically I have a JFrame that opens a JDialog.
The problem I'm facing is when I attempt to do a right click on the taskbar -> close window in Windows OS
The JDialog would be 'blocking' the closing event.
I would want to close the application even if the JDialog is shown but at the same time blocking any input into the parent frame.
Edit: As a norm, dialog boxes are made to block the output but as I have mention in the comment below :
Problem lies when users need to close the application but when with a
few dialog box on screen. It requires them to close all to them before
being able to close the entire application. It would be better if only
selected critical dialog boxes blocks the closing event but not those
trivial ones
Main class
public class Main {
private Frame frmMain;
public static void main(String[] args) {
new Main();
}
public Main(){
initialize stuff..
frmMain.setVisible(true);
Message msg = MessageFrame.openDialog(frmMain);
...
}
MessageFrame class
public class MessageFrame extends JDialog {
private Message message;
public static void openDialog(Frame frame){
return new MessageFrame(frame).message;
}
private MessageFrame(Frame frame){
super(frame);
setModalityType(ModalityType.APPLICATION_MODAL);
...
setVisible(true);
}

How to connect two JFrames in netbeans without using swing?

I am make a project on cars. How can I make distributor frame popup and cars frame not visible and close automatic? Kindly send any solution in simple and effective way.
I have done coding this way:-
{
Cars frm1=new Cars();
Distributor frm2=new Distributor();
frm2.setVisible(true);
frm1.setVisible(false);
frm1.setDefaultCloseOperation(frm1.DISPOSE_ON_CLOSE);
}
".Please help me to how I can make distributor frame popup and cars frame is not visible and close automatic."
Ok so in Netbeans GUI Builder, you may want to do the following (this is assuming you have created two separate JFrame form files
In the frame that is the launching program (we'll call it MyFrame1) add a button to it (we'll call it jButton1)
Add a listener to the button, then the following code should be auto-generated
public void jButton1ActionPerforemd(javax.swing.ActionEvent evt) {
}
In that actionPerformed, just instantiate the second frame (we'll call it MyFrame2) and setVisible(false) to MyFrame1. MyFrame2 should already be visible upon instantiation, so you wouldn't have to setVisisble(true) on it
public void jButton1ActionPerforemd(javax.swing.ActionEvent evt) {
MyFrame2 frame2 = new MyFrame2();
MyFrame1.this.setVisible(false);
// You can also use MyFrame1.this.dispose(); dependind if you ever need to use that frame again
}
I think this should work
you need to setVisible Jframe2 as true...so it can apear on output sceen
public void jButton1ActionPerforemd(javax.swing.ActionEvent evt)
{
myFrame2 frame2=new myframe2();
myframe1.this.setVisible(false);
frame2.setVisible(true);
}
create action event for the button such that when when you click will take
you
to the next page for my case next page is nextjFrame
private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {
setVisible(false);
nextjFrame ob=new nextjFrame();
ob.setVisible(true);
}
private void BTNConvertActionPerformed(java.awt.event.ActionEvent evt) {
/*
This is the action performed event for my Button "BTNConvert"
*/
java.awt.EventQueue.invokeLater
(new Runnable()
{
public void run()
{
new JFrame2().setVisible(true);
}
});
/*
This will set the second Frame Visible.
*/
JFrame1.this.setVisible(false);
/*
This should set the first frame invisible or whatever. Any other code should be
written before the curly brace below.
*/
}
//You're Welcome.

How to close a frame on action event of a button from a different class?

I have a designed a control-frame on 1 class which has jbutton components, I have coded like on action event of these components diff-diff frame get initialized.
For each frame there is a class. now when I press second jbutton from control-frame then have to close the frame (which has opened when I pressed first jbutton from control-frame).
Just keep track of frame instance as instance variable in main class. Then close that instance on clicking close button.
public void open(ActionEvent e) {
frame = new something.. // your frame
}
public void close(ActionEvent e) {
frame.dispose();
}
Note:
JFrame frame is global variable.
write this in your button click linstener
yourFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Categories