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.
Welcome to Bucaro TecHelp!

Bucaro TecHelp
HTTPS Encryption not required because no account numbers or
personal information is ever requested or accepted by this site

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

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");
    panel2.add(version);

    JButton ok = new JButton("Ok");
    panel2.add(ok);
    getContentPane().add(panel2, "Center");

    ok.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent evt)
      {
         setVisible(false);
      }
    });

    setSize(150, 100);
  }
}

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.

RSS Feed RSS Feed


Follow Stephen Bucaro Follow @Stephen Bucaro


Java

Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2018 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268