-
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:
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.
}