How to modify jFrame characteristics - java

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);
}
}

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.

Start application as Dialog that calls parent frame

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.

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.

Two of my Java GUI's won't display with setVisible(true) but the other three will

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

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.

Categories