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

Initial commit

parents
9780596004088
\ No newline at end of file
## Example files for the title:
# Java Swing 2nd Edition, by Robert Eckstein
[![Java Swing 2nd Edition, by Robert Eckstein](http://akamaicovers.oreilly.com/images/9780596004088/cat.gif)](https://www.safaribooksonline.com/library/view/title/0596004087//)
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.
// CCPHandler.java
// A simple event handler to display cut/copy/paste events.
//
import java.awt.event.*;
public class CCPHandler implements ActionListener {
public final static String CUT = "cut";
public final static String COPY = "copy";
public final static String PASTE = "paste";
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command == CUT) { // We can do this since we're comparing constants.
System.out.println("Got Cut event");
}
else if (command == COPY) {
System.out.println("Got Copy event");
}
else if (command == PASTE) {
System.out.println("Got Paste event");
}
}
}
// CopyAction.java
// A simple Action that copies text from a PageFrame object.
//
import java.awt.event.ActionEvent;
import javax.swing.*;
public class CopyAction extends AbstractAction {
SiteManager manager;
public CopyAction(SiteManager sm) {
super("", new ImageIcon("copy.gif"));
manager = sm;
}
public void actionPerformed(ActionEvent ae) {
JInternalFrame currentFrame = manager.getCurrentFrame();
if (currentFrame == null) { return; }
// can't cut or paste sites
if (currentFrame instanceof SiteFrame) { return; }
((PageFrame)currentFrame).copyText();
}
}
// CutAction.java
// A simple Action that copies text from a PageFrame object.
//
import java.awt.event.ActionEvent;
import javax.swing.*;
public class CutAction extends AbstractAction {
SiteManager manager;
public CutAction(SiteManager sm) {
super("", new ImageIcon("cut.gif"));
manager = sm;
}
public void actionPerformed(ActionEvent ae) {
JInternalFrame currentFrame = manager.getCurrentFrame();
if (currentFrame == null) { return; }
// cannot cut or paste sites
if (currentFrame instanceof SiteFrame) { return; }
((PageFrame)currentFrame).cutText();
}
}
// LnFListener.java
// A listener that can change the look-and-feel of a frame based on
// the actionCommand of an ActionEvent object. Supported look-and-feels are:
// Mac, Metal, Motif, Windows.
// Not all L&Fs will be available on a given machine. Notably, the Mac and
// Windows L&Fs work only on their specific platforms.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LnFListener implements ActionListener {
Frame frame;
public LnFListener(Frame f) {
frame = f;
}
public void actionPerformed(ActionEvent e) {
String lnfName = null;
if (e.getActionCommand().equals("Mac")) {
lnfName = "com.apple.mrj.swing.MacLookAndFeel";
} else if (e.getActionCommand().equals("Metal")) {
lnfName = "javax.swing.plaf.metal.MetalLookAndFeel";
} else if (e.getActionCommand().equals("Motif")) {
lnfName = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
} else if (e.getActionCommand().equals("Windows")) {
lnfName = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
} else {
System.err.println("Unrecognized L&F request action: " +
e.getActionCommand());
return;
}
try {
UIManager.setLookAndFeel(lnfName);
SwingUtilities.updateComponentTreeUI(frame);
}
catch (UnsupportedLookAndFeelException ex1) {
System.err.println("Unsupported LookAndFeel: " + lnfName);
}
catch (ClassNotFoundException ex2) {
System.err.println("LookAndFeel class not found: " + lnfName);
}
catch (InstantiationException ex3) {
System.err.println("Could not load LookAndFeel: " + lnfName);
}
catch (IllegalAccessException ex4) {
System.err.println("Cannot use LookAndFeel: " + lnfName);
}
}
}
// PageFrame.java
// A simple extension of the JInternalFrame class that contains a list
// object. Elements of the list represent HTML pages for a web site.
//
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
public class PageFrame extends JInternalFrame implements ActionListener {
SiteManager parent;
String filename;
JTextArea ta;
public PageFrame(String name, SiteManager sm) {
super("Page: " + name, true, true, true, true);
parent = sm;
setBounds(50,50,300,150);
Container contentPane = getContentPane();
// Create a text area to display the contents of our file in
// and stick it in a scrollable pane so we can see everything
ta = new JTextArea();
JScrollPane jsp = new JScrollPane(ta);
contentPane.add(jsp, BorderLayout.CENTER);
JMenuBar jmb = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem saveItem = new JMenuItem("Save");
saveItem.addActionListener(this);
fileMenu.add(saveItem);
jmb.add(fileMenu);
setJMenuBar(jmb);
filename = name;
loadContent();
}
public void actionPerformed(ActionEvent ae) {
// Can only be the save menu
saveContent();
}
public void loadContent() {
try {
FileReader fr = new FileReader(filename);
ta.read(fr, null);
fr.close();
}
catch (Exception e) {
System.err.println("Could not load page: " + filename);
}
}
public void saveContent() {
try {
FileWriter fw = new FileWriter(filename);
ta.write(fw);
fw.close();
}
catch(Exception e) {
System.err.println("Could not save page: " + filename);
}
}
public void cutText() { ta.cut(); }
public void copyText() { ta.copy(); }
public void pasteText() { ta.paste(); }
}
// PasteAction.java
// A simple Action that pastes text into a PageFrame object.
//
import java.awt.event.ActionEvent;
import javax.swing.*;
public class PasteAction extends AbstractAction {
SiteManager manager;
public PasteAction(SiteManager sm) {
super("", new ImageIcon("paste.gif"));
manager = sm;
}
public void actionPerformed(ActionEvent ae) {
JInternalFrame currentFrame = manager.getCurrentFrame();
if (currentFrame == null) { return; }
// cannot cut or paste sites
if (currentFrame instanceof SiteFrame) { return; }
((PageFrame)currentFrame).pasteText();
}
}
// SimpleInternalFrame.java
// A quick demonstration of setting up an internal frame in an application.
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SimpleInternalFrame extends Frame {
JButton openButton, macButton, javaButton, motifButton, winButton;
JLayeredPane desktop;
JInternalFrame internalFrame;
public SimpleInternalFrame() {
super("Internal Frame Demo");
setSize(500,400);
openButton = new JButton("Open");
macButton = new JButton("Mac");
javaButton = new JButton("Metal");
motifButton = new JButton("Motif");
winButton = new JButton("Windows");
Panel p = new Panel();
p.add(openButton);
p.add(macButton);
p.add(javaButton);
p.add(motifButton);
p.add(winButton);
add(p, BorderLayout.SOUTH);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
openButton.addActionListener(new OpenListener());
LnFListener lnf = new LnFListener(this);
macButton.addActionListener(lnf);
javaButton.addActionListener(lnf);
motifButton.addActionListener(lnf);
winButton.addActionListener(lnf);
// Set up the layered pane
desktop = new JDesktopPane();
desktop.setOpaque(true);
add(desktop, BorderLayout.CENTER);
}
// An inner class to handle presses of the Open button
class OpenListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if ((internalFrame == null) || (internalFrame.isClosed())) {
internalFrame = new JInternalFrame("Internal Frame",
true, true, true, true);
internalFrame.setBounds(50, 50, 200, 100);
desktop.add(internalFrame, new Integer(1));
internalFrame.setVisible(true);
}
}
}
public static void main(String args[]) {
SimpleInternalFrame sif = new SimpleInternalFrame();
sif.setVisible(true);
}
}
// SiteFrame.java
// A simple extension of the JInternalFrame class that contains a list
// object. Elements of the list represent HTML pages for a web site.
//
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class SiteFrame extends JInternalFrame {
JList nameList;
SiteManager parent;
// Hardcode the pages of our "site" to keep things simple
String[] pages = {"index.html", "page1.html", "page2.html"};
public SiteFrame(String name, SiteManager sm) {
super("Site: " + name, true, true, true);
parent = sm;
setBounds(50,50,250,100);
nameList = new JList(pages);
nameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
nameList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent lse) {
// We know this is the list, so pop up the page.
if (!lse.getValueIsAdjusting()) {
parent.addPageFrame((String)nameList.getSelectedValue());
}
}
});
Container contentPane = getContentPane();
contentPane.add(nameList, BorderLayout.CENTER);
}
}