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.
Related
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.
My Add-in opens a new frame for user input. When this frame is closed, ArcMap also closes. I have the DefaultCloseOperation set to DISPOSE_ON_CLOSE.
My code inside the add-in button creates an instance of my GUI class.
public void onClick() throws IOException, AutomationException {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createGUI();
}
});
Any Ideas? I want the user to be able to exit the window normally and continue working in ArcMap.
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);
}
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);
}
}
EDIT: Made the question shorter.
I have 5 classes in my program, all are GUI's. The front page GUI, options GUI, and quiz GUI all are able to be called and setVisible works for them. The tips GUI and the score card GUI are unable to be displayed with setVisible. There is a button on the quiz GUI that is supposed to call the tips GUI. However, the button will not call the GUI. It does work though; I know this because I did a System.out.println("Button test") and it printed.
The tips GUI is just a panel with a label and 3 buttons. I have not done any listeners yet, just designed the panel with NetBeans. On my quiz GUI, I have the button to launch the tips GUI and the code is:
private void tipsButtonMouseClicked(java.awt.event.MouseEvent evt) {
FrontPageGUI.tipsGUI.setVisible(true);
System.out.println("Test to check if help button works");
}
This is just as I have done with calling the other GUI's. It is just the tips and the scorecard that do not display. I don't understand at all... On my options GUI, I have a button that starts the quiz with the same code and it works just fine. Hence my confusion.
Hopefully my editing makes it easier to understand my question.
Thanks all,
Brandon
This is in NetBeans so it did generate almost all the code you see. This is why I don't understand. Does anybody have an idea on what I might be missing? If any of the other four classes are needed, I will happily post them.
then without any comments,
replace your public static void main(String args[]) { from your code
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
FrontPageGUI.tipsGUI.setVisible(true);
}
});
}
with
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TipsMainGUI());
frame.pack();
frame.setVisible(true);
}
});
}
and output will be