Results 1 to 1 of 1
  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    213
    Rep Power
    10

    Default java.util.Preferences

    1. The .xml file is not saved with an .xml extension by default.
    2. I would like to update the code so that the width and height of the frame are saved within the .xml saved by the user.
    3. How can I validate my xml file against the schema? (http://java.sun.com/dtd/preferences.dtd)

    :confused:


    Java Code:
    package homenetwork.bkr.training;
    
    import java.awt.EventQueue;
    import java.awt.event.*;
    import java.io.*;
    import java.util.prefs.*;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class PreferencesFrame extends JFrame {
    	
    	public PreferencesFrame()
    	{
    		//get position, size and title from preferences
    		Preferences root = Preferences.userRoot();
    		final Preferences node = root.node("/com/horstmann/corejava");
    		int left = node.getInt("left", 0);
    		int top = node.getInt("top", 0);
    		int width = node.getInt("width", DEFAULT_WIDTH);
    		int height = node.getInt("height", DEFAULT_HEIGHT);
    		setBounds(left, top, width, height);
    		
    		//if no title given, ask user
    		String title = node.get("title", "");
    		if (title.equals("")) title = JOptionPane.showInputDialog("Please supply a frame title:");
    		if (title == null) title = "";
    		setTitle(title);
    		
    		//set up file chooser that shows XML files
    		final JFileChooser chooser = new JFileChooser();
    		chooser.setCurrentDirectory(new File("."));
    		
    		//accept all files ending with .xml
    		chooser.setFileFilter(new javax.swing.filechooser.FileFilter()
    		{
    			public boolean accept (File f)
    			{
    				return f.getName().toLowerCase().endsWith(".xml") || f.isDirectory();
    			}
    			public String getDescription()
    			{
    				return "XML files";
    			}
    		});
    		
    		//set up menus
    		JMenuBar menuBar = new JMenuBar();
    		setJMenuBar(menuBar);
    		JMenu menu = new JMenu("File");
    		menuBar.add(menu);
    		
    		[B]JMenuItem exportItem = new JMenuItem("Export preferences");[/B]
    		menu.add(exportItem);
    		exportItem.addActionListener(new ActionListener()
    
    		{
    			public void actionPerformed(ActionEvent event)
    			{
    				if (chooser.showSaveDialog(PreferencesFrame.this) == JFileChooser.APPROVE_OPTION)
    				{
    					try 
    					{
    						OutputStream out = new FileOutputStream(chooser.getSelectedFile());
    						node.exportSubtree(out);
    						out.close();
    					}
    					catch (Exception e)
    					{
    						e.printStackTrace();
    					}
    				}
    			}
    		});
    
    		JMenuItem importItem = new JMenuItem("Import preferences");
    		menu.add(importItem);
    		importItem.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent event)
    			{
    				if (chooser.showOpenDialog(PreferencesFrame.this) == JFileChooser.APPROVE_OPTION)
    					try
    					{
    						InputStream in = new FileInputStream(chooser.getSelectedFile());
    						Preferences.importPreferences(in);
    						in.close();
    					}
    					catch (Exception e)
    					{
    						e.printStackTrace();
    					}
    			}
    		});
    		
    		JMenuItem exitItem = new JMenuItem("Exit");
    		menu.add(exitItem);
    		exitItem.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent event)
    			{  //save settings before exiting
    
    				[B]node.putInt("left", getX());
    				node.putInt("top", getY());
    				node.putInt("width", getWidth());
    				node.putInt("height", getHeight());
    				node.put("title", getTitle());
    				System.exit(0);[/B]
    			}
    		});
    	}
    
    	public static final int DEFAULT_WIDTH = 300;
    	public static final int DEFAULT_HEIGHT = 200;
    	//TODO update the code so that the width and height of the frame
    	//     are saved within the preferences file.
    }
    Last edited by jon80; 06-06-2009 at 11:33 PM. Reason: update 1

Similar Threads

  1. scripting preferences import
    By MikeW in forum Eclipse
    Replies: 2
    Last Post: 07-18-2008, 03:18 PM
  2. How to use JFace Preferences and Field Editors
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 04:48 PM
  3. JFace Preferences Demonstration
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 04:48 PM
  4. java.util
    By Java Tutorial in forum Java Tutorial
    Replies: 1
    Last Post: 02-07-2008, 02:46 PM
  5. Using java.util.Formatter
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-16-2007, 03:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •