Results 1 to 1 of 1
Thread: java.util.Preferences
- 06-06-2009, 11:28 PM #1
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
-
scripting preferences import
By MikeW in forum EclipseReplies: 2Last Post: 07-18-2008, 03:18 PM -
How to use JFace Preferences and Field Editors
By Java Tip in forum SWTReplies: 0Last Post: 07-07-2008, 04:48 PM -
JFace Preferences Demonstration
By Java Tip in forum SWTReplies: 0Last Post: 07-07-2008, 04:48 PM -
java.util
By Java Tutorial in forum Java TutorialReplies: 1Last Post: 02-07-2008, 01:46 PM -
Using java.util.Formatter
By Java Tip in forum Java TipReplies: 0Last Post: 11-16-2007, 02:29 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks