Reputation: 43
I'm trying to follow some demo code based on the Java Preferences API, but I'm getting strange method not found errors relating to setDefaultCloseOperation(int) and setJMenuBar(JMenuBar). This sample program is from pages 796-799 of Core Java, Volume I - Fundamentals, 11th Edition, by Cay S. Horstman.
I apologize for what seems like a trivial error, but I've been scratching my head over what should be a simple fix. The entire file is needed to reproduce the problem, since other compilation errors besides the above mentioned could arise if parts of the file were left out.
I have tried the following.
.*
ones.JDK 1.8_202
versus older/never JDK versions).PreferencesFrame.java
import java.awt.*;
import java.io.*;
import java.util.prefs.*;
import javax.swing.*;
import javax.swing.filechooser.*;
/** A program to test preference settings. The program remembers the frame position, size, and title. **/
public class PreferencesTest {
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
PreferencesFrame frame = new PreferencesFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}
/** A frame that restores position and size from user preferences and updates the preferences upon exit. **/
class PreferencesFrame extends Frame {
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 200;
private Preferences root = Preferences.userRoot();
private Preferences node = root.node("/com/horstmann/corejava");
public PreferencesFrame() {
// Get position, size, and title from preferences.
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 was given, ask the user.
String title = node.get("title", "");
if (title.equals(""))
title = JOptionPane.showInputDialog("Title:");
if (title == null) title = "";
setTitle(title);
// Set up the file chooser to show XML files, to select the preferences.xml file.
final JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setFileFilter(new FileNameExtensionFilter("XML files", "xml"));
// Set up the menus.
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menu = new JMenu("File");
menuBar.add(menu);
JMenuItem exportItem = new JMenuItem("Export Preferences");
menu.add(exportItem);
exportItem.addActionListener(event -> {
if (chooser.showSaveDialog(PreferencesFrame.this) == JFileChooser.APPROVE_OPTION) {
try {
savePreferences();
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(event -> {
if (chooser.showSaveDialog(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(event -> {
savePreferences();
System.exit(0);
});
}
public void savePreferences() {
node.putInt("left", getX());
node.putInt("top", getY());
node.putInt("width", getWidth());
node.putInt("height", getHeight());
node.put("title", getTitle());
}
}
preferences.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd">
<preferences EXTERNAL_XML_VERSION="1.0">
<root type="user">
<map/>
<node name="com">
<map/>
<node name="horstmann">
<map/>
<node name="corejava">
<map>
<entry key="left" value="11"/>
<entry key="top" value="9"/>
<entry key="width" value="453"/>
<entry key="height" value="365"/>
<entry key="title" value="Hello, World!"/>
</map>
</node>
</node>
</node>
</root>
</preferences>
I expect the program to compile and display a JFrame, but 2 compilation errors are seen.
1. PreferencesTest.java.13: error: cannot find symbol
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
symbol: method setDefaultCloseOperation(int)
class PreferencesFrame
2. PreferencesTest.java:49: error: cannot find symbol setJMenuBar(menuBar);
symbol: method setJMenuBar(JMenuBar)
class PreferencesFrame
Upvotes: 1
Views: 65
Reputation: 6808
The problem is probably here: class PreferencesFrame extends Frame
. It should be class PreferencesFrame extends JFrame
. Frame
class is fromjava.awt
package and JFrame
class (the one with setDefaultCloseOperation
method) is from javax.swing
package.
Your total imports should be:
import java.awt.EventQueue;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.prefs.Preferences;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
Upvotes: 2