How to connect two JFrames in netbeans without using swing? - java

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.

Related

Make JFrame GUI wait and then continue after a button is pressed

I have a gui class MyGUIClass that extends JFrame. I would like to accomplish following in java. I looked into EventQueue, but somehow could not get things right. I am not even sure if this is the correct approach.
In the main, I would like following sequence of events:
-> start JFrame
-> Keep JFrame active and wait until play button is pressed. when button pressed,
-> execute rest of the code in main, but keep gui alive to receive commands from JFrame.
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
new MyGUIClass().setVisible(true);
}
});
}
catch (Exception e) {
e.printStackTrace();
}
System.out.println("Portion to execute only after request from GUI");
}
But the above trial is not working and the println prints while gui is running without any commend from GUI.
You will have to provide a listener to the button on click of which you want to execute some code.
But if you want to put the code which you want to execute in main. You will need some anonymous class containing code or lambda etc.
I will show here how you can achieve what you want
class MyGUIClass extends JFrame {
...
// I am using Runnable to contain code you want to execute.
// You can use Function or any class/interface you want
Runnable codeToExecute;
MyGUIClass(Runnable codeToExecute){
this.codeToExecute = codeToExecute;
...
}
...
void yourGUImethod(){
...
JButton btn = new JButton(new AbstractAction(){
#Override
public void actionPerformed(ActionEvent e) {
codeToExecute.run(); // execute code on button press
}
});
yourPanel.add(btn); // Add this button to your panel
...
}
Now you can provide executing code from main
Runnable codeToExecute = () -> { // Using lambda
System.out.println("Portion to execute only after request from GUI");
}
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
new MyGUIClass(codeToExecute).setVisible(true);
}
});
I am not even sure if this is the correct approach.
Well you haven't stated if this is the main window or a child window.
If this is a "child window" and you are prompting for information from the main window, then you should be using a modal JDialog for the child window.
If this is the "main window" then no it isn't the correct approach.
GUI's are event driven. This means you write code to respond to events.
So, when the frame is created you create all the components and add them to the frame.
For the "Play" button you need to add an ActionListener to the button. So when the button is pressed you invoke the code related to the play action.
Then the GUI will just sit there waiting for more events to be generated by the user.

jInternalFrame not brought to the front

I have a JDesktopPane which contains a number of JInternalFrames. The first time I press one button to visible jinternalframe1 and second button to visible jinternalframe2, it appear above the main window without problems. However, if I press one of the buttons to Reopen jinternalframe1 or jinternalframe2, they are not brought in front of the main window... EDIT: actually, i can't do anything with jinternalframe on a button click...i can only click once on the button and then no operation can be perform on the jinternalframe through the button..why it doesn't work!!
this is the coding of button1...
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
jinternalframe1 frame1 = new jinternalframe1();
try {
if(Allow.flag == false) {
desktopPane.add(frame1);
frame1.setVisible(true);
Allow.flag = true;
} else if(Allow.flag == true) {
frame1.setSelected(true);
}
} catch(PropertyVetoException e) {
System.out.println(e);
}
}
Allow.java
public class Allow {
static boolean flag = false;
}
Every time you click on the button you create a new JInternalFrame object, but you only ever add the first internal frame that you create to the desktop pane.
Don't keep creating new internal frame objects. I would guess you should only create the internal frame if your "frame1" variable is null.
If you need more help then post a proper SSCCE that demonstrates the problem.

How to go back to previous frame that was invisible

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.

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