How to convert java application using some data from a text file to an applet

Posted on 2007-11-14
Medium Priority
Last Modified: 2013-11-23
I've just started learning java, so I really need detailed information to understand. I made a java application that supposed to be placed on the website. When I finished someone told me that I can't put there application. It has to be an applet. How can I convert my application to an applet ? This is a quiz, questions and answers are contained in a text file. I need to put it on the website and don't know how to do that. Can you help me with the code please ? This is the code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;      //to handle RandomAccessFile
import javax.swing.border.*;

public class Quizza1 implements ActionListener{
      static FrameLayout fm;
      static int count=0;
      JDialog dialog;
      JComboBox l1;

            public void actionPerformed(ActionEvent a)
            public Quizza1()
              Dimension screenDim =Toolkit.getDefaultToolkit().getScreenSize();
                  dialog=new JDialog();
                  dialog.setTitle("No Of Questions");
                  Container content=dialog.getContentPane();
                  dialog.setBounds((screenDim.width - 300) / 2,(screenDim.height - 100) / 2,300,100); // polozenie okienka wyb ilosci pytan
                  l1=new JComboBox();
                  JLabel label=new JLabel("Enter The Number Of Question/Session");
                  content.setLayout(new FlowLayout());
                  JButton button=new JButton("OK");

