Click here to Skip to main content
13,447,415 members (40,174 online)
Click here to Skip to main content
Add your own
alternative version


10 bookmarked
Posted 14 May 2010

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


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)


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)
   lab2.setText("File = "+JokeFiles[FilesList.getSelectedIndex()].getName());
   showStatus(FilesList.getSelectedItem()+" " +LinesList.getSelectedItem()+ 
			"  "+ LinesList.getSelectedIndex());

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;

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

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

         } catch ( exception)
           if ( exception instanceof FileNotFoundException)
                 "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)
   showStatus(FilesList.getSelectedItem()+" " +
	LinesList.getSelectedItem()+ "  "+ LinesList.getSelectedIndex());
  lab4.setText("Current Line = "+(JokeLine+1));

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:

public void selectLine()
   lab4.setText("Current Line = "+(JokeLine+1));
   lab5.setText("TimeDelay  = "+TimeDelay);

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

Thanks for reading.


  • 14th May, 2010: Initial post


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

You may also be interested in...


Comments and Discussions

GeneralVirus Alert Pin
GenJerDan14-May-10 3:12
memberGenJerDan14-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180318.3 | Last Updated 14 May 2010
Article Copyright 2010 by Software_Developer
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid