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

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);
}
}
// SiteManager.java
// A sample Swing application that manages several internal frames. This
// is the main class for working with the SiteFrame and PageFrame classes.
//
import java.awt.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
public class SiteManager extends JFrame {
JLayeredPane desktop;
Vector popups = new Vector();
public SiteManager() {
super("Web Site Manager");
setSize(450, 250);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container contentPane = getContentPane();
JToolBar jtb = new JToolBar();
jtb.add(new CutAction(this));
jtb.add(new CopyAction(this));
jtb.add(new PasteAction(this));
contentPane.add(jtb, BorderLayout.NORTH);
// Add our LayeredPane object for the internal frames.
desktop = new JDesktopPane();
contentPane.add(desktop, BorderLayout.CENTER);
addSiteFrame("Sample");
}
public static void main(String args[]) {
SiteManager mgr = new SiteManager();
mgr.setVisible(true);
}
// Methods to create our internal frames
public void addSiteFrame(String name) {
SiteFrame sf = new SiteFrame(name, this);
popups.addElement(sf);
desktop.add(sf, new Integer(2)); // Keep sites on top for now
sf.setVisible(true);
}
public void addPageFrame(String name) {
PageFrame pf = new PageFrame(name, this);
desktop.add(pf, new Integer(1));
pf.setVisible(true);
pf.setIconifiable(true);
popups.addElement(pf);
}
public JInternalFrame getCurrentFrame() {
for (int i = 0; i < popups.size(); i++) {
JInternalFrame currentFrame = (JInternalFrame)popups.elementAt(i);
if (currentFrame.isSelected()) {
return currentFrame;
}
}
return null;
}
}
// ToolbarFrame1.java
// A simple frame containing a "toolbar" made up of several java.awt.Button
// objects. We'll be converting the Buttons to JButtons in the
// ToolbarFrame2.java file.
//
import java.awt.*;
import java.awt.event.*;
public class ToolbarFrame1 extends Frame {
Button cutButton, copyButton, pasteButton;
public ToolbarFrame1() {
super("Toolbar Example (AWT)");
setSize(450, 250);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
ActionListener printListener = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.out.println(ae.getActionCommand());
}
};
Panel toolbar = new Panel();
toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
cutButton = new Button("Cut");
cutButton.addActionListener(printListener);
toolbar.add(cutButton);
copyButton = new Button("Copy");
copyButton.addActionListener(printListener);
toolbar.add(copyButton);
pasteButton = new Button("Paste");
pasteButton.addActionListener(printListener);
toolbar.add(pasteButton);
// The "preferred" BorderLayout add call
add(toolbar, BorderLayout.NORTH);
}
public static void main(String args[]) {
ToolbarFrame1 tf1 = new ToolbarFrame1();
tf1.setVisible(true);
}
}
// ToolbarFrame2.java
// The Swing-ified button example.
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ToolbarFrame2 extends Frame {
// This time, let's use JButtons!
JButton cutButton, copyButton, pasteButton;
JButton javaButton, macButton, motifButton, winButton;
public ToolbarFrame2() {
super("Toolbar Example (Swing)");
setSize(450, 250);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
ActionListener printListener = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.out.println(ae.getActionCommand());
}
};
// JPanel works similarly to Panel, so we'll use it
JPanel toolbar = new JPanel();
toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
cutButton = new JButton("Cut");
cutButton.addActionListener(printListener);
toolbar.add(cutButton);
copyButton = new JButton("Copy");
copyButton.addActionListener(printListener);
toolbar.add(copyButton);
pasteButton = new JButton("Paste");
pasteButton.addActionListener(printListener);
toolbar.add(pasteButton);
add(toolbar, BorderLayout.NORTH); // The new BorderLayout add
// Add the look-and-feel controls
JPanel lnfPanel = new JPanel();
LnFListener lnfListener = new LnFListener(this);
macButton = new JButton("Mac");
macButton.addActionListener(lnfListener);
lnfPanel.add(macButton);
javaButton = new JButton("Metal");
javaButton.addActionListener(lnfListener);
lnfPanel.add(javaButton);
motifButton = new JButton("Motif");
motifButton.addActionListener(lnfListener);
lnfPanel.add(motifButton);
winButton = new JButton("Windows");
winButton.addActionListener(lnfListener);
lnfPanel.add(winButton);
add(lnfPanel, BorderLayout.SOUTH);
}
public static void main(String args[]) {
ToolbarFrame2 tf2 = new ToolbarFrame2();
tf2.setVisible(true);
}
}
// ToolbarFrame3.java
// The Swing-ified button example. The buttons in this toolbar all carry images
// but no text.
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ToolbarFrame3 extends Frame {
JButton cutButton, copyButton, pasteButton;
JButton javaButton, macButton, motifButton, winButton;
public ToolbarFrame3() {
super("Toolbar Example (Swing no text)");
setSize(450, 250);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// JPanel works much like Panel does, so we'll use it
JPanel toolbar = new JPanel();
toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
CCPHandler handler = new CCPHandler();
cutButton = new JButton(new ImageIcon("cut.gif"));
cutButton.setActionCommand(CCPHandler.CUT);
cutButton.addActionListener(handler);
toolbar.add(cutButton);
copyButton = new JButton(new ImageIcon("copy.gif"));
copyButton.setActionCommand(CCPHandler.COPY);
copyButton.addActionListener(handler);
toolbar.add(copyButton);
pasteButton = new JButton(new ImageIcon("paste.gif"));
pasteButton.setActionCommand(CCPHandler.PASTE);
pasteButton.addActionListener(handler);
toolbar.add(pasteButton);
add(toolbar, BorderLayout.NORTH);
// Add the look-and-feel controls
JPanel lnfPanel = new JPanel();
LnFListener lnfListener = new LnFListener(this);
macButton = new JButton("Mac");
macButton.addActionListener(lnfListener);
lnfPanel.add(macButton);
javaButton = new JButton("Metal");
javaButton.addActionListener(lnfListener);
lnfPanel.add(javaButton);
motifButton = new JButton("Motif");
motifButton.addActionListener(lnfListener);
lnfPanel.add(motifButton);
winButton = new JButton("Windows");
winButton.addActionListener(lnfListener);
lnfPanel.add(winButton);
add(lnfPanel, BorderLayout.SOUTH);
}
public static void main(String args[]) {
ToolbarFrame3 tf3 = new ToolbarFrame3();
tf3.setVisible(true);
}
}
<html>
<body>
<h1>Welcome</h1>
<hr>
Please feel free to browse our site:
<ul>
<li><a href="page1.html">Page 1</a>
<li><a href="page2.html">Page 2</a>
</ul>
<hr>