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
}
Related
I have my main JFrame and one more JDialog.
If user click on the button, i want JDialog to call method from this JFrame(which contains some operations on ComboBox in this JFrame).
How can I do that?
I don't want to use MyJFrame form = new MyJFrame(); because it will make a new JFrame which i don't want to do, i want to call method from JFrame which is running currently on my computer.
Thanks.
Assuming the JButton is on the JDialog.
If both are in the same class, why not just do this?
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//I just got clicked
form.doSomething();
}
});
which can be shortened to
button.addActionListener(e -> form.doSomething());
If they aren't, and you're extending JDialog (which I wouldn't recommend) just pass the JFrame in its constructor, then your dialog will have access to it.
It's not possible to add much more without seeing more of your code.
Maybe JOptionPane.showInputDialog() , show a JDialog to take input from User.
I have made a queue linkedlist, my main jFrame window is called "UI" on which there is a button "Donate" after pressing it a new jFrame window called "Donate" opens setting Visibility of previous jFrame(UI) to false (setVisible(false)).
jFrame "Donate" contains some text field and a "Donate Blood" button in the end, after filling out the text fields we need to press the "Donate Blood" button so that the entered values in text field should be stored in linkedlist and then setting jFrame "Donate" Visibility to false and jFrame "UI" to true to return back to our main jFrame window.
The problem is that every time I click "Confirm Donate" my data is not linking, for e.g: 3 people donated
John
Matt
Harry
When I traverse it I can only see the last entered name, where did John and Matt disappear to?
Donate button in "UI" action listener code:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
Donate d = new Donate();
this.setVisible(false);
d.setTitle("Donate - Blood Bank");
d.setVisible(true);
}
Donate Blood button action listener code which calls to donate jFrame window:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
UserInterface ui = new UserInterface();
BloodBank bb = new BloodBank();
enQueue(jTextField1.getText(), (int)jSpinner1.getValue(), (String)jComboBox1.getSelectedItem(), (String)jComboBox2.getSelectedItem());
first.Display();
this.setVisible(false);
ui.setTitle("Blood Bank");
ui.setVisible(true);
}
After debugging it many times I found out that if I remove this line of code from the button action listener and stop the jFrame "Donate" window from going invisible my queue linked list works fine, is setVisible(false) discarding my previous save data? And how can i fix this?
this.setVisible(false);
To help better understand here are some screenshots:-
Donate button in "UI" jFrame:
"Donate" jFrame window:
Each time you are creating a new instance of Donate in the first actionlistener and that of UserInterface in the second actionlistener. If your list is related to the instances of these frames, then you are loosing them. Also, it is not and advisable thing to do. You could create the instances of both the frames in some way so that it can be accessed from both the actionlisteners and then just call setVisible() on the same instances. This should solve your problem.
two JFrames,
JFrame Main; // Main JFrame
JFrame Sub; //Second JFrame that is initialized from within Main via a JMenuItems ActionListener.
mainMenuItem.setActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
try{
Sub subFrame = new Sub();
subFrame.setVisible(true);
}catch(Exception e){}
}
});
}
The problem is whenever i close the second JFrame (Sub) it closes the first aswell.
Both JFrames have:
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Is that part of the problem?
EXIT_ON_CLOSE means to exit the program immediately (System.exit()) when the frame is closed.
You probably want to set this to DISPOSE_ON_CLOSE, then add a WindowListener and close the program only if both frames have been closed.
(Or, perhaps you want only the main frame to have EXIT_ON_CLOSE)
Yes. JFrame.EXIT_ON_CLOSE by definition exits the application. For your second Frame use DISPOSE_ON_CLOSE or HIDE_ON_CLOSE.
Hope this helps!
You state:
JFrame Sub; //Second JFrame that is initialized from within Main via a JMenuItems ActionListener.
This suggests you've a design problem:
Your 2nd "frame" shouldn't even be a JFrame since it is not behaving as a separate independent main program window.
Instead it's acting as a dialog since it is dependent on and shown from a parent window, the main JFrame. So make the secondary window a JDialog not a JFrame, and all these problems will go away.
You will need to consider whether it should be a modal dialog where the main window is not accessable to the user while the dialog is open, or a non-modal dialog.
Having said that, you may even be better off using one window/JFrame and swapping views via a CardLayout.
Please read this link: The Use of Multiple JFrames, Good/Bad Practice?, and in particular please have a look at Andrew Thompson's community wiki answer.
Let's say we have a JFrame frame that contains two JPanels, buttonPanel and dataPanel, and in this panel a single JButton button. When clicked, button creates and shows a JDialog dialog in its own window (as usual). Using several JTextFields and a submit button, the JDialog dialog creates a new Object dataObject encapsulating these input data. If we wish our dataPanel JPanel in the main application frame to display this dataObject, how should dataObject be appropriately passed to the JPanel residing in a foreign JFrame?
That was a mouthful even to me while writing it, so I'll attempt to clarify:
JFrame frame
JPanel dataPanel - meant to display data from Objects created in the JDialog
JPanel buttonPanel - contains a button to open the JDialog, into which some information will be entered and with said information our Object dataObject is constructed.
The goal here is to pass this dataObject (and it's constituent fields) to the dataPanel to be displayed. What is the most appropriate way to handle this? I considered keeping the Objects in dataPanel static and then calling a static method from the JDialog to add the new object, but it doesn't seem the proper thing to do.
Some guidance?
Much will depend on the structure of your program, including how the dialog is supposed to behave:
If the JDialog is modal and disappears when the submit button has been pushed, then the solution is easy -- extract the data from the dialog-related class after it returns which will be the line of code right after where you display the dialog. The dialog's submit JButton's listener could simply make the dialog no longer visible.
Otherwise if the JDialog is non-modal and disappears when the submit button has been pushed, then you may wish to attach a Listener to its Window, I believe a WindowListener, and then have your calling code extract the information when the listener indicates that the dialog has been closed or is closing.
Otherwise, if the JDialog is non-modal and is not supposed to become invisible when the submit button has been pressed but you need to update the calling program with new data, then I would have the calling class add a PropertyChangeListener onto the dialog-related class so that the dialog-related class can notify any listeners that submit has been pressed. This code would be in the dialog's submit JButton's listener.
I would give a dialog-related class a public DataObject getDataObject() method that the calling code can call once the dialog returns, allowing the class that displays the dialog to extract the pertinent information when needed.
Whatever you do, there is no reason to use static fields and many reasons not to. I strongly urge you to not even consider this.
For example of a modal dialog that returns:
// caveat: code has not been tested by compilation or running.
JButton myButton = new JButton(new AbstractAction("Show Dialog Button") {
public void actionPerformed(ActionEvent evt) {
MyDialogPanel myDialogPanel = new MyDialogPanel();
JDialog myDialog = new JDialog(myJFrame, "My Dialog",
ModalityType.APPLICATION_MODAL);
myDialog.add(myDialogPanel);
myDialog.pack();
myDialog.setLocationRelativeTo(myJFrame);
myDialog.setVisible(true);
// dialog now returns and we can get the data
// assuming that the wrapper object for your data
// is called "DataObject"
DataObject dataObject = myDialogPanel.getDataObject();
// and perhaps use it. Assume setDataObject is a method
// of the main GUI that displays the data object
setDataObject(dataObject);
}
});
How do i restrict access to other JFrame?
if I open my main frame and when click the button to display other jframe, the user should not be able to go back to the main frame.
how do I do that?
if i open my main frame and click the add button,
When you click the button you display a modal JDialog. Then until the user closes the dialog they won't be able to access the main frame.
try this method...
this.setEnabled(false);
Your question is not clear to me but as per my understanding I believe You want to open a dialog on button click but when you click the button a new JFrame is displayed and it becomes impossible for you to get back to the original frame.
Use a dialog /pop-up on your button click like JOptionPane.
If you want to open a JFrame on button click, making a HOME button on the newly created/opened JFrame and linking that button to main JFrame might be a good option.Closing the newly created JFrame will display the originally created JFrame anyway.
Use a dialog (JDialog class) instead and make it modal.
Here is some help on how to do this:
http://download.oracle.com/javase/tutorial/uiswing/components/dialog.html
Regards,