Commit b159d71b authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.

Initial commit

parents
9781565924888
\ No newline at end of file
## Example files for the title:
# Java Foundation Classes in a Nutshell, by David Flanagan
[![Java Foundation Classes in a Nutshell, by David Flanagan](http://akamaicovers.oreilly.com/images/9781565924888/cat.gif)](https://www.safaribooksonline.com/)
The following applies to example files from material published by O’Reilly Media, Inc. Content from other publishers may include different rules of usage. Please refer to any additional usage rights explained in the actual example files or refer to the publisher’s website.
O'Reilly books are here to help you get your job done. In general, you may use the code in O'Reilly books in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from our books does not require permission. Answering a question by citing our books and quoting example code does not require permission. On the other hand, selling or distributing a CD-ROM of examples from O'Reilly books does require permission. Incorporating a significant amount of example code from our books into your product's documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN.
If you think your use of code examples falls outside fair use or the permission given here, feel free to contact us at <permissions@oreilly.com>.
Please note that the examples are not production code and have not been carefully testing. They are provided "as-is" and come with no warranty of any kind.
<HTML>
<HEAD>
<TITLE>Examples From Java Foundation Classes in a Nutshell</TITLE>
</HEAD>
<body bgcolor="#ffffff">
<img src="http://www.oreilly.com/graphics_new/generic_ora_header_xwide.gif"
width="610" height="37" alt="O'Reilly and Associates" class="headerimg" />
<h2>Java Foundation Classes in a Nutshell
<br />
Examples</h2>
<p><b>[ Back to:
<a href="http://www.oreilly.com/catalog/jfcnut/">Java Foundation Classes in a Nutshell</a> ]</b></p>
<p>
The code in these example archives may not match the examples in the latest print copy of the book. Please check
<A HREF="http://www.oreilly.com/catalog/jfcnut/errata/">the posted errata</A> for discrepancies between the book and online errata. We apologize for the inconvenience.
<p>
You can download the examples from the book <I>Java Foundation Classes
in a Nutshell</I> from:
<UL>
<LI><A HREF="jfcnutex.zip">jfcnutex.zip</A> (20 Kb)
</UL>
<P>
Note that if you are using Sun's Java distribution, you
can use the <I>jar</I> command to unpack this archive:
<PRE>
% jar xf jfcnutex.zip
</PRE>
<p>
This creates a subdirectory <I>jfcnutex</I> that contains
the example files.
<P>
<I>Note to Windows users:</I> the examples include files with long
file names, so be sure to unzip it with a modern unzip program that
is capable of handling file names with more than eight characters!
<p><b>Back to: <a href="http://www.oreilly.com/catalog/jfcnut/">Java
Foundation Classes in a Nutshell</a></b></p>
<center>
<hr size="1" noshade="noshade" />
<font size="1" face="Verdana, Arial, Helvetica">
<a href="http://www.oreilly.com/"><b>oreilly.com Home</b></a> <b> | </b>
<a href="http://www.oreilly.com/sales/bookstores"><b>O'Reilly Bookstores</b></a> <b> | </b>
<a href="http://www.oreilly.com/order_new/"><b>How to Order</b></a> <b> | </b>
<a href="http://www.oreilly.com/oreilly/contact.html"><b>O'Reilly
Contacts<br /></b></a>
<a href="http://www.oreilly.com/international/"><b>International</b></a> <b> | </b>
<a href="http://www.oreilly.com/oreilly/about.html"><b>About O'Reilly</b></a> <b> | </b>
<a href="http://www.oreilly.com/affiliates.html"><b>Affiliated Companies</b></a> <b> | </b>
<a href="http://www.oreilly.com/privacy_policy.html"><b>Privacy Policy</b></a>
<p><em>&copy; 2001, O'Reilly &amp; Associates, Inc.</em>
</font>
</center>
</body>
</html>
/*
* This example is from the book "Java Foundation Classes in a Nutshell".
* Written by David Flanagan. Copyright (c) 1999 by O'Reilly & Associates.
* You may distribute this source code for non-commercial purposes only.
* You may study, modify, and use this example for any purpose, as long as
* this notice is retained. Note that this example is provided "as is",
* WITHOUT WARRANTY of any kind either expressed or implied.
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
import java.util.List;
/**
* This simple JTextArea subclass allows TransferableColor objects to
* be pasted or dropped into it. It also supports the pasting of
* text, and the dropping of File objects.
*/
public class ColorSink extends JTextArea implements DropTargetListener {
/** Create a new ColorSink object */
public ColorSink() {
// Listen for double clicks. Use them to trigger a paste action.
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) { pastecolor(); e.consume(); }
}
});
// We have to create a DropTarget object to support Drag-and-Drop
// It will listen for drops on top of us, and notify our DropTargetListener
// methods when drag-and-drop related events occur.
setDropTarget(new DropTarget(this, this));
}
// This method is invoked when the user double-clicks on us. It attempts
// to paste a color or text. Note that the JTextArea we extend
// already supports cut-and-paste of text through the Ctrl-V keystroke.
// This adds a different kind of cut-and-paste for demonstration purposes.
public void pastecolor() {
// Get the clipboard, and read its contents
Clipboard c = this.getToolkit().getSystemClipboard();
Transferable t = c.getContents(this);
if (t == null) { // If nothing to paste
this.getToolkit().beep(); // then beep and do nothing
return;
}
try {
// If the clipboard contained a color, use it as the background color
if (t.isDataFlavorSupported(TransferableColor.colorFlavor)) {
Color color = (Color) t.getTransferData(TransferableColor.colorFlavor);
this.setBackground(color);
}
// If the clipboard contained text, insert it.
else if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
String s = (String) t.getTransferData(DataFlavor.stringFlavor);
this.replaceSelection(s);
}
// Otherwise, we don't know how to paste the data, so just beep.
else this.getToolkit().beep();
}
catch (UnsupportedFlavorException ex) { this.getToolkit().beep(); }
catch (IOException ex) { this.getToolkit().beep(); }
}
// The methods below are the methods of DropTargetListener.
// They are invoked at various times when something is being
// dragged over us, and allow us an opportunity to respond to the drag
// This is the border we display when the user is dragging over us.
protected static Border dropBorder = new BevelBorder(BevelBorder.LOWERED);
// Something is being dragged over us. If we can support this data type
// tell the drag-and-drop system that we are interested, and display
// a special border to tell the user that we're intereted.
public void dragEnter(DropTargetDragEvent e) {
if (e.isDataFlavorSupported(TransferableColor.colorFlavor) ||
e.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
e.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
this.setBorder(dropBorder);
}
}
/** The user is no longer dragging over us, so restore the default border */
public void dragExit(DropTargetEvent e) { this.setBorder(null); }
/** This method is invoked when the user drops something on us */
public void drop(DropTargetDropEvent e){
this.setBorder(null); // Restore the default border
Transferable t = e.getTransferable(); // Get the data that was dropped
// Check for types of data that we support
if (t.isDataFlavorSupported(TransferableColor.colorFlavor)) {
// If it was a color, accept it, and use it as the background color
e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
try {
Color c = (Color) t.getTransferData(TransferableColor.colorFlavor);
this.setBackground(c);
e.dropComplete(true);
}
catch (Exception ex) { e.dropComplete(false); }
}
else if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
// If it was a file list, accept it, read the first file in the list
// and display the file contents
e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
try {
List files = (List) t.getTransferData(DataFlavor.javaFileListFlavor);
File f = (File) files.get(0);
BufferedReader in = new BufferedReader(new FileReader(f));
String s;
this.setText("");
while((s = in.readLine()) != null) this.append(s);
e.dropComplete(true);
}
catch (Exception ex) { e.dropComplete(false); }
}
else { // If it wasn't a color or a file list, reject it.
e.rejectDrop();
return;
}
}
// These are unused DropTargetListener methods
public void dragOver(DropTargetDragEvent e) {}
public void dropActionChanged(DropTargetDragEvent e) {}
/** This is a simple test program for ColorSource and ColorSink */
public static void main(String[] args) {
// Create a window
JFrame f = new JFrame("ColorSourceTest");
f.getContentPane().setLayout(new BorderLayout());
// Add some ColorSources
JPanel panel = new JPanel();
f.getContentPane().add(panel, BorderLayout.NORTH);
panel.add(new ColorSource(Color.yellow));
panel.add(new ColorSource(Color.pink));
panel.add(new ColorSource(Color.white));
panel.add(new ColorSource(Color.gray));
// Add a ColorSink
ColorSink sink = new ColorSink();
f.getContentPane().add(sink, BorderLayout.CENTER);
// Pop it all up
f.setSize(400, 300);
f.show();
}
}
/*
* This example is from the book "Java Foundation Classes in a Nutshell".
* Written by David Flanagan. Copyright (c) 1999 by O'Reilly & Associates.
* You may distribute this source code for non-commercial purposes only.
* You may study, modify, and use this example for any purpose, as long as
* this notice is retained. Note that this example is provided "as is",
* WITHOUT WARRANTY of any kind either expressed or implied.
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
/**
* This simple component displays a solid color, and allows that color
* to be dragged. Also, it copies the color to the clipboard when the
* user clicks on it.
*/
public class ColorSource extends JComponent
implements ClipboardOwner, DragGestureListener, DragSourceListener
{
Color color; // The color to display
TransferableColor tcolor; // The color, encapsulated for data transfer
DragSource dragSource; // We need this object for drag-and-drop
/** A ColorSource normally displays itself with this border */
protected static Border defaultBorder = new BevelBorder(BevelBorder.LOWERED);
/** When we are the clipboard owner, uses this border */
protected static Border highlightBorder =
new CompoundBorder(defaultBorder, new LineBorder(Color.black, 2));
/** Create a new ColorSource object that displays the speciifed color */
public ColorSource(Color color) {
// Save the color. Encapsulate it in a Transferable object so that
// it can be used with cut-and-paste and drag-and-drop
this.color = color;
this.tcolor = new TransferableColor(color);
// Set our default border
this.setBorder(defaultBorder);
// Listen for mouse clicks, and copy the color to the clipboard.
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) { copy(); }
});
// Set up a DragGestureRecognizer that will detect when the user
// begins a drag. When it detects one, it will notify us by calling
// the dragGestureRecognized() method of the DragGestureListener
// interface we implement below
this.dragSource = DragSource.getDefaultDragSource();
dragSource.createDefaultDragGestureRecognizer(this, // Look for drags on us
DnDConstants.ACTION_COPY_OR_MOVE, // Recognize these types
this); // Tell us when recognized
}
// These are component methods that make this class work as a component.
// They specify how big the component is, and what it it looks like.
protected static Dimension mysize = new Dimension(25, 25);
public Dimension getMinimumSize() { return mysize; }
public Dimension getPreferredSize() { return mysize; }
public void paintComponent(Graphics g) {
g.setColor(color);
Dimension s = this.getSize();
Insets i = this.getInsets();
g.fillRect(i.left, i.top,
s.width-i.left-i.right, s.height-i.top-i.bottom);
}
// The methods below support cut-and-paste
/** This method copies the color to the clipboard. */
public void copy() {
// Get system clipboard
Clipboard c = this.getToolkit().getSystemClipboard();
// Put our TransferableColor object on the clipboard.
// Also, we'll get notification when we no longer own the clipboard
c.setContents(tcolor, this);
// Set a special border on ourselves that indicates that we're the
// current color available for pasting.
this.setBorder(highlightBorder);
}
// This ClipboardOwner method is called when something else is
// placed on the clipboard. It means that our color is no longer
// available for pasting, and we should not display the highlight border
public void lostOwnership(Clipboard clipboard, Transferable contents) {
this.setBorder(defaultBorder);
}
// The methods below support drag-and-drop
// This DragGestureListener method is called when the DragGestureRecognizer
// detects that the user has dragged the mouse. It is responsible
// for beginning the drag-and-drop process.
public void dragGestureRecognized(DragGestureEvent e) {
// Create an image we can drag along with us.
// Not all systems support this, but it doesn't hurt to try.
Image colorblock = this.createImage(25,25);
Graphics g = colorblock.getGraphics();
g.setColor(color);
g.fillRect(0,0,25,25);
// Start dragging our transferable color object.
e.startDrag(DragSource.DefaultMoveDrop, // The initial drag cursor
colorblock, new Point(0,0), // The image to drag
tcolor, // The data being dragged
this); // Who to notify during drag
}
// These methods implement DragSourceListener.
// Since we passed this object to startDrag, these methods will be
// called at interesting points during the drag. We could use them,
// for example to implement custom cursors or other "drag over" effects
public void dragEnter(DragSourceDragEvent e) {}
public void dragExit(DragSourceEvent e) {}
public void dragDropEnd(DragSourceDropEvent e) {}
public void dragOver(DragSourceDragEvent e) {}
public void dropActionChanged(DragSourceDragEvent e) {}
}
/*
* This example is from the book "Java Foundation Classes in a Nutshell".
* Written by David Flanagan. Copyright (c) 1999 by O'Reilly & Associates.
* You may distribute this source code for non-commercial purposes only.
* You may study, modify, and use this example for any purpose, as long as
* this notice is retained. Note that this example is provided "as is",
* WITHOUT WARRANTY of any kind either expressed or implied.
*/
import java.awt.*; // AWT classes
import javax.swing.*; // Swing components and classes
import javax.swing.border.*; // Borders for Swing components
import java.awt.event.*; // Basic event handling
public class DisplayMessage {
public static void main(String[] args) {
/*
* Step 1: Create the components
*/
JLabel msgLabel = new JLabel(); // Component to display the question
JButton yesButton = new JButton(); // Button for an affirmative response
JButton noButton = new JButton(); // Button for a negative response
/*
* Step 2: Set properties of the components
*/
msgLabel.setText(args[0]); // The msg to display
msgLabel.setBorder(new EmptyBorder(10,10,10,10)); // A 10-pixel margin
yesButton.setText((args.length >= 2)?args[1]:"Yes"); // Text for Yes button
noButton.setText((args.length >= 3)?args[2]:"No"); // Text for no button
/*
* Step 3: Create containers to hold the components
*/
JFrame win = new JFrame("Message"); // The main application window
JPanel buttonbox = new JPanel(); // A container for the two buttons
/*
* Step 4: Specify LayoutManagers to arrange components in the containers
*/
win.getContentPane().setLayout(new BorderLayout()); // layout on borders
buttonbox.setLayout(new FlowLayout()); // layout left-to-right
/*
* Step 5: Add components to containers, with optional layout constraints
*/
buttonbox.add(yesButton); // add yes button to the panel
buttonbox.add(noButton); // add no button to the panel
// add JLabel to window, telling the BorderLayout to put it in the middle
win.getContentPane().add(msgLabel, "Center");
// add panel to window, telling the BorderLayout to put it at the bottom
win.getContentPane().add(buttonbox, "South");
/*
* Step 6: Arrange to handle events in the user interface.
*/
yesButton.addActionListener(new ActionListener() { // Note: inner class
// This method is called when the Yes button is clicked.
public void actionPerformed(ActionEvent e) { System.exit(0); }
});
noButton.addActionListener(new ActionListener() { // Note: inner class
// This method is called when the No button is clicked.
public void actionPerformed(ActionEvent e) { System.exit(1); }
});
/*
* Step 7: Display the GUI to the user
*/
win.pack(); // Set the size of the window based its children's sizes.
win.show(); // Make the window visible.
}
}
/*
* This example is from the book "Java Foundation Classes in a Nutshell".
* Written by David Flanagan. Copyright (c) 1999 by O'Reilly & Associates.
* You may distribute this source code for non-commercial purposes only.
* You may study, modify, and use this example for any purpose, as long as
* this notice is retained. Note that this example is provided "as is",
* WITHOUT WARRANTY of any kind either expressed or implied.
*/
import javax.swing.*;
import javax.swing.table.*;
import java.io.File;
import java.util.Date;
public class FileTableDemo {
public static void main(String[] args) {
// Figure out what directory to display;
File dir;
if (args.length > 0) dir = new File(args[0]);
else dir = new File(System.getProperty("user.home"));
// Create a TableModel object to represent the contents of the directory
FileTableModel model = new FileTableModel(dir);
// Create a JTable and tell it to display our model
JTable table = new JTable(model);
// Display it all in a scrolling window and make the window appear
JFrame frame = new JFrame("FileTableDemo");
frame.getContentPane().add(new JScrollPane(table), "Center");
frame.setSize(600, 400);
frame.setVisible(true);
}
}
/**
* The methods in this class allow the JTable component to get
* and display data about the files in a specified directly.
* It represents a table with 6 columns: file name, size, modification date,
* plus three columns for flags: directory, readable, writable
**/
class FileTableModel extends AbstractTableModel {
protected File dir;
protected String[] filenames;
protected String[] columnNames = new String[] {
"name", "size", "last modified", "directory?", "readable?", "writable?"
};
protected Class[] columnClasses = new Class[] {
String.class, Long.class, Date.class,
Boolean.class, Boolean.class, Boolean.class
};
// This table model works for any one given directory
public FileTableModel(File dir) {
this.dir = dir;
this.filenames = dir.list(); // Store a list of files in the directory
}
// These are easy methods.
public int getColumnCount() { return 6; } // A constant for this model
public int getRowCount() { return filenames.length; } // # of files in dir
// Information about each column.
public String getColumnName(int col) { return columnNames[col]; }
public Class getColumnClass(int col) { return columnClasses[col]; }
// The method that must actually return the value of each cell.
public Object getValueAt(int row, int col) {
File f = new File(dir, filenames[row]);
switch(col) {
case 0: return filenames[row];
case 1: return new Long(f.length());
case 2: return new Date(f.lastModified());
case 3: return f.isDirectory() ? Boolean.TRUE : Boolean.FALSE;
case 4: return f.canRead() ? Boolean.TRUE : Boolean.FALSE;
case 5: return f.canWrite() ? Boolean.TRUE : Boolean.FALSE;
default: return null;
}
}
}
/*
* This example is from the book "Java Foundation Classes in a Nutshell".
* Written by David Flanagan. Copyright (c) 1999 by O'Reilly & Associates.
* You may distribute this source code for non-commercial purposes only.
* You may study, modify, and use this example for any purpose, as long as
* this notice is retained. Note that this example is provided "as is",
* WITHOUT WARRANTY of any kind either expressed or implied.
*/
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.util.Date;
/**
* This class implements a simple directory browser using the HTML
* display capabilities of the JEditorPane component.
**/
public class FileTableHTML {
public static void main(String[] args) throws IOException {
// Get the name of the directory to display
String dirname = (args.length>0)?args[0]:System.getProperty("user.home");
// Create something to display it in.
final JEditorPane editor = new JEditorPane();
editor.setEditable(false); // we're browsing not editing
editor.setContentType("text/html"); // must specify HTML text
editor.setText(makeHTMLTable(dirname)); // specify the text to display
// Set up the JEditorPane to handle clicks on hyperlinks
editor.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
// Handle clicks; ignore mouseovers and other link-related events
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
// Get the HREF of the link and display it.
editor.setText(makeHTMLTable(e.getDescription()));
}
}
});
// Put the JEditorPane in a scrolling window and display it.
JFrame frame = new JFrame("FileTableHTML");
frame.getContentPane().add(new JScrollPane(editor));
frame.setSize(650, 500);
frame.setVisible(true);
}
// This method returns an HTML table representing the specified directory
public static String makeHTMLTable(String dirname) {
// Look up the contents of the directory
File dir = new File(dirname);
String[] entries = dir.list();
// Set up an output stream we can print the table to.
// This is easier than concatenating strings all the time.
StringWriter sout = new StringWriter();
PrintWriter out = new PrintWriter(sout);
// Print the directory name as the page title
out.println("<H1>" + dirname + "</H1>");
// Print an "up" link, unless we're already at the root
String parent = dir.getParent();
if ((parent != null) && (parent.length() > 0))
out.println("<A HREF=\"" + parent + "\">Up to parent directory</A><P>");
// Print out the table
out.print("<TABLE BORDER=2 WIDTH=600><TR>");
out.print("<TH>Name</TH><TH>Size</TH><TH>Modified</TH>");
out.println("<TH>Readable?</TH><TH>Writable?</TH></TR>");
for(int i=0; i < entries.length; i++) {
File f = new File(dir, entries[i]);
out.println("<TR><TD>" +
(f.isDirectory() ?
"<a href=\""+f+"\">" + entries[i] + "</a>" :
entries[i]) +
"</TD><TD>" + f.length() +
"</TD><TD>" + new Date(f.lastModified()) +
"</TD><TD align=center>" + (f.canRead()?"x":" ") +
"</TD><TD align=center>" + (f.canWrite()?"x":" ") +
"</TD></TR>");
}
out.println("</TABLE>");
out.close();
// Get the string of HTML from the StringWriter and return it.
return sout.toString();
}
}
/*
* This example is from the book "Java Foundation Classes in a Nutshell".
* Written by David Flanagan. Copyright (c) 1999 by O'Reilly & Associates.
* You may distribute this source code for non-commercial purposes only.
* You may study, modify, and use this example for any purpose, as long as
* this notice is retained. Note that this example is provided "as is",
* WITHOUT WARRANTY of any kind either expressed or implied.
*/
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.io.File;
public class FileTreeDemo {
public static void main(String[] args) {
// Figure out where in the filesystem to start displaying
File root;
if (args.length > 0) root = new File(args[0]);
else root = new File(System.getProperty("user.home"));
// Create a TreeModel object to represent our tree of files
FileTreeModel model = new FileTreeModel(root);
// Create a JTree and tell it to display our model
JTree tree = new JTree();
tree.setModel(model);
// The JTree can get big, so allow it to scroll.
JScrollPane scrollpane = new JScrollPane(tree);
// Display it all in a window and make the window appear
JFrame frame = new JFrame("FileTreeDemo");
frame.getContentPane().add(scrollpane, "Center");
frame.setSize(400,600);
frame.setVisible(true);
}
}
/**
* The methods in this class allow the JTree component to traverse
* the file system tree, and display the files and directories.
**/
class FileTreeModel implements TreeModel {
// We specify the root directory when we create the model.
protected File root;
public FileTreeModel(File root) { this.root = root; }
// The model knows how to return the root object of the tree
public Object getRoot() { return root; }
// Tell JTree whether an object in the tree is a leaf or not
public boolean isLeaf(Object node) { return ((File)node).isFile(); }
// Tell JTree how many children a node has
public int getChildCount(Object parent) {
String[] children = ((File)parent).list();
if (children == null) return 0;
return children.length;
}
// Fetch any numbered child of a node for the JTree.
// Our model returns File objects for all nodes in the tree. The
// JTree displays these by calling the File.toString() method.
public Object getChild(Object parent, int index) {
String[] children = ((File)parent).list();
if ((children == null) || (index >= children.length)) return null;
return new File((File) parent, children[index]);
}
// Figure out a child's position in its parent node.
public int getIndexOfChild(Object parent, Object child) {
String[] children = ((File)parent).list();
if (children == null) return -1;
String childname = ((File)child).getName();
for(int i = 0; i < children.length; i++) {
if (childname.equals(children[i])) return i;
}
return -1;
}
// This method is only invoked by the JTree for editable trees.
// This TreeModel does not allow editing, so we do not implement
// this method. The JTree editable property is false by default.
public void valueForPathChanged(TreePath path, Object newvalue) {}
// Since this is not an editable tree model, we never fire any events,
// so we don't actually have to keep track of interested listeners.
public void addTreeModelListener(TreeModelListener l) {}
public void removeTreeModelListener(TreeModelListener l) {}
}
/*
* This example is from the book "Java Foundation Classes in a Nutshell".
* Written by David Flanagan. Copyright (c) 1999 by O'Reilly & Associates.
* You may distribute this source code for non-commercial purposes only.
* You may study, modify, and use this example for any purpose, as long as
* this notice is retained. Note that this example is provided "as is",
* WITHOUT WARRANTY of any kind either expressed or implied.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MenuDemo {
public static void main(String[] args) {
// Create a window for this demo
JFrame frame = new JFrame("Menu Demo");
JPanel panel = new JPanel();
frame.getContentPane().add(panel, "Center");