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;
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) + "");
count++;
}
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));
}
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);
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