Question about Applets

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 821 Views - Last Post: 10 July 2011 - 12:42 AM Rate Topic: -----

#1 Steve Shields  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 06-July 11

Question about Applets

Posted 08 July 2011 - 02:13 AM

I have a program that i would like to convert to an applett. It uses a loop to create a menu the user can choose from, and scanner to get the input. I looked up the Applett tutorial by vortex, and it doesnt seem that you can use System.out.println(). From what I could gather it seems that you have to draw the string to the applett using screen2D.drawString(). I was hoping to have it appear pretty much as a command line interface on the site itself with my menu still functioning the same. Is there anyway to do this? If not how much do I have to change? Can someone point me in the right direction? Thanks for your time!

Is This A Good Question/Topic? 0
  • +

Replies To: Question about Applets

#2 4D1  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 225
  • Joined: 22-October 09

Re: Question about Applets

Posted 08 July 2011 - 02:33 AM

By the sounds of things you are trying to create an Applet that mimics a Java Application run from the command line? I'm not the greatest expert in Java but I dont think you can do this with an applet, you would most likely have to use text boxes and style/format the text boxes and the text to look and feel like a command line interface.. Obviously a Java applet runs in a browser where as a non GUI Java application runs from the command line which provides the interface.

It wouldn't be overly difficult to do, but would be more involved than a simple Java console app. Applets provide a few overridable methods that you can use to do your drawing etc, the only difficulty would be styling the applet like a console app

This post has been edited by 4D1: 08 July 2011 - 02:38 AM

Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2558
  • View blog
  • Posts: 10,681
  • Joined: 20-September 08

Re: Question about Applets

Posted 08 July 2011 - 03:35 AM

4D1 is right. You can add a JTextArea to the applet and append to it
Was This Post Helpful? 0
  • +
  • -

#4 Steve Shields  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 06-July 11

Re: Question about Applets

Posted 08 July 2011 - 03:47 AM

So if I were to add the JTextArea, it would peform similar to a console(as in i can write to it using append)? would the user be able to type in that area or would i have to use a seperate method to recieve the input?
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2558
  • View blog
  • Posts: 10,681
  • Joined: 20-September 08

Re: Question about Applets

Posted 08 July 2011 - 03:49 AM

It sounds to me like you should really be using dialogs etc. rather than simply mimicking an application command line
Was This Post Helpful? 0
  • +
  • -

#6 Steve Shields  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 06-July 11

Re: Question about Applets

Posted 08 July 2011 - 04:03 AM

View Postg00se, on 08 July 2011 - 03:49 AM, said:

It sounds to me like you should really be using dialogs etc. rather than simply mimicking an application command line


What is that? I have not ever heard of it. Is that an easier way to go about doing it?
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2558
  • View blog
  • Posts: 10,681
  • Joined: 20-September 08

Re: Question about Applets

Posted 08 July 2011 - 04:16 AM

Get the input from the user using, say, JOptionPane.showInputDialog

http://download.orac...a.awt.Component, java.lang.Object)
Was This Post Helpful? 0
  • +
  • -

#8 4D1  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 225
  • Joined: 22-October 09

Re: Question about Applets

Posted 08 July 2011 - 04:35 AM

I have not used a JDialog box before, but by the looks of the link goose posted it is a pop out box like a message box that can be used to get input from the user, if I am right, it wont give you the true look and feel of a command line, but would avoid you having to handle alot of things yourself. If you wanted to truly mimic a command line you would have to create a text box, style it like a command line including the font and colour etc, then you would have to handle events pressing enter etc... You wouldn't be able to use scanner you would have to read in the contents of the text box and manually search for what the user might have entered, in other words compare what was there bfore the user pressed enter to what was there after, and then obviously take appropriate action depending on what was typed. Can you elaborate on what exactly it is you want to do/acheive as we may be able to advise better if we know exactly what you want

This post has been edited by 4D1: 08 July 2011 - 04:38 AM

Was This Post Helpful? 0
  • +
  • -

#9 Steve Shields  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 06-July 11

Re: Question about Applets

Posted 08 July 2011 - 04:45 AM

It started off as wanting to mimic a command line, but thats starting to make my head spin a bit. Basically i want a menu enabling the user to chose between three options (view object, modify object/create a new one, and exit). whatever is to be displayed by the first two choices will replace the text currently there (with an option to go back to the main menu included). I have the code to do what the choice is, I just dont know how to display the menu and get the input from the user in applett form.
Was This Post Helpful? 0
  • +
  • -

#10 4D1  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 225
  • Joined: 22-October 09