      public static void main(String args[]){

Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();                                    // ustala wymiary ekranu
            fm=new FrameLayout();                                                              
            fm.setSize(screenDim.width,screenDim.height);                                                           // rozmiar okna quizu

        Rectangle winDim = fm.getBounds();
        fm.setLocation((screenDim.width - winDim.width) / 2,
            (screenDim.height - winDim.height) / 2);
            new Quizza1();

                  Handles events on Closing the Window
            fm.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                JOptionPane op=new JOptionPane();
                  int aaa=op.showConfirmDialog(null,"DO U REALLY WANT TO EXIT","CONFIRM",JOptionPane.YES_NO_OPTION);      

            Creates the area where Questions are displayed

class FrameLayout extends JFrame {
//      JScrollPane jsp=new JScrollPane();
      int count;

      public void setQues(int a)
            QueNo.setText("Question No 1/"+count);
      JTextArea TQuestion=new JTextArea();
      String Question[][] =new String[50][7];
      boolean Answer[][]=new boolean[50][6];
      Label QueNo =new Label();
      int maxQue=1;
      private MenuBar mainMenu;
      private Menu MFile;
      private MenuItem IStart;
      JCheckBox JAnswer[]=new JCheckBox[10];
      JButton JNext,JPrev,JGrade, button;
      static int QuestionNo=1;
      Questions Ques=new Questions();
            setTitle("Marta's simple quiz");      //title of the frame.
            Container contentPane=getContentPane();


            TQuestion.setBackground(new Color(0,0,0));

                TQuestion.setFont(new Font("Seriff",Font.PLAIN,25));

            Panel Top=new Panel();

            Top.setBackground(new Color(0,0,0));
            Top.setLayout(new BorderLayout());
JButton button = new JButton();
button.setIcon(new ImageIcon("title.gif"));

            Top.add(QueNo,BorderLayout.WEST);            // bylo west
                Top.setFont(new Font("Seriff",Font.PLAIN,30)); // sama dorzucilam  
            TQuestion.setEditable(false);            //Questions are not Editable.
            JScrollPane jsp=new JScrollPane(TQuestion);            
            Top.add(jsp,BorderLayout.SOUTH);       // pokazuje nr pytania
            Panel center =new Panel();
            center.setLayout(new GridLayout(5,1)); // 5 pytan w jednym rzedzie

                        Creates the Options

            for(int i=0;i<5;i++){
                  JAnswer[i] =new JCheckBox();
                  JAnswer[i].addItemListener(new ItemList());
                  JAnswer[i].setBackground(new Color(225,221,217));
                  JAnswer[i].setFont(new Font("Seriff",Font.PLAIN,25)); // ust czcionki w odpowiedziach
                        Creates the Buttons

            Color fantastic=new Color(0,0,60); //belka ze strzalkami
            Panel bottom=new Panel();
            bottom.setLayout(new GridLayout(1,5));
            ImageIcon na=new ImageIcon("next111.gif");
            ImageIcon pa=new ImageIcon("prev111.gif");
                ImageIcon gr=new ImageIcon("grade111.gif");
                JNext=new JButton("Next",na);
                JPrev=new JButton("Previous",pa);
                JGrade =new JButton(gr);
//            center.setBackground(new Color(0,0,0));            
            ActionList a=new ActionList();
                  Creates the Order of the Questions

      void makeQueVis(int No){
            if(No>0 && No<51){
                  QueNo.setText("Question No " + No + "/"+count);
                        for(int i=0;i<5;i++){
                              else JAnswer[i].setVisible(false);
                        for(int i=0;i<5;i++){
                              else JAnswer[i].setVisible(false);

                  Event Handling for the Questions

class ActionList implements ActionListener{
      public void setCount(int aa)
            public void actionPerformed(ActionEvent a){
                              QuestionNo=QuestionNo+1;      //move to next question.
                              if(QuestionNo>=count) JNext.setEnabled(false);//questions are ended.
                              QuestionNo=QuestionNo-1;      //move to previous question,
                              if(QuestionNo<=1) JPrev.setEnabled(false);      //No questions .
                        Result fr=new Result(Quizza1.fm);      //calls Result class.

                        Check the Answers here
      class ItemList implements ItemListener{
            public void itemStateChanged(ItemEvent e)
            JCheckBox jc=(JCheckBox)e.getItem();
            for(int i=0;i<5;i++)


                        Generate Questions Here
class Questions {
      RandomAccessFile file;
      String Question[][] =new String[50][7];
                  file=new RandomAccessFile("sawal.txt","rw"); // zmienilam na plik html
            catch(IOException e){System.out.println(e);}

      String[][] getQuestions(){
            StringBuffer s1;
            for(int i=0;i<50;i++){
                  StringBuffer sb1=new StringBuffer();
                  b1: while(true){
                              String Temp=new String(file.readLine());
                                    int k;
                                          case 2:
                                                break b1;
                        s1=new StringBuffer(file.readLine());
                        if(new String(s1).equals("##-----Question-----##")){
                        else if(new String(s1).equals("##ans1##")){
                        else if(new String(s1).equals("##ans2##")){
                        else if(new String(s1).equals("##ans3##")){
                        else if(new String(s1).equals("##ans4##")){
                        else if(new String(s1).equals("##ans5##")){
                        else if(new String(s1).equals("##correct##")){
                              Question[i][0]=new String(sb1);
            catch(IOException e){System.out.println(e);}
            return Question;


                        Create the Result
class Result extends JDialog {
      Label TotalQue =new Label("Total Question");
      Label TotalAns =new Label("Correct Answer");
      Label TotalWrg =new Label("In-Correct Answer");
      Label Per =new Label("Percentage Of Marks");
      Label Grade =new Label("Grade");
      JButton jb=new JButton("Ok-End Test");
      int correct;
      Result(final FrameLayout fm){
            for(int i=0;i<fm.maxQue;i++){
                  StringBuffer s=new StringBuffer();
                  for(int j=0;j<5;j++){
                              s.append(j+1 + ",");
                  if(fm.Question[i][6].equals(new String(s))){
            Container content=getContentPane();
            Panel con=new Panel();
            con.setLayout(new GridLayout(5,2));
            con.add(new Label("" + fm.maxQue));
            con.add(new Label("" + correct));
            con.add(new Label((fm.maxQue-correct) + ""));
            int l=0;
            con.add(new Label(l+ " %"));
            if(l<71) con.add(new Label("Failed"));
            else con.add(new Label("Passed"));
            jb.addActionListener(new ActionListener(){
                  public void actionPerformed(ActionEvent a){
        Dimension screenDim =
        Rectangle winDim =getBounds();
        setLocation((screenDim.width - winDim.width) / 2,
            (screenDim.height - winDim.height) / 2);

            Handles the Review Option in the Main Window

 class Final extends JDialog{
      private JButton Question[] =new JButton[50];
      FrameLayout cl=new FrameLayout();
      class ActionList implements ActionListener{
                  int i;
                  public void actionPerformed(ActionEvent a){
                        String s=a.getActionCommand();

                        else s=s.substring(7,9);
                        int i=new Integer(s).intValue();
                  ActionList(int i){
      Final(final FrameLayout cl,int count){
            int j=0;
            final Container panel=getContentPane();
            Panel pane=new Panel();
                  pane.setLayout(new GridLayout(2,5));
            else if(cl.maxQue<20){
                  pane.setLayout(new GridLayout(4,5));
            else if(cl.maxQue<30){
                  pane.setLayout(new GridLayout(6,5));
            else if(cl.maxQue<50){
                  pane.setLayout(new GridLayout(8,5));
            for(int i=0;i<cl.maxQue;i++){
                  Question[i]=new JButton("Que No." + j +"/"+ count);
                  Question[i].addActionListener(new ActionList(i));
            Button ok=new Button("Ok");
            ok.addActionListener(new ActionListener(){
                  public void actionPerformed(ActionEvent a){
        Dimension screenDim =
        Rectangle winDim = getBounds();
        setLocation((screenDim.width - winDim.width) / 2,
            (screenDim.height - winDim.height) / 2);
            addWindowListener(new WindowAdapter(){
                        public void windowClosing(WindowEvent e){

*********** And this is fragment of the text file called Sawal.txt:
Which state has a climate suitable for growing citrus fruits?






Question by:Maribell
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 23

Accepted Solution

rama_krishna580 earned 2000 total points
ID: 20280411

Author Comment

ID: 20280569
Thanks for reply. My application reads a text file but the problem is that I don't know how to convert above application to an applet that it could be placed od the website.

Author Comment

ID: 20285689
Does anyone know how to put this application and a text file on the website? I really need help. I made a mistake that I created an application. This is for my assignment, I suppose to create an applet that has to be put on the website. Unfortunatelly, because I'm beginner, I didn't notice the difference between an applet and the application. Now I have to submit it by Friday and it's loo late to create a completely new applet. I don't want to fail. Can anyone help me, please ? :(

Expert Comment

ID: 20296489
Did you take the time to look at the links posted by rama?

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month11 days, 22 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question