aspose file tools*
The moose likes Applets and the fly likes Making applet of java program for the first time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Making applet of java program for the first time" Watch "Making applet of java program for the first time" New topic
Author

Making applet of java program for the first time

Jonas Kazlauskas
Greenhorn

Joined: Jun 19, 2014
Posts: 12
Hi guys, this is my first post in this forum. I heard it is very good. Don't be rough on me cause I am a begginer. ;)
I have an idea of making a word cloud where text is read from .txt file and being presented onto the screen with most common words being written in bigger font. I wrote program in Java. It might be not sophisticated but working. I decided to make an applet of the same program.
The problem is that in initial java program(not applet) I had my graphics, readfile, sorting words methods in one class(if I remember correctly) AND NOW when I am trying to make an applet. From the examples on the net I found that graphics could/should be in constructor of class that extends JPanel. I am lost cause I don't know how to make GUI and readfile/calculations separately cause they are so connected. I know you are going criticize me for uploading too much code but it maybe the best way for you to understand what I am experiencing. Please, share your knowledge with me.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42595
    
  65
Welcome to JavaRanch. The amount of code is fine, but the big question is: Why an applet instead of an application? Applets can not use the local file system (as the code does in the "go" method), so it would need to load the file contents some other way - maybe via an URL from a server. But applets have fallen out of favor for security reasons, so I would question their use.


Ping & DNS - my free Android networking tools app
Jonas Kazlauskas
Greenhorn

Joined: Jun 19, 2014
Posts: 12
Thank you for such quick reply. Why I chose to use applet? My thought process in child's words: wanted to make word cloud and put in on website. I used applet approach cause I didn't know any other alternatives(using Java language). Are there any?? I predict these things nowdays are being programmed with JavaScript?
I am gonna use my WAMP server to try to load my .txt file -> thank you for that, Ulf.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42595
    
  65
Any heavy duty processing would be done on the server (possibly by a Java servlet, or a Java REST WS), and then accessed via a web page.

As regards reading a text file from a server in an applet, see the "HelloServerFile" example on http://www.coderanch.com/how-to/java/CodeBarnApplets.
Jonas Kazlauskas
Greenhorn

Joined: Jun 19, 2014
Posts: 12
Thank you for reply. I will take your advice for using other approach then applet in the future. I told myself I am gonna finish with my plan making the applet running and move on. But it steams that I found a dead end. I spent so much time trying to get it running but failed and want to ask for your help. Please, help me
I changed the reading method as advised. Here is the code:



It runs with 4 warnings:
1) The import java.applet is never used;
2) The serializable class WCURL does not declare a static final serialVersionUID field of type long;
3) The serializable class display does not declare a static final serialVersionUID field of type long;
4) Iterator is a raw type. References to generic type Iterator<E> should be parameterized.

I created WCURL.jar file through eclipse IDE.
I created index.php file:


I put files in WCURL folder which I put in my WAMP server's www folder(local host). (See picture) The file WCURL.jar is in archive directory. And what I get is (see second picture). It should be filled with words from .txt file which is not empty. Any ideas? So lost...


[Thumbnail for Applet.png]

[Thumbnail for Files on www.png]

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42595
    
  65
The first step in debugging applets is to open the Java Console. If there are any exceptions or error messages, that's where you'll find them.

The jar file is probably not found - remove the "codebase" attribute, and put the jar file in the same directory as PHP file.

As to the warnings, unused imports can simply be deleted, and you can ignore the serialization warnings. As to the generics stuff ... well, let's get the applet working first :-)
Jonas Kazlauskas
Greenhorn

Joined: Jun 19, 2014
Posts: 12
I don't get any exceptions or errors. Just the warnings I posted before. I removed "codebase" attribute and moved my jar file from archive dir to where my index.php and MyText.txt is. Still no good. Still getting the same view as in the picture I attached in my last post. I tried to change browsers - no effect. Looks like the file reading is done(see the picture).
Maybe the problem is in the bit of:?


[Thumbnail for Picture02.png]

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42595
    
  65
You can add lots of logging to the countWords method. It is called, after all, but doesn't seem to do the right thing. So printing out lots of stuff should help you narrow down the problem.
Jonas Kazlauskas
Greenhorn

Joined: Jun 19, 2014
Posts: 12
OK. After adding:


In console I get:
Label created javax.swing.JLabel[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,
maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,
horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=cats,verticalAlignment=CENTER,verticalTextPosition=CENTER]
Label created javax.swing.JLabel[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,
maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,
horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=cats,verticalAlignment=CENTER,verticalTextPosition=CENTER]
....

So I come to conclusion that the casting is the problem. I wonder if I can somehow extract "text" attribute or is it better to try to get back before I create an Array of Objects and try somehow to get my desired string from LinkedHashSet?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42595
    
  65
So I come to conclusion that the casting is the problem.

Why? I don't see any issue there.

I would use a specific layout, though - I'm not sure what the default layout is, you might be adding all labels on top of one another. Or use a TextArea, and add lines of text to it (unless the JLabels have some specific function).
Jonas Kazlauskas
Greenhorn

Joined: Jun 19, 2014
Posts: 12
A minute after I submitted last post I figured out that it's not a casting issue but since you, Ulf, ar so great and fast in helping us - I didn't want to edit. The great news is that i got it running by (thanks to you) changing the layout

and


Now I need to play a bit with layout to make it look decent at least. I thank you very much for your help!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Making applet of java program for the first time