Re: Question about Applets

Posted 08 July 2011 - 05:56 AM

ok, the easiest I think would be to create an applet with 3 buttons and a text box, New Object, View Object and Exit, and append accordingly to the text box, are you wanting to fill an object aswell? Do you want the user to type things in, to be stored in the object? If so using a Dialog box as goose suggested could be ideal, when the user clicks "new" a dialogue could appear requesting all relevant data, once they click ok the object is created with the data, then obviously view object would just output the contents to text box/area.

package javaapplication1;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JDialog;
import java.awt.FlowLayout;

public class JavaApplication1 extends JFrame implements ActionListener{

    private JButton[] myButtons;
    private Object myCustomObject;
    private JDialog dialog;
    private JTextArea text;
    
    
    public static void main(String[] args) {
        new JavaApplication1();
    }
    
    public JavaApplication1(){   
        super("My Frame");
        text = new JTextArea("Welcome", 10, 25);
        text.setAutoscrolls(true);
        myButtons = new JButton[3];
        myButtons[0] = new JButton("New Object");
        myButtons[0].addActionListener(this);
        myButtons[1] = new JButton("View Object");
        myButtons[1].addActionListener(this);
        myButtons[2] = new JButton("Exit");
        myButtons[2].addActionListener(this);
        this.setSize(300, 300);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLayout(new FlowLayout());
        this.add(myButtons[0]);
        this.add(myButtons[1]);
        this.add(myButtons[2]);
        this.add(text);
        //this.add
        this.setVisible(true);
    }
     
    @Override
    public void actionPerformed(ActionEvent e)
    {
        
        if(e.getSource() == myButtons[0])//New Object
        {
            //Dialog box code here
        }
        else if(e.getSource() == myButtons[1])//View Object
        {
            text.append("\n\nMy Object = Some stuff");
        }
        else if(e.getSource() == myButtons[2])//Exit
        {
            System.exit(0);
        }
    }
}



That should be easy enough to convert to an applet and should give you a starting point for adding a dialogue box etc

This post has been edited by 4D1: 08 July 2011 - 08:09 AM

Was This Post Helpful? 1
  • +
  • -

#11 Steve Shields  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 06-July 11

Re: Question about Applets

Posted 08 July 2011 - 09:39 PM

ok this is wonderful! is so much simpler than what i was trying to do haha. Now then, quick noob question. Can i use this form as my applett and have it use my other class to get all its objects and methods from as if it were a normal class?
Was This Post Helpful? 0
  • +
  • -

#12 Steve Shields  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 06-July 11

Re: Question about Applets

Posted 09 July 2011 - 04:48 AM

OK that last question i answered myself, it was a dumb question haha. ive run into,i hope, my last problem. i cant find a good reference for jdialog. my internet connection is dreadfully slow out to sea, and ive bene looking for a few hours now. is there anychance someone with better internet can either tell me how to use jdialog or post a link to a tutorial? thanks for your help thus far.
Was This Post Helpful? 0
  • +
  • -

#13 coder364  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-July 11

Re: Question about Applets

Posted 09 July 2011 - 05:07 AM

If you are considering to make it as an application, u should better try using a JFrame...
Here's a tutorial on it ...

http://download.orac...ents/frame.html
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10194
  • View blog
  • Posts: 37,644
  • Joined: 27-December 08

Re: Question about Applets

Posted 09 July 2011 - 07:08 AM

The documentation is a great place to start off. Also, remember that GUI programming is event-driven. I have a tutorial on Event-Driven Programming, which will hopefully help clarify these concepts.

Hope this helps some. :)
Was This Post Helpful? 0
  • +
  • -

#15 Steve Shields  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 06-July 11

Re: Question about Applets

Posted 09 July 2011 - 09:34 PM

View Postmacosxnerd101, on 09 July 2011 - 07:08 AM, said:

The documentation is a great place to start off. Also, remember that GUI programming is event-driven. I have a tutorial on Event-Driven Programming, which will hopefully help clarify these concepts.

Hope this helps some. :)


I hadn't seen your turoial yet, it was nice to see a concise example of frames, most of the ones i had found were rather lengthy and, apparently, over complicated. i had found the page about jdialog before, but if im reading it correctly i have to have two classes in order to use it? the demo dialog and the custom dialog. is it really that complicated to get user input in a GUI? if it is then thats what i'll do, but i was hoping there was a more...compact way of doing it. where maybe all i would need is a class for my frame that has the frame and the jdialog code? and then have my applett call that as its init() method.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2