-1

I have a fairly simple Java Application I created with JFrames. There is more than one JFrame in my program. I have a menu launching at the beginning of the program. After going through the menu, I have the menu JFrame disposed.

The main JFrame has a menu button that should launch the exact same menu at a later time. However, when you launch the menu from inside of the ActionListener (when you press the menu button), the JFrame doesn't launch properly. None of the components show up and colors are off.

However, when the menu is launched from outside of the ActionListener, the JFrame shows up perfectly. I have tried multiple methods to fix this, but none of them have worked.

My full program code is available by clicking here.

  • The main class is "LetsMultiply5.java". This class also sets up the ActionListener.
  • The JFrame causing the problem is "MenuWindow.java".
  • "LetsMultiply5.java" calls the "Booter.java" class, which then calls the "MenuWindow.java".
  • "MainWindow.java" is the JFrame that has the "Menu" button.
  • For proof, "SpeedModer.java" calls the menu window after it has been disposed, and works.

================================EDIT================================

Also, I'd like to let you know that I realize my code is a little bit messy. I am not sure how else to write the code for this program.

I am forced to use Thread.sleep(x); because the Swing timers aren't what I am looking for. The Swing timers activate an ActionListener when the timer goes off. I need a system that will wait a second before continuing on with the code.

I realize that the while (repeater==0) loop with ActionListeners inside of it seems crazy, but that was the only way I could get it to work. If I put a single ActionListener and just had the while loop do no code inside of it, nothing happens when I press the button.

I would, as MadProgrammer mentioned:

Advice: Scrap your current approach and start again.

However, the way that I have my program currently coded is the only way that I know how to get what I need to do done. I read the tutorials, but still don't know how to improve the code in the way that you told me.

I thank everyone for trying to tell me to improve my bad "Java grammar", but as far as I am concerned, I am not going to continue this program for the next 20 years and make my millions off of it.

I have looked at the Swing timers before and I understand the whole new Timer(speed, this); concept, but I don't understand how this would make my code any better.

If anyone would like to show me how to fix my ActionListeners or Thread.sleep(x); lines, please tell me. Thank you.

Nicholas
  • 15
  • 1
  • 9
  • You seem to have a misunderstanding of how UI's work, they do not work like console apps, that is, in a linear fashion, they are event driven. That is, something happens (at some point in time) and then you respond to it. You `while (repeat == 0)` loop is adding a never ending number of `ActionListener`s to the `MainWindow.submit` button, this is just nuts. `Thread.sleep` is a horrendously bad idea within a single threaded environment like Swing (and a really bad idea in a GUI), even if you're not blocking the EDT, you can't possibly maintaining the timing between the two threads – MadProgrammer Aug 11 '15 at 02:20
  • `static` IS NOT a cross object communication mechanism and should NOT be used this way. You run the risk of some other part of your program creating a new reference to the object and suddenly you have lost the connection to the component that is on the UI. – MadProgrammer Aug 11 '15 at 02:21
  • Advice: Scrap your current approach and start again. Have a look at and understand [Creating a GUI With JFC/Swing](http://docs.oracle.com/javase/tutorial/uiswing/), [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) and [How to use Swing Timers](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) – MadProgrammer Aug 11 '15 at 02:22
  • The problem is, you're coding yourself into a deep dark hole, from which you will find it very, very difficult to get out of and which will increasingly generate more problems, each which is more difficult to solve. Instead of trying to continue, scrap what you have and start afresh. Understand how the GUI framework works and how you can work with it to produce your desired results. Start with small steps, generating proof of concept and slowly build the complexity around it. Right now, you are butting your head against a brick wall and the wall will win – MadProgrammer Aug 11 '15 at 02:29
  • One of the important skills as a developer you need is knowing when to walk away from a approach and try something different. Too many projects have spiraled into chaos and unmanageable code base because people didn't want to walk away from the investment that they had put into solving the problem, when they could have made better decisions and generate a better working API if only they had the courage to walk away from the current path they were trying to follow. Personally, even when I get something working, I'm interested in knowing other ways to do it – MadProgrammer Aug 11 '15 at 02:32
  • @MadProgrammer I understand that the way I am programming may not be proper, and I thank you for trying to explain that I should take another approach, but at the moment, I do not wish to start over. Up to the point when I added the menu button, my program worked perfectly and just the way that I wanted it to work. I realize it wasn't proper, but I was satisfied. I want a way to add a menu any way I can, even an improper way. – Nicholas Aug 11 '15 at 02:41
  • I may have very improper methods of getting this program done, but I like the way that it is, and all that I want to change right now is the menu. – Nicholas Aug 11 '15 at 02:44
  • And what if you can't? You're working against the API, instead of with it. Please don't take anything I say as been criticism, but one of the most important lessons you can learn is, always work with the API – MadProgrammer Aug 11 '15 at 02:58
  • I don't think there is any way you could possible get this to work the way you think it should. Of course, you are welcome to continue trying, but as I said, a better solution would be to learn how the API is suppose to be used and use to your advantage, but that choice is you own, I'm only trying to provide you with the experience of 30+ years of programming and 9+ languages and countless APIs – MadProgrammer Aug 11 '15 at 03:02
  • I tried to "work with the API", but everything I tried didn't work. That is the reason that I created this question. – Nicholas Aug 11 '15 at 14:37
  • All your while loops and Thread.sleeps are either causing you issues or will cause you issues, these are country to how the API works – MadProgrammer Aug 11 '15 at 21:04

1 Answers1

3

You're blocking the Event Dispatching Thread with Thread.sleep(3000); - Don't do this, it will prevent the UI from been painted

See Concurrency in Swing for more details about the problem and How to use Swing Timers for a possible solution

You may also want to consider having a look at The Use of Multiple JFrames, Good/Bad Practice? and consider using CardLayout or JTabbedPane

If you need to block the user temporarily (to display a error message or gather important details), consider using a modal JDialog. See How to Make Dialogs for more details

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Really sorry, but during my simplifying of the code, I must have made that mistake. I am pretty sure that that was not the mistake I made in this code. Also, I saw the article about multiple JFrames already. I think that I like the idea of multiple JFrames better. – Nicholas Aug 11 '15 at 02:03
  • @NickBIncorporated Having looked over your linked code, I would say I'm not far of the mark – MadProgrammer Aug 11 '15 at 02:23