Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Java / JavaSE / J2EE

How to Create a Random Joke Generator in Java

4.33/5 (2 votes)
14 May 2010CPOL1 min read 23.9K   1  
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.

Java
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.

Java
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.

Java
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.

Java
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:

Java
JokeDisplay.setText(lines[LinesList.getSelectedIndex()]); 
Java
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)