• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Java Applet to display text in a colored oval

 
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have written a program to enter a name in a text box of an html file, get the name in a Servlet, search for the name in the database and inform the user whether or not the name is in the database via an applet. I'm trying to get the information from the applet to display in a colored oval, green w/white font if the name is in the database and red w/black font if the name is not in the database. I can get the information to display just not in the colored ovals and text that I want, any assistance is appreciated. I am including only the applet code, not the HTML or Servlet.
 
Marshal
Posts: 80735
485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you still using Applets?
 
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Betty,

Why are you using applets at all? Applets are pretty much dead and buried, and come with lots of security issues. I would recommend using something different to display the outcome, such as JavaScript.
 
Betty Christiansen
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well this is for a class assignment, so there's that
 
Sheriff
Posts: 28407
101
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to say that I'm not at all impressed by your course then. However it's not your fault that you are being taught in such a way, you just have to deal with it. So... could you tell us what you expected that code to do, and what it did instead? It would also help if we had some idea of what was in that database table.
 
Betty Christiansen
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I'm not impressed with this course either...enough said about that. So the idea behind this program is to enter a name in a text box of an html file, get the name in a Servlet, search for the name in the database and inform the user whether or not the name is in the database.

The database is MySQL and consists of two tables; tableA has three names entered into it via insert when the table was created, John Doe, Jane Doe, and David Paterson. TableB is used for the name being searched (entered in the text field of the HTML). If the name is found a question mark is inserted followed by the name. If it is not found an exclamation mark followed by the name. For example the name: David Doe is not in the above table therefore !David Doe is inserted in tableB. But for John Doe ?John Doe is inserted in tableB. The applet is using this table (tableB) to display a message telling the name is (or is not) in the table.

It's fairly convoluted. You enter a name in the HTML text field and click submit; that takes you to a servlet that displays the message/link "Click here to see the applet". When you click the link it displays another page that says, for example, "John Doe is in the table".

I have that all working correctly. Now the last step that I can't seem to get to work is to have that applet message "John Doe is in the table" display in a green oval w/white text, or if the name is not one of those three in the table the message should display in a red oval with black text. It just continues to display as regular text on a plain white background.




 
Paul Clapham
Sheriff
Posts: 28407
101
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code doesn't use table A at all, it just looks at the single record in table B, decides it's in the table, and behaves accordingly. But there's this code:



I'm surprised that even compiles. Since you don't have any braces (the { and } characters), the scope of the "if" is only the line below it and nothing else. And so the "else" doesn't have any matching "if" before it and the compiler should complain about it. My guess is that you wanted to have braces like this:



But even if you did that, the record you read from table B always has a ? at the beginning so you'll always see the "if" block being run and the "else" block never being run.
 
Betty Christiansen
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the help with the braces; I continually misplace those or omit them altogether, and you were right it wasn't compiling, I just couldn't figure out what I had left off. Regarding tableA it's actually part of the Servlet program, so all that works okay and the program recognizes either one of the three names in tableA or a new name and gives the correct response. However even with the braces in the correct spot I'm not getting the answer displayed as I want it. Thank you for the help, I guess I need to approach the professor (which is a whole other story)!
 
Betty Christiansen
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I am told that is not how painting should be done EVER. Instead, I should have some method that can be called which builds a list of the results and then I should attempt to draw those results in the paint method. I should also be calling super.paint. I'm afraid as a beginner I'm not sure where to start with this advice. I already have the paint method, so do I need another method within that which creates a list of list of DB results? I'm only checking one name at a time so I'm not sure what that even means.
 
Paul Clapham
Sheriff
Posts: 28407
101
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's true, your posted code has numerous deficiencies. I preferred to get the basic problems sorted out before cleaning it up.

So, you're writing an applet. Have you learned about the applet's life cycle? If not, then have a look at the Oracle tutorial which explains it: Life Cycle of an Applet. That should help you extract some code which shouldn't be in your paint() method. Operations which only need to be done once... yes, you should put them in a different method than the paint() method, which is called repeatedly at times which you can't control. I'd suggest you fix that first.
 
Betty Christiansen
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for that. I guess my understanding is flawed. I thought on an applet init was to set up the display, like background and foreground colors etc. and then if you wanted to draw on the applet you used paint. I apologize for the seemingly stupid questions but I wrote a little stand-alone applet to see if it would display the text the way I wanted and that worked, so I thought I could transfer that to my program. Apparently I thought wrong and just don't know where to place it now. This is the very first Java class I've taken and the only other programming or code writing I've done is HTML/CSS, so I'm way behind the curve here.

This is the code that worked for my stand-alone applet:
 
Paul Clapham
Sheriff
Posts: 28407
101
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Betty Christiansen wrote:I thought on an applet init was to set up the display, like background and foreground colors etc. and then if you wanted to draw on the applet you used paint.



Yes, that's exactly correct. Or not quite "exactly"... the init method is to set up everything that the applet is going to need, not just the displays. And yes, when you want to draw on the applet you use the paint method. But the paint method should only draw on the applet, nothing else.

They probably didn't tell you that the paint method will be called whenever the applet-viewer code decides that it needs to redraw the applet. So it's called the first time the applet is drawn. Then if you minimize the browser and then restore it, the paint method is called again. And if you maximize some other application in front of the browser and then minimize that application, the paint method is called again. That's why the paint method shouldn't be doing anything except drawing what you want drawn.
 
Betty Christiansen
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Makes sense and no that detail hadn't been provided in class. Thank you for all the assistance.
 
reply
    Bookmark Topic Watch Topic
  • New Topic