Click here to Skip to main content
Click here to Skip to main content

How to Create a Random Joke Generator in Java

, 14 May 2010
Rate this:
Please Sign up or sign in to vote.
How to create a Random Joke Generator in Java
01_small.jpg

Introduction

Designing apps with Java in Textpad is interesting nonetheless fun. Fast forwarding a bit, we've created a panel, placed listboxes and labels on the panel. And to have all these controls respond to mouse clicks, we have to put some code into it.
But this time, we have chosen to handle all events in the actionPerformed(ActionEvent event) method.

public void actionPerformed(ActionEvent event)
{
   if (event.getSource()==ExitButton)
   {
	   System.exit(0);
   }

 ...
}

The levent.getSource()==FilesList Event

The event.getSource()==FilesList event is called upon when the left button is pressed down while above this listBox.
Once the index has changed, this method is called upon and it represents the user changing from one textfile containing jokes to another.

if (event.getSource()==FilesList)
{
   JokeFile=FilesList.getSelectedIndex();
   lab2.setText("File = "+JokeFiles[FilesList.getSelectedIndex()].getName());
   showStatus(FilesList.getSelectedItem()+" " +LinesList.getSelectedItem()+ 
			"  "+ LinesList.getSelectedIndex());
   readLines(JokeFiles[FilesList.getSelectedIndex()].getName());
   JokeLine=0;
   //selectFile();
   selectLine();
}

The readLines(String fileName) Method

The readLines(String fileName) method is called upon to load jokes from a textfile to a string array.

public void readLines(String fileName)
{
      DataInputStream inStream;

      int count=0;
	  LinesList.removeAll();

         try {
               inStream = new DataInputStream(new FileInputStream("jokes\\"+fileName));

            while ( inStream.available() > 0 )
            {
				lines[count]=inStream.readLine();
				if (count==0)
				{
					JokeTitle.setText(lines[count]);
				    JokeDisplay.setText(lines[count]);
			    }
				LinesList.add((count+1) + "");
              // System.out.println( lines[count]);
               count++;
            } // End while.
            maxLines=count;
            lab3.setText("Joke Lines = "+maxLines);

            inStream.close();
         } catch ( java.io.IOException exception)
         {
           if ( exception instanceof FileNotFoundException)
           {
              System.out.println(
                 "A file called test.txt could not be found, \n" +
                 "or could not be opened.\n"                     +
                 "Please investigate and try again.");
           }
         }
} 

The event.getSource()==LinesList Event

The event.getSource()==LinesList event is called upon when the left button is pressed down while above this listBox.
Once the index has changed, this method is called upon and it represents the user changing from one line of joke to another.

if (event.getSource()==LinesList)
{
   JokeLine=LinesList.getSelectedIndex();
   showStatus(FilesList.getSelectedItem()+" " +
	LinesList.getSelectedItem()+ "  "+ LinesList.getSelectedIndex());
  JokeDisplay.setText(lines[LinesList.getSelectedIndex()]);
  lab4.setText("Current Line = "+(JokeLine+1));
  //JokeTitle.setText(lines[LinesList.getSelectedIndex()]);
}

The selectLine() Method

Finally the selectLine() method is called upon to display one line of joke from a string array on a label with the syntax:

JokeDisplay.setText(lines[LinesList.getSelectedIndex()]); 
public void selectLine()
{
   LinesList.select(JokeLine);
   //LinesList.makeVisible(JokeLine);
   JokeDisplay.setText(lines[LinesList.getSelectedIndex()]);
   lab4.setText("Current Line = "+(JokeLine+1));
   TimeDelay=lines[JokeLine].length()*TimeWait;
   lab5.setText("TimeDelay  = "+TimeDelay);
}

And that is how easy it is to create a Random Joke Generator in Java.

Thanks for reading.

History

  • 14th May, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

About me:
I attended programming School and I have a degree in three programming languages.
C/C++, Visual Basic and Java. So i know i can code. And there is a diploma hanging on my wall to prove it.
I am a professional, I've gotten paid to teach coding. I am roughly 20 years old and i have been a teacher's assistant in programming ,
i have held a lecture in Visual basic programming. I have also coached students in C++, Java and Visual basic.

In my spare time i do enjoy developing computer games, and i am developing a rather simple flight simulator game
in the c++ programming language using the openGL graphics libray.
 
I've written about a dozen small simple applications and games.

Comments and Discussions

 
GeneralVirus Alert PinmemberGenJerDan14-May-10 3:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 14 May 2010
Article Copyright 2010 by Software_Developer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid