« Return to Thread: Building GUI based application

Re: Building GUI based application

by Sean Carrick :: Rate this Message:

Reply to Author | View in Thread

Muhammad,

To use the JFileChooser, you just need to right click on the button in
Design view and click on the Events...Actions...ActionPerformed
command.  This will place you into the Source view and create a skeleton
of the JButtonActionPerformed(ActionEvent evt) event handler method.

Inside of that event handler, place the following code:

  // Show a file browse dialog, set to show only folders, to allow the user
  // to visually locate where s/he would like to install the program.
  if ( System.getProperty("os.name").contains("Linux") ) {
    this.selectFile = new JFileChooser("/usr/local/");  // Default for Linux
  } else if ( System.getProperty("os.name").contains("Windows") ) {
    this.selectFile = new JFileChooser("C:\\Program Files\\"); //
Standard for Windows
  } // end of this.selectFile creation.
 
  // Only allow folders to be selected.
  this.selectFile.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
 
  // Set the title for this.selectFile.
  this.selectFile.setDialogTitle("Select Installation Path");
 
  // Show the selectFile modally.
  int choice = this.selectFile.showOpenDialog(this);
 
  if ( choice == JFileChooser.APPROVE_OPTION ) {
    this.txtInstallationPath.setText(
          this.selectFile.getSelectedFile().toString());
  }
 
  if ( System.getProperty("os.name").contains("Linux") ) {
    this.txtInstallationPath.setText(
          this.txtInstallationPath.getText() +
          "/loe");
  } else if ( System.getProperty("os.name").contains("Windows") ) {
    this.txtInstallationPath.setText(
          this.txtInstallationPath.getText() +
          "\\Law Office Essentials");
  }
                               
This is a snippet from a GUI installer I'm writing for one of my
projects.  A lot of what is going on is specific to my application, but
it gives you the gist of how to use the JFileChooser bean within a
JButton's event handler.

In the previous code, "selectFile" is a class-level private field of the
type JFileChooser.  I have numerous "browse" buttons (next to text
fields with an ellipsis (...) on them) to allow the user to visually
navigate to a location, instead of having to type it in the text
fields.  Therefore, I only wanted to create the one object, then I
initialize it in each browse button's action event handler.  This helps
to save on resources, as I don't have numerous JFileChooser objects
sucking up memory.  An alternative way of using the JFileChooser is to
access its members directly.  Personally, I find it clearer to do as I
have done, by creating an instance of the JFileChooser.  The
setFileSelectionMode allows you to limit what is allowed to be
selected.  In this instance, I want the user to only be able to choose
directories, so I gave it the JFileChooser.DIRECTORIES_ONLY option.  You
can limit or expand this mode as you desire through the JFileChooser's
enumeration.

For more information, see the JavaDoc for the Sun Java API, located here:
http://java.sun.com/javase/6/docs/api/

Cheers,

Sean Carrick
PekinSOFT Systems
www.pekinsoft.net

alee amin wrote:

> I am trying to build GUI based application in NetBeans 6. But i am
> unable to start through. I have used the online tutorials from
> netbeans sites but those are not helping me much. I am unable to bind
> some controls with classes (beans) like JFileChooser etc. Can someone
> guide me in detail
>
> --
> Muhammad Ali
> http://techboard.wordpress.com
> Software Engineer - E2ESP
> blackishbubble(at)gmail(dot)com

--

Cheers,

Sean Carrick
PekinSOFT Systems
sean@...
http://www.pekinsoft.net

----------------------------------
Proud to be 100% Microsoft free...
----------------------------------

 « Return to Thread: Building GUI based application