Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Roll Your Own Swing-based XML Editor (Part III)-3 : Page 3


advertisement

Build the File System Access Components
There is often a need for your Java Swing applications to allow the user to access the file system through a graphical file system browser. Typically, this is a result of the user wanting to open or save a component or file. In our XmlEditor application, we want users to be able to do exactly that.

To access the underlying file system, the javax.swing package has an excellent component, the JFileChooser component. No doubt, you have used a Swing application before that utilized the JFileChooser component.

To create a JFileChooser, you first instantiate a JFileChooser object, set its size, and then declare it to either open files, or save them. To do this, you associate the object with its parent - the object that will invoke it (a menu item in our case) - and then set it to be either an open dialog or save dialog. To do this, you use the showSaveDialog() or showOpenDialog(), both of which have a return type of int. Below is a brief example:




JFileChooser jfc = new JFileChooser();
jfc.setSize( 500, 250 );
Container parent = openItem.getParent();
int choice = jfc.showOpenDialog(parent);

The integer that is returned in the final line indicates whether or not the user ultimately opens/saves a file, or presses the cancel button. To respond to the open/save event, the integer can be compared to the APPROVE_OPTION constant defined in the JFileChooser class. At this point, you simply use whatever means are appropriate for you to open/save/serialize the request file on behalf of the user.

To see a complete implementation of this, consult Listing 1; it contains all six methods used to construct the menu handling functionality for the XmlEditor application.

Build the Dialog Component to Verify Choices
Currently, whenever you click the Jframe's close window, the application immediately closes down. This is not good. What if the user accidentally closes the window in the middle of working on a file? We want to prompt the user and ask them if it is all right to shut the application down.

To do that, we want to use a JDialog box. Dialog boxes are used by every graphical application that is worth their salt. They will prompt the user before writing over another file, closing an open file without saving it, or before closing an application. We will only concern ourselves with closing the editor.

What we need to do is create a JDialog box complete with a Jlabel containing the prompt, and two buttons, one to accept the closing of the program, the other to reject it. Below is the code to construct this component:


	JDialog verifyDialog = new JDialog( this, "Confirm Exit", true );

	Jlabel question = new JLabel( "Are you sure you want to exit?" );
	
	Jbutton okButton = new JButton( "OK" );
	okButton.addActionListener( this );

	Jbutton cancelButton = new JButton( "Cancel" );
	cancelButton.addActionListener( this );

	verifyDialog.getContentPane().setLayout( new FlowLayout() );
	verifyDialog.getContentPane().add( question );
	verifyDialog.getContentPane().add( okButton );
	verifyDialog.getContentPane().add( cancelButton );
	verifyDialog.hide();

Now, only two things remain. We must write the event handling code for the two buttons, and override the window closing event behavior. As far as handling the two buttons, we simply want to close the application when 'OK' is pressed, and hide the dialog box when 'Cancel' is pressed.

The final step is to override the default window closing event behavior. By default, even if you create a dialog box and the user clicks the cancel button, the JFrame has still received the "close window " event. This will cause the JFrame to hide itself unless we override it with the following setting:

setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE ); 

The above line belongs in the constructor when the other properties of the frame are defined. This new setting will make it so that the JFrame never closes itself in response to a window close event. It will only close itself in response to a System.exit() call.

Congratulations!
A lot of material has been covered in this last installment. An entire article could easily have been written on the JMenu component, JFileChooser, and JDialog components. I encourage you to download the source code and study the changes made between the last version and this new version.

Once you've added the menu component, defined event handling routines for the menu events, and added a means of canceling an accidental window close event, they you are ready to test the application and begin to create, edit, and save XML files!

Congratulations! You have a home-grown, Swing-based, XML Editor. Play around with it, make it more robust, and tell me what you come up with. I'd love to hear from you!



DevX Java Pro Kyle Gabhart is a consultant for Objective Solutions, a high-end engineering services company focusing on the medical, semiconductor, and telecommunications domains. He is an Associate Member of the Worldwide Institute of Software Architects (WWISA), an organization dedicated to promoting and enriching software architecture as a profession. Kyle is a co-author of "Professional Java and XML".
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date