77Views3Replies

Author Options:

More Java problems! Answered

I'm trying to make a simple program that will display a different text, depending on the day selected by the user. When I run it, I get the frame, the label, and the combobox. I don't see the textarea(source attached), but that might just be the way it is. However, the combobox is supposed to determine the textarea's text with a Switch statement. But it doesn't do anything on any of the days. I'm pretty sure it just has something to do with how I'm implementing the textarea, as the rest of it is fairly straightforward.

Comments

The forums are retiring in 2021 and are now closed for new topics and comments.
0
andy
andy

10 years ago

As with the last problem I think your understanding of Java is still a little shaky. What I would suggest is using the excellent resources on the oracle site (links below).

There are many problems here, cheifly that the day.getSelected() does not "block". When a method "blocks" it will not execute until something happens. So following your code down, you initialise the jFrame by adding a layout manager and objects. (Incidentally, JTextArea isn't visible because it isn't in a scroll pane). Then you add options to the drop box. Then you set it visible. Then you imediatly get the selected day (which hasn't been selected yet, because the window has just loaded, so you actually get an empty string). Then you execute the switch case. The switch case goes to default because "" is translated to 0 by your implicit cast. Then you update the invisible jTextArea with "No day selected".

As you can see it is a bit of a medley of errors. I could correct it for you again but I don't think that would help you learn. I would strongly advise you to go through the tutorials listed below (this is how we learnt Java at uni). This would also allow you to grasp the underlying mechanics of Java, giving you a much better understanding of how to use all components and API's - even the ones you haven't come across yet.

The tutorial home page is here; it has links to each tutorial "trail" - trail is used to mean a sequence of pages. I would strongly recommend the first six "trails" under Trails Covering the Basics and then whatever else takes your fancy in the advanced section. Be aware it will take time and effort to learn the basics right, but once you have, solving problems like the one you have asked will seem trivial.

Regards,
Drew


0
metrogdor22
metrogdor22

Reply 10 years ago

Thanks again for the help, and the links. Actually I think "shaky" would even be stretching it.