I have a question. I couldn't solve it for 4 days. I'm making a stopwatch using gui swing in Java. There are 1 jframe and 1 jdialog. I used threads in Jframe. It has a start stop and reset button, it works very well. The problem is; I had to use the jdialog like this (this is the job situation) so when I press the start stop and reset button that I created in jdialog, it triggers the start, stop and reset button in the jframe. There is no problem with triggering either. The main problem is this: While the thread works as it should in the jframe, it does not work when I call it from jdialog. I searched a lot, I made a logic, but I couldn't find it, can anyone help?
Related
In a JFrame, when I click on 'login', I pop up another Jframe which is the login window.
How do I make my main Jframe wait for my login Jframe to exit, before doing anything else?
Just use a modal dialog in stead of a frame, that way you cannot do anything else until it'is closed
see http://mindprod.com/jgloss/modal.html for explanation
and see http://www.java2s.com/Tutorial/Java/0240__Swing/ASimpleModalDialog.htm for code example
If you insist on using a JFrame, you could use a workaround by cover the other frame by a glassframe.. Not too a nice solution, I admit..
I agree that a modal dialog would be the best option here, but if I were to answer this question in it's more general form, say:
How do I make one JFrame wait for another JFrame?
I would say the easiest way to acheive this is by registering and firing event listeners.
In your "child" frame, register the "main" frame as an event listener.
In your "main" frame,
implement your choice of listener, e.g. ActionListener
in the method called by the listener, e.g. actionPerformed, code the logic that handles what happens upon each of the actions it can respond to in the "child" frame.
One can easily implement this to a ny number of situations, including the login scenario described in the question.
Use JModalFrame instead of JFrame.
What is the best practice for loading a slow JPanel for the first time?
I have a JButton, that, when clicked, loads a new JPanel that contains 52 JLabels with each containing an image (it's a card game).
But clicking this JButton freezes the GUI for around 5 seconds as the JPanel loads for the first time.
I know I should use a SwingWorker, but I tried initializing the JPanel, adding it to the JFrame, and calling its repaint() all within a SwingWorker, to no avail.
How should I properly handle loading the JPanel in the background so that the GUI doesn't freeze while it loads? (Side note, If I quit back to the main menu and click the button again, it takes a split second since it has already been loaded once)
Thanks!
I have the first JFrame and it works fine. When I push a button it is supposed to show a JProgressBar frame , but i get empty JFrame. I open it with
p = new Progress("1/3");
p.setMax(2);
p.setProgress(0, "Getting bytes...");
Anyone know why?
EDIT:
I am going to explain more detail(Because someone misunderstood and corrected my post in the wrong way) - On my main class i start the first JFrame:
new Crypt();
And in the Crypt class i have registered a button ActionListener. OnClick it opens a second JFrame But it is empty:
p = new Progress("1/3");
p.setMax(2);
p.setProgress(0, "Getting bytes...");
The Progress class
Screen shot
in the Crypt class i have registered a button ActionListener. OnClick it opens a second JFrame But it is empty
Code invoked from an Swing listener executes on the Event Dispatch Thread (EDT). The EDT is responsible for painting Swing components. Since your code is executing a long running task on the EDT y9ou are preventing Swing from painting the component until the task is finished.
You need to start a separate Thread for your long running task. Or better yet you should probably be using a SwingWorker. Read the section from the Swing tutorial on Concurrency in Swing which explains this in more detail and provides a working example of a SwingWorker.
I have two seperate JFrames but when i click the X in the topright of one, it will exit out of the other also. I have an "exit" button near the bottom to do setVisible(false), but i still have the tendency to use the x button. How would i make it so that it doesnt cancel out of the entire project?
Also, how would i make it so that the second JFrame locks out of the other JFrame untill the second JFrame is closed, like how a popup message works
Don't give your GUI two JFrames. The GUI ideally should have only one GUI. If a separate window is required, then make it a dialog such as a JDialog, and this won't happen.
Also, how would i make it so that the second JFrame locks out of the other JFrame untill the second JFrame is closed, like how a popup message works
You are perfectly describing the behavior of a modal JDialog or JOptionPane. Just use 'em.
Later we'll chat about using CardLayouts to swap views in a single GUI.
Edit, you state:
Im using Netbeans form editor to create them faster but I only see JFrame and JPanel. Can I edit them in Netbeans? I'd rather not do them through scratch Java
You've touched on another zealous belief of mine, that this is yet another reason not to use a code generator when learning a library as one can get too tied into the code generator, that it prevents one from learning the library. I strongly advise you to put aside your code-generation tool and create by hand, referring to the tutorials and API. Then later when you get more familiar with the library, sure use the tool. By the way, an answer to your direct question here is to gear your GUI's to create JPanels, and then use these JPanels where and how you want them -- in JFrames, or JDialogs, or JOptionPanes, or swapped in CardLayouts, or JTabbedPanes or nested in other JPanels,... etc...
You should be using a modal JDialog, not a second JFrame, because JDialogs provide certain functionality such as not adding another window bar to the taskbar, and automatically setting focus when the parent JFrame receives focus. Modal JDialogs prevent user input to the JFrame while it's open, useful for an "Are you sure you want to exit?" dialog, for example.
As for one JFrame exiting the other, you probably have their default close operation set to EXIT_ON_CLOSE. If you do this:
jframe.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
jframe.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
handleUserWantsToCloseWindow();
}
});
Then you can control what happens when the user wants to close, such as popping up a "Save Changes?" modal JDialog or "Are you sure you want to quit?" modal JDialog. Note that you have to manually dispose of the JFrame if you use this method.
In a JFrame, when I click on 'login', I pop up another Jframe which is the login window.
How do I make my main Jframe wait for my login Jframe to exit, before doing anything else?
Just use a modal dialog in stead of a frame, that way you cannot do anything else until it'is closed
see http://mindprod.com/jgloss/modal.html for explanation
and see http://www.java2s.com/Tutorial/Java/0240__Swing/ASimpleModalDialog.htm for code example
If you insist on using a JFrame, you could use a workaround by cover the other frame by a glassframe.. Not too a nice solution, I admit..
I agree that a modal dialog would be the best option here, but if I were to answer this question in it's more general form, say:
How do I make one JFrame wait for another JFrame?
I would say the easiest way to acheive this is by registering and firing event listeners.
In your "child" frame, register the "main" frame as an event listener.
In your "main" frame,
implement your choice of listener, e.g. ActionListener
in the method called by the listener, e.g. actionPerformed, code the logic that handles what happens upon each of the actions it can respond to in the "child" frame.
One can easily implement this to a ny number of situations, including the login scenario described in the question.
Use JModalFrame instead of JFrame.