If you just want a login dialog to wait for the user to enter user name and password, is there a reason not to just use a modal JDialog (after all, that's what it's there for...). If you really do want some arbitrary thread to wait for a signal to close the window/manipulate the GUI, then you need to do the waiting in the other thread, and then make that thread call SwingUtilities.invokeLater() with the actual GUI manipulation code.P.S.There are actually some GUI manipulation methods that it is safe to call from other threads, e.g. Calls that are "just setting a label" are often safe. But which calls are safe isn't terribly well-defined, so it's best just to avoid the issue in practice.
I guess you guys are right...All I want is the window to show up, a user selects his name from a list, types his password, and as soon as the password is confirmed, I wanted the window to just disappear. Im changing the frame to a dialog right now to test my luck – durrellp Mar 29 '09 at 20:29 If it isn't document thread-safe it isn't. In fact JDK7 removes some documentation that methods are thread-safe because they weren't (and couldn't).
– Tom Hawtin - tackline Mar 30 '09 at 11:58.
The Swing components should only be manipulated by the swing event dispatch thread. Class SwingUtilites has methods to submit tasks to the dispatch thread.
Ha - yes, that is a lesson hard learned. :) – javamonkey79 Mar 29 '09 at 18:11 I don't understand the answer, plz re-read what ive added as I think I used SwingUtilities right... – durrellp Mar 29 '09 at 18:37 your code looks really weird. I would advice you to go back and read the sun Swing Tutorials and start from scratch again.
– willcodejavaforfood Mar 29 '09 at 19:15 Not that I don't appreciate others comments,I know how to program d00d, looking for solutions not to start from scratch with a project that has been going on since September. I also use NetBeans so any nasty, weird Swing settings should be set already... – durrellp Mar 29 '09 at 19:32 Well that would be the solution as they would explain how to open up frames/dialogs. All that waiting/sleeping looks rather dodgy.
Have you tried using a JDialog instead of a JFrame for these 'temp' windows? – willcodejavaforfood Mar 29 '09 at 19:50.
It is difficult to diagnose your problem. I'm not sure what you're trying to do with the wait methods, but I recommend leaving wait/notify alone. This code has two frames - when you create a second frame, the first is hidden until you close it.
Public class SwapFrames { private JFrame frame; private JFrame createMainFrame() { JButton openOtherFrameButton = new JButton( "Show other frame"); frame = new JFrame(); frame. SetDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); contentPane.
SetLayout(new FlowLayout()); contentPane. Add(openOtherFrameButton); frame.pack(); openOtherFrameButton . AddActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { onClickOpenOtherFrame(); } }); return frame; } private void onClickOpenOtherFrame() { frame.
SetVisible(false); JFrame otherFrame = new JFrame(); otherFrame . SetDefaultCloseOperation(JFrame. DISPOSE_ON_CLOSE); otherFrame.
SetContentPane(new JLabel( "Close this to make other frame reappear. ")); otherFrame.pack(); otherFrame. SetVisible(true); otherFrame.
AddWindowListener(new WindowAdapter() { @Override public void windowClosed(WindowEvent e) { frame. SetVisible(true); } }); } public static void main(String args) { JFrame frame = new SwapFrames().createMainFrame(); frame. SetVisible(true); } } Because I don't see any evidence of them in your code, I'm going to suggest you read up on using event listeners rather than trying to "wait" for code to finish.It isn't entirely clear what you're trying to achieve, but you might be better off with a modal dialog: public class DialogDemo { public JFrame createApplicationFrame() { JButton openDialogButton = new JButton("Open Dialog"); final JFrame frame = new JFrame(); frame.
SetDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); Container container = frame.getContentPane(); container. SetLayout(new FlowLayout()); container.
Add(openDialogButton); frame.pack(); openDialogButton . AddActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { onOpenDialog(frame); } }); return frame; } private void onOpenDialog(JFrame frame) { JDialog dialog = createDialog(frame); dialog. SetVisible(true); } private JDialog createDialog(JFrame parent) { JButton closeDialogButton = new JButton("Close"); boolean modal = true; final JDialog dialog = new JDialog(parent, modal); dialog .
SetDefaultCloseOperation(JDialog. DISPOSE_ON_CLOSE); Container container = dialog.getContentPane(); container. Add(closeDialogButton); dialog.pack(); dialog.
SetLocationRelativeTo(parent); closeDialogButton . AddActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { dialog. SetVisible(false); } }); return dialog; } public static void main(String args) { new DialogDemo().
CreateApplicationFrame(). SetVisible( true); } }.
How about doing simply: //This method is called once a user presses the "first" login button on the main GUI public void loginPopUpThread() { SwingUtilities. InvokeLater(new Runnable() { public void run() { loginPopUpFrame.pack(); loginPopUpFrame. SetVisible(true); } }; } //This is called when the "second" loginB is pressed and the password is correct... public void notifyPopUp() { SwingUtilities.
InvokeLater(new Runnable() { public void run() { loginPopUpFrame. SetVisible(false); } }; }.
What you really want to be using is a modal JDialog. Note, bits of this are left out. It's your homework/project.
Public void actionPerformed(ActionEvent e) { // User clicked the login button SwingUtilities. InvokeLater(new Runnable() { public void run() { LoginDialog ld = new LoginDialog(); // Will block ld. SetVisible(true); } }); } public class LoginDialog extends JDialog { public LoginDialog() { super((Frame)null, "Login Dialog", true); // create buttons/labels/components, add listeners, etc } public void actionPerformed(ActionEvent e) { // user probably clicked login // valid their info if(validUser) { // This will release the modality of the JDialog and free up the rest of the app setVisible(false); dispose(); } else { // bad user!
Scold them angrily, a frowny face will do } } }.
I've changed to Dialogs and no more visibility problems...THANKS ALOT TO EVERYONE FOR ALL YOUR INPUT. Never really saw the difference between jFrame and jDialog so I arbitrarily just used frames. – durrellp Mar 30 '09 at 19:03.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.