Start application as Dialog that calls parent frame - java

The situation is as follows:
My app consists of a dialog box containing x elements and a button. The user presses button after interacting with elements and if he interacted them with a specific way, only then the parent frame in which the Dialog Box resides should appear.
For this purpose, I currently know of this approach:
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(false);
jDialog.setVisible(true);
}
});
}
And then add this command on Button which resides inside jDialog:
new NewJFrame().setVisible(true);
This does the trick quite well and neat, but the previous instance called using new NewJFrame().setVisible(false); is still running (as far as I know).
Isn't there anyway I could perform this action on button (residing inside jDialog) press as using something like:
NewJFrame.setVisible(true);
(It currently gives me error: Non-static method cannot be referenced from static context)

Make sure that the dialog is modal, and you can simply do:
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
NewJFrame newJFrame = new NewJFrame();
newJFrame.pack();
// no need to set visible false. It already is
MyDialog myDialog = new MyDialog(newJFrame);
// make sure the super constructor makes the dialog modal
myDialog.pack();
myDialog.setVisible(true);
// here the dialog is no longer visible
// and we can extract data from it and send it to the JFrame if needed
newJFrame.setVisible(true); // ****** here
}
});
}
Otherwise if you absolutely must fiddle with the JFrame from within the JDialog, simply pass the NewJFrame into the JDialog's constructor, something that you need to do regardless since it should be used in the JDialog super constructor, use it to set a NewJFrame field, and call setVisible(true) on the instance inside of your dialog.

Related

How to modify jFrame characteristics

I'm using the Java GUI for the first time through Netbeans. I'm trying to design some basic hotel software, and for that I'm trying to implement multiple screens through multiple JFrames.
What I don't know is how to get the jFrame I'm using to go invisible and another jFrame to go visible when a button in my current jFrame is clicked.
I have a class, Hotel with this code:
public static void main(String[] args) {
// TODO code application logic here
HotelMain h = new HotelMain();
HotelBooking b = new HotelBooking();
h.setVisible(true);
}
HotelMain and HotelBooking are 2 separate jFrame files. I'm trying to get HotelMain to disappear and HotelBooking to appear when a button is clicked on the HotelMain screen.
And this is some code from the main function in HotelMain. Should this even be there? Also, how do I use this to get the jFrame to disappear?
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new HotelMain().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.

Please help me to keep my main window open

I have a main window, that opens another window. I want to close this window and keep the main window open, but it closes the main window too.
I tried a lot of methods: setDefaultCloseOperation(), dispose(), setVisible(), but nothing worked for me.
In the main window I have this code
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
AdaugaComanda ac = new AdaugaComanda();
ac.setVisible(true);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FereastraPrincipala().setVisible(true);
}});}
and in the other window (that closes the main window when I close it) I have the following code
public class AdaugaProdus extends javax.swing.JFrame {
public AdaugaProdus() {
initComponents();
initComboBoxes();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
AdaugaProdus ad = new AdaugaProdus();
ad.setVisible(true);
}
});
}
A possible simple solution: make the secondary window a dialog such as a JDialog, not a JFrame. An application will usually have only have one JFrame open at one time. A JDialog can hold as complex a GUI as any JFrame can, and when it closes, it will never close down the JVM as a JFrame can (as you're finding out). You also have the option of making the dialog modal or not, as the need dictates.
Having said this, please understand that while new information can be shown as a dialog that is owned by the JFrame, it can also be displayed by swapping components on the JFrame via a CardLayout -- it depends on what type of information that you're trying to show.

Closing the Main JFrame

I have a main jFrame with the help of which i press button and open new JFrames but the problem is that when i open other JFrame the previous ones still remains there where as what i want is that when i press next button then i move forward to the new JFrame (the previous one should not be there) and when i press previous button i move back to the previous JFrame.
I know there are functions of dispose,they do well like jframe1.dispose() but i dont get it how to hide the very first JFrame whose code in the main is written like this
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run()
{
new GraphicalInterface().setVisible(true);
}
});
}
how do i set this as .setVisible(false) in the button code?
You need to retain a reference to the JFrame, so you can set it's visibility later.
private JFrame myFrame;
public void run() {
myFrame = new GUI();
myFrame.setVisible(true);
}
public void hide() {
myFrame.setVisible(false);
}
Note that a JFrame is a top-level container, you are only really supposed to have one per application. It may be better if instead of using multiple JFrames you use just one, and swap in various JPanels instead.
It would safe resources if you keep just one frame and set a new panel as content.
Your question was how to handle the reference to the frame? Please provide the code where the next frame is created.
You could assign your GUI (extends JFrame I suppose) to a variable and call .setVisible(false) on that object. Since your object from the code above is more or less anonymous, you won't have access on that.
You could also check this.

While the form is running, other commands are executed

i write a method to create a form, then some other commands after that in main.(java)
package pak2;
import javax.swing.*;
public class form6 {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame jframe = new JFrame();
JButton jButton = new JButton("JButton");
jframe.getContentPane().add(jButton);
jframe.pack();
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
System.out.println("test***ok");//testtttttttttttttt
}
}
i want to execute "System.out.println("test***ok");" after that the form are closed.
but when i run program, before i enter information in form, Other commands that are executed!
While the form is running, other commands are executed! how can i set it.
You're going the wrong way about this.
Here's an example with comments:
public class Form2 {
public static void main(String[] args) {
final JFrame jframe = new JFrame();
final JButton jButton = new JButton("JButton");
/**
* Once you create a JFrame, the frame will "listen" for events.
* If you want to do something when the user clicks a button for example
* you need to add an action listener (an object that implements ActionListener)
*
* One way of doing this is by using an anonymous inner class:
*/
jButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(jButton)){
jframe.dispose();
System.out.println("Button clicked!");
}
}
});
jframe.getContentPane().add(jButton);
jframe.pack();
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// displaying the form will NOT block execution
jframe.setVisible(true);
System.out.println("test***ok");// testtttttttttttttt
}
}
There are two important things you need to know about Swing and Frames before continuing:
Constructing components and calling methods on components must always be done on the Event Dispatch Thread (EDT). This article explains the principle of the single-thread rule in Swing. Note that this rule also applies to the Main thread. Use SwingUtilities.invokeLater and SwingUtilities.invokeAndWait to do this correctly.
JFrames are independent elements. Making one visible will not block the calling thread. However, if that's what you want to do, then use a JDialog. Dialogs are designed for blocking and waiting for user input, and if you create a modal dialog, making it visible will block the calling thread (and if you set a parent Frame or Dialog, then you can make it stay on top too). A good example of this is JOptionPane (give it a try!) Other than that, and the fact that JDialog extending Dialog instead of Frame, it is almost identical and you can add whatever elements you want to a base dialog.
Some example code:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
// The boolean parameter 'modal' makes this dialog block.
JDialog d = new JDialog((Frame) null, true);
d.add(new JLabel("This is my test dialog."));
d.setLocationRelativeTo(null);
d.pack();
System.out.println("Dialog is opening...");
d.setVisible(true);
System.out.println("Dialog is closed.");
System.exit(0);
}
});
}
I hope that answers your question. :)
In your code main() function is the calling function and form6() is the called function. After the form6() function is called from the main() function it returns back to the main() function. Remember this the control always returns back to the calling function after the called function is executed.
I'm not sure what the implementation behind your form is, but you'll need it to block for input if you don't want it later code to be executed immediately afterward, maybe using Scanner?

Categories