Reading Files with Java
Without the ability to read files, your application would be severely limited. In
this article I show you; how to use the JfileChooser dialog box to select a file, how
to display the contents of the file in your applications window, and how to scroll the Window.
The first example shows you how to create a simple dialog box. When the user selects
About AboutApp in the applications menu, a small dialog appears displaying the
name of the application and the version number. If you want to learn how to create a
menu, read my previous article "Your First Java Menu".
For this example, I modify the code from MenuApp in may last article, adding an
AboutDialog class as shown below.
class AboutDialog extends JDialog
public AboutDialog(JFrame parent)
super(parent, "About AboutApp", true);
JPanel panel2 = new JPanel();
JLabel version = new JLabel("AboutApp Version 1");
JButton ok = new JButton("Ok");
public void actionPerformed(ActionEvent evt)
The only thing new here is the code for the JButton class, which is very similar to a
JMenuItem. When the user clicks the Ok button, the visibility of the dilaog
box is set to false.
You might be wondering what the call to super does. The AboutDialog class is
derived from JDialog, or as Java puts it, About Dialog extends JDialog. In
other words, the AboutDialog class inherits all the methods and attributes of the
JDialog class, and then adds some of its own. In this situation we need to call the
JDialog class constructor to provide it with some initialization information. Java
uses the Super() method to call a parent class's constructor.
Note: I'm writing this series of articles assuming that you are more interested in
getting some hands-on working code and seeing quick results rather than delving into
the details of object oriented programming.