Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to stop multiple windows from coming up  RSS feed

 
Rajib Ban
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am into Java for the last two months. Things that appear cakewalk for accomplished programmers are difficult to comprehend. So, patience is requested.
During the time spent in waiting to find a solution for my last post, I have changed and improved upon my earlier code. It works somewhat, but opens multiple windows, perhaps because it runs within a loop and employs static text window for a non-static integer variable.
There are solutions in the Stackoverflow Java, but they are beyond my current ability to comprehend. I need a little help. The issue was posted in the Eclipse forum but it is off-topic for the same.
Saved as Countdown.java and run from Eclipse with argument as small integer.

I further altered the codes into two parts, one with the main function and one without. Creates other problems.
But I will post and learn about them later.
In the meanwhile, a very small program on Dynamic text will be very nice.
 
Dave Tolls
Ranch Hand
Posts: 2823
30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create the Frame outside the timer.
Inside the timer just call repaint().

One thing, you probably ought to be using Swing rather than AWT.  So a JFrame instead of a Frame.
 
Rajib Ban
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sir,
Swing can wait, as I am just learning.


The error codes in the Eclipse console:
 
Dave Tolls
Ranch Hand
Posts: 2823
30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajib Ban wrote:Sir,
Swing can wait, as I am just learning.


OK, but AWT is hardly used anymore, so I just thought it would save you time by learning Swing.

As for your issue, I was thinking more of creating the frame inside the main() method, just not inside the timer.

Though it has highlighted an issue, in that you will also need to move 'i' out as well, otherwise your frame won't be able to see it to reference it.
 
Rajib Ban
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, if you advise, I will move over to swing.

I will move the frame within timer. But shows a hell lot of errors.

Okay, we follow and  get back.
 
Dave Tolls
Ranch Hand
Posts: 2823
30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I mean re: the positioning of your variables:

 
Rajib Ban
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sticking with AWT. Will solve what is begun, then move over to swing.
Correcting this code is beyond me. You have to support.


Errors from console:

The Console version of Java

 
Rajib Ban
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On, sorry! Did not see your post while I was editing and posted. I will do the needful and get back.
 
Rajib Ban
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:This is what I mean re: the positioning of your variables:



Sir, in your still earlier post you had advised,
Dave Tolls wrote:I was thinking more of creating the frame inside the main() method, just not inside the timer.

Though it has highlighted an issue, in that you will also need to move 'i' out as well, otherwise your frame won't be able to see it to reference it.
 
Dave Tolls
Ranch Hand
Posts: 2823
30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, that's what my code shows.

I took the declaration of 'i' out of the timer code and stuck it on line 3 of my code snippet.
I also put the declaration of the Frame out and placed it just after the declaration of 'i' since the paint() method needed access to 'i'.

There's no guarantee this would work, by the way.
I would, for example, create a class that handled my gui for me, probably with a display(int) method that the timer could call, but I suspect you would rather have this little piece working before handling classes.
 
Rajib Ban
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My query is: Does your code work as intended?
 
Dave Tolls
Ranch Hand
Posts: 2823
30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No idea.
I was just showing the structure you would need to aim for.
You may hit issues around changing 'i', I'm not sure.

Personally I would remove the GUI code into its own class and possibly provide a display(int) method, that the timer could then call, but there's no need for that if the above actually works.

Coding is often about trying things out.
 
Rajib Ban
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you haven't tested your code to check whether it works as intended, how come you are advising me to keep on changing the codes?
Please check and confirm! Don't worry, I won't cheat in learning, and won't ask you to divulge the codes to me.
I am continuing to keep posting the complete codes here, expecting you to check them!
 
Campbell Ritchie
Sheriff
Posts: 55332
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We do what we do here and no more. Remember we are not being paid to help here, and you cannot expect us to test code before posting it. Sometimes we do and sometimes we don't. We expect you to build your application, and you will have to test it for yourself. Many people here have lots of experience and can tell whether code is correct or not simply by reading it.
 
Dave Tolls
Ranch Hand
Posts: 2823
30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajib Ban wrote:If you haven't tested your code to check whether it works as intended, how come you are advising me to keep on changing the codes?


Because the basic structure of your code was what was at issue, at least until you can compile and execute it.

All I am doing is showing you the basic structure.  That should be plenty for you to move on.
 
Rajib Ban
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ha! Ha!
Then it has been a deterioration! ;-)
Earlier my codes used to run but only in an unwanted fashion. Now, it doesn't compile at all!
Is this not deterioration!? ;-)
 
Campbell Ritchie
Sheriff
Posts: 55332
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe not. It is good to find out your errors as soon as possible, so it is a useful skill to learn to use compile time errors.
 
Dave Tolls
Ranch Hand
Posts: 2823
30
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajib Ban wrote:Ha! Ha!
Then it has been a deterioration! ;-)
Earlier my codes used to run but only in an unwanted fashion. Now, it doesn't compile at all!
Is this not deterioration!? ;-)


As Campbell says, not necessarily.
If the basic structure is improved then you are moving forwards.  In your earlier set up it may have compiled, but it wasn't working, and minor changes could not possibly fix it.

This is the point where you post the full compilation error text and the associated code and I'm sure we can point out where the error is...or if there is a further structural change needed.
 
salvin francis
Bartender
Posts: 1562
29
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajib Ban wrote:If you haven't tested your code to check whether it works as intended, how come you are advising me to keep on changing the codes?

...
Rajib Ban wrote:I am continuing to keep posting the complete codes here, expecting you to check them!

Hi Rajib, coderanch is a huge community of volunteers who share their knowledge and help others. Since you have registered with this site, you too are a volunteer now
The first step towards learning java is to actually write code that compiles. I suggest that you do not start with swing/awt/any other api until you have mastered the concepts about OOPS. Specifically, how polymorphism works, what is abstraction, encapsulation, and so on. Writing coding is like learning a new foreign language. You cannot expect to write novels unless you know the grammar and syntax.

People here can help you with certain points in your code and provide suggestions and code snippets. But they you expect you to know the basics of the language and know where to add the code. A code snippet is like a jigsaw puzzle piece. You need to know where you have to "plug" that piece in so that your puzzle is complete. I think you would agree with me that no one is going to sit with you and solve the complete puzzle for you. We can help and give you pointers over what are the possible problems. Over time, you too can start helping others and giving them suggestions based on the knowledge you obtain.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!