Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / Java / Java SE / J2EE

How to Create a Random Joke Generator in Java

Rate me:
Please Sign up or sign in to vote.
4.33/5 (2 votes)
14 May 2010CPOL1 min read 23.1K   10   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)


Written By
Sweden Sweden
About me:
I attended programming college and I have a degree in three most famous and successful 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 am paid tons of cash to teach or do software development. I am roughly 30 years old .

I hold lectures in 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 hundreds of thousands of code syntax lines for small simple applications and games.

Comments and Discussions

 
GeneralVirus Alert Pin
GenJerDan14-May-10 3:12
GenJerDan14-May-10 3:12 

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

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