Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: JApplets and JFileUploader

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Thumbs up JApplets and JFileUploader

    I know some things about JApplet, but how do you make an applet that will let you upload files, and open, or at least download, files.

    Also, how do you get it to paint in more than once color?



  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,260
    Thanks
    176
    Thanked 818 Times in 761 Posts
    Blog Entries
    5

    Default Re: JApplets and JFileUploader

    For file navigation to save or select, see How to Use File Choosers. For applets, doing so requires your applet be signed. See the following: Signed Applets - How to sign an applet (and get it to work)

    Also, how do you get it to paint in more than once color?
    Vague answers (or none at all) result from vague questions. I suggest you start another thread with this question, containing a bit more information and code describing the problem, what you have, and what you wish to accomplish.
    Last edited by copeg; August 28th, 2010 at 10:06 PM.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: JApplets and JFileUploader

    In your paint method, set the color of the graphics object.

    public void paint(Graphics g)
    {
        g.setColor(Color.red); // sets the color to red
        g.setColor(new Color(255, 0, 0)); // also sets the color to red
    }

  4. #4
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: JApplets and JFileUploader

    In applets you use paint instead of paintComponenet?

  5. #5
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    489
    Thanks
    8
    Thanked 45 Times in 43 Posts

    Default Re: JApplets and JFileUploader

    In Applets you can override paint(...)
    In JApplets you can override paintComponent(...)

    Better than either is to override paintComponent(..) in a JPanel or JComponent and add that (or set as contentPane) in a JApplet. That way, you can re-use your JPanel/JComponent by adding the same JPanel to a JFrame/JDIalog or by passing it as the message parameter to a JOptionPane.

    db

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: JApplets and JFileUploader

    ^^ what Darryl said.

    Build your GUI off of a JPanel/JComponent, then you can simply add it to your applet if you want to make an Applet/JApplet, or you can set it to the content pane of your JFrame if you want a stand-alone Java application.

  7. #7
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: JApplets and JFileUploader

    I override a jpanel and add it to my JFrame. Thanks for the info.

  8. #8
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Question Re: JApplets and JFileUploader

    Quote Originally Posted by helloworld922 View Post
    In your paint method, set the color of the graphics object.

    public void paint(Graphics g)
    {
        g.setColor(Color.red); // sets the color to red
        g.setColor(new Color(255, 0, 0)); // also sets the color to red
    }
    I've seen them make an applet with shapes drawn in more than one color, but that used a switch statement and I'm not good at those, maybe I should have paid a bit more attention in class when that part was discussed. Anyway, if a break statement at line two will take it out of the switch structure even if a valid and true value was on line 3, do you use continue if you want it to check line 3 and so on?

    Also, maybe I'm just a but are FileChoosers pretty complicated, i.e. something someone who's only been doing GUIs for less than a year's worth, should be fiddling with, or are they pretty easy to get it to read and write and upload stuff? I got it to write part of an object in another class thing but part of that object wasn't Serializable. Also, if I'm going to change components, like update the text in a text area, or have the saved layout start out with the last used background color setting, do I have to remove the old ones and bring in the updated ones? You can't add an Object to a JPanel. How do you put back in the object that was made from readObject() and writeObject()? However, if the said Object is also a Component then it can be added. Is there a way to make the writeObject() or readObject() value made by writing the Component and then reading the Object back in a reference variable of Component? Yeah I need a try and catch block to deal with the possibility of a Class Cast Exception, but is it doable?

    JPanel panel = new JPanel();
    Component comp = new Component();
    panel.add(comp);
    JFileChooser chooser = new JFileChooser(JFileChooser.SAVE_DIALOG);
    File file = chooser.getSelectedFile();
    OutputStream os = new OutputStream(file);
    ObjectOutputStream oos = new ObjectOutputStream(os);
    Object obj = oos.writeObject(comp);
    panel.remove(comp);
    JFileChooser chooser2 = new JFileChooser(JFileChooser.LOAD_DIALOG);
    File file2 = chooser2.getSelectedFile();
    InputStream is = new InputStream(file2);
    ObjectInputStream ois = new ObjectInputStream(is);
    Object obj2 = ois.readObject();
     
    Component c;
     
    try (
    c = obj2;
    panel.add(c);
    }
     
    catch(ClassCastException cceRef)
    {
    System.out.println(cceRef.toString());
    }

    Also, it won't just be writing a Component itself, but actually a subclass like JTextArea, JButton, etc.

    That might change what happens so I thought I should mention it.