aspose file tools*
The moose likes Applets and the fly likes JApplets and page source Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "JApplets and page source" Watch "JApplets and page source" New topic
Author

JApplets and page source

Xavi Villalta
Greenhorn

Joined: Aug 02, 2010
Posts: 29
Okay so I''m having some problems with getting IO to work properly with an applet that I made.
I created a class that extends JPanel which gets the page source of a file that I uploaded to the internet (it simply says "This is a text file" on several lines).
And this is the class:

When I test the main method it works just as intended. A blue panel with a JTextArea with appropriate text.
However, when I try adding this to an applet, all that shows up is a blue box, there isn't a text area. I even tried manually resizing it just to make sure it wasn't a paint thing.
this is my applet code:

I can't tell what's wrong with it. Can anyone help?
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
If you import "java.awt.*", it's not a good idea to refer to a class called "Panel". In other words, rename that class.

Oh wait, you said it is actually blue. Hm ... start by deleting that "paint" method. That's an AWT thing that doesn't work in conjunction with Swing.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

You override paint but inside only call paintComponent. paint does a lot more than only call paintComponent. Either override paint and call super.paint, or preferably override paintComponent and call super.paintComponent.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Xavi Villalta
Greenhorn

Joined: Aug 02, 2010
Posts: 29
Lester Burnham wrote:If you import "java.awt.*", it's not a good idea to refer to a class called "Panel". In other words, rename that class.

Oh wait, you said it is actually blue. Hm ... start by deleting that "paint" method. That's an AWT thing that doesn't work in conjunction with Swing.


Okay I'll try that.
Hm. Still nothing.

And on a side note, does someone here know anything about MySQL databases and JDBC drivers?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

Xavi Villalta wrote:And on a side note, does someone here know anything about MySQL databases and JDBC drivers?

Please Use One Thread Per Question. You can post a new thread in our JDBC forum.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

By default, applets aren't allowed to open network connections anywhere except back to the server they were downloaded from. It's a security measure. If it were otherwise, then downloading an applet from behind a firewall would give the downloaded code access to all of your private network.

If you want to do this from an applet, you need to enter into the fun world of code signing -- and I say "fun" in the most sarcastic possible way. See here for an introduction:

http://faq.javaranch.com/java/HowCanAnAppletReadFilesOnTheLocalFileSystem


[Jess in Action][AskingGoodQuestions]
Xavi Villalta
Greenhorn

Joined: Aug 02, 2010
Posts: 29
By "Local File System" (in the link) does it mean the folder that the applet class was uploaded to? or to the files on the particular site visitor's computer?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

It means the file system of the computer where the applet is running. (Recall that the applet is downloaded from a server and runs in the browser of the "local" computer.)
Xavi Villalta
Greenhorn

Joined: Aug 02, 2010
Posts: 29
Oh okay that makes more sense.
So I made that temporary website that I referenced for the URL. Would I be able to directly refer to the file as new File("Text.txt"); because they are in the same folder on the server? Or would that not work since the code is only read until the browser executes it...
I need to be able to change the file dynamically in the complete project that I want to put in.

Sorry, but I have no experience in web stuff so it's all pretty confusing to me X)
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
No, applets can't use the File class when dealing with files on the server; that would work only for files that are available locally on the client machine where the applet is executed. Here's an example of an applet that reads a text file from the server where it's hosted.
Xavi Villalta
Greenhorn

Joined: Aug 02, 2010
Posts: 29
Yeah I know that you could do that just to read, but the thing is I also need to update the text file because my project has several different GUI's that are interconnected by various files.
Is there any way that I could get around this then? Would I have to make some sort of database?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

Database? Where did that idea come from? No, if you want your program (in this case the applet) to update a file on a different computer (in this case the server), you will have to have some process running on the server that allows that to happen. You can't just reach out and change files on other computers, if you think about it that would be a very bad thing to allow.

The process running on the server could be many things. It could be an FTP server which accepted uploads, or it could be a Java servlet, or a PHP script, and so on.

(I have lost count of the number of times I have seen forum threads where the real-life implications of applets have to be explained to people who thought an applet would be a good thing to use. Don't the textbooks which tell you how to use applets talk about this at all?)
Xavi Villalta
Greenhorn

Joined: Aug 02, 2010
Posts: 29
So once I made a java servlet and uploaded it, it would continuously run on the server?
Would this servlet send out and accept say an ArrayList of strings?

I think to move on I need to know the best way to approach my problem:
What I need is to be able to store strings to a file(or some other source of memory) and modify them and also add new strings. I also need to have separate memory locations for three different groups of names and another for tournament info.
It would also be great if separate brain classes would be able to access the info at the same time, but it is not a necessity.

If anyone can tell me the best way to approach this I can then seek help in the appropriate forum.
Thanks for all your help guys
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
It's a bit hard to say from the problem description, but it sounds as if a java.util.Properties object handled by a servlet may be sufficient. You should ensure that it's written to disk whenever it's changed to make sure the changes are persisted. If there will ever be more than one applet client you also need to pay attention to thread safety.
Xavi Villalta
Greenhorn

Joined: Aug 02, 2010
Posts: 29
Okay thank you for the help.
I'll be certain to try that out.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JApplets and page source