JRadioButtonTest.java Creates three radio buttons and illustrates handling ItemEvents in response to selecting a radio button. import javax.swing.JRadioButton; import javax.swing.ButtonGroup; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** */ public class JRadioButtonTest extends JPanel implements ItemListener { public JRadioButtonTest() { String[] labels = {"Java Swing","Java Servlets", "JavaServer Pages"}; JRadioButton[] buttons = new JRadioButton[3]; ButtonGroup group = new ButtonGroup(); for(int i=0; i
Aug 25
Creates three radio buttons and illustrates handling
Aug 25
Simple button that the user can select to load the entered URL.
JIconButton.java A simple button that the user can select to load the entered URL. import javax.swing.*; /** A regular JButton created with an ImageIcon and with borders * and content areas turned off. * */ public class JIconButton extends JButton { public JIconButton(String file) { super(new ImageIcon(file)); setContentAreaFilled(false); setBorderPainted(false); setFocusPainted(false); } }
Aug 25
A simple button that contains an image and a label for use in a toolbar
ToolBarButton.java A simple button that contains an image and a label for use in a toolbar. import java.awt.*; import javax.swing.*; /** Part of a small example showing basic use of JToolBar. * The point here is that dropping a regular JButton in a * JToolBar (or adding an Action) in JDK 1.2 doesn't give * you what you want -- namely, a small button just enclosing * the icon, and with text labels (if any) below the icon, * not to the right of it. In JDK 1.3, if you add an Action * to the toolbar, the Action label is no longer displayed. * */ public class ToolBarButton extends JButton { private static final Insets margins = new Insets(0, 0, 0, 0); public ToolBarButton(Icon icon) { super(icon); setMargin(margins); setVerticalTextPosition(BOTTOM); setHorizontalTextPosition(CENTER); } public ToolBarButton(String imageFile) { this(new ImageIcon(imageFile)); } public ToolBarButton(String imageFile, String text) { this(new ImageIcon(imageFile)); setText(text); } }
Aug 25
Demonstrates the use of a JColorChooser which presents a dialog with three different tabbed panes to allow the user to select a color preference
Demonstrates the use of a JColorChooser which presents a dialog with three different tabbed panes to allow the user to select a color preference. The dialog returns a Color object based on the user’s selection or null if the user entered Cancel.
import java.awt.*; import java.awt.event.*; import javax.swing.*; /** Simple example illustrating the use of internal frames. * * */ public class JInternalFrames extends JFrame { public static void main(String[] args) { new JInternalFrames(); } public JInternalFrames() { super("Multiple Document Interface"); WindowUtilities.setNativeLookAndFeel(); addWindowListener(new ExitListener()); Container content = getContentPane(); content.setBackground(Color.white); JDesktopPane desktop = new JDesktopPane(); desktop.setBackground(Color.white); content.add(desktop, BorderLayout.CENTER); setSize(450, 400); for(int i=0; i<5; i++) { JInternalFrame frame = new JInternalFrame(("Internal Frame " + i), true, true, true, true); frame.setLocation(i*50+10, i*50+10); frame.setSize(200, 150); frame.setBackground(Color.white); frame.setVisible(true); desktop.add(frame); frame.moveToFront(); } setVisible(true); } }
Aug 25
Creates various buttons. In Swing
import java.awt.*; import javax.swing.*; /** Simple example illustrating the use of JButton, especially * the new constructors that permit you to add an image. * */ public class JButtons extends JFrame { public static void main(String[] args) { new JButtons(); } public JButtons() { super("Using JButton"); WindowUtilities.setNativeLookAndFeel(); addWindowListener(new ExitListener()); Container content = getContentPane(); content.setBackground(Color.white); content.setLayout(new FlowLayout()); JButton button1 = new JButton("Java"); content.add(button1); ImageIcon cup = new ImageIcon("images/cup.gif"); JButton button2 = new JButton(cup); content.add(button2); JButton button3 = new JButton("Java", cup); content.add(button3); JButton button4 = new JButton("Java", cup); button4.setHorizontalTextPosition(SwingConstants.LEFT); content.add(button4); pack(); setVisible(true); } }
Aug 25
Multithreaded Graphics and Double Buffering
ShipSimulation.java Illustrates the basic approach of multithreaded graphics whereas a thread adjusts parameters affecting the appearance of the graphics and then calls repaint to schedule an update of the display.
import java.applet.Applet; import java.awt.*; public class ShipSimulation extends Applet implements Runnable { ... public void run() { Ship s; for(int i=0; i * When the "stop" button is pressed, stop the thread and * clear the Vector of circles. */ public void actionPerformed(ActionEvent event) { if (event.getSource() == startButton) { if (circles.size() == 0) { // Erase any circles from previous run. getGraphics().clearRect(0, 0, getSize().width, getSize().height); animationThread = new Thread(this); animationThread.start(); } int radius = 25; int x = radius + randomInt(width - 2 * radius); int y = radius + randomInt(height - 2 * radius); int deltaX = 1 + randomInt(10); int deltaY = 1 + randomInt(10); circles.addElement(new MovingCircle(x, y, radius, deltaX, deltaY)); } else if (event.getSource() == stopButton) { if (animationThread != null) { animationThread = null; circles.removeAllElements(); } } repaint(); } /** Each time around the loop, call paint and then take a * short pause. The paint method will move the circles and * draw them. */ public void run() { Thread myThread = Thread.currentThread(); // Really while animationThread not null while(animationThread==myThread) { repaint(); pause(100); } } /** Skip the usual screen-clearing step of update so that * there is no flicker between each drawing step. */ public void update(Graphics g) { paint(g); } /** Erase each circle's old position, move it, then draw it * in new location. */ public void paint(Graphics g) { MovingCircle circle; for(int i=0; i windowWidth) && (deltaX > 0)) { setDeltaX(-deltaX); } if ((y -radius < 0) && (deltaY < 0)) { setDeltaY(-deltaY); } else if((y + radius > windowHeight) && (deltaY > 0)) { setDeltaY(-deltaY); } } public int getDeltaX() { return(deltaX); } public void setDeltaX(int deltaX) { this.deltaX = deltaX; } public int getDeltaY() { return(deltaY); } public void setDeltaY(int deltaY) { this.deltaY = deltaY; } } *// SimpleCircle.java A class to store the x, y, and radius of a circle. Also, provides a draw method to paint the circle on the graphics object. import java.awt.*; /** A class to store an x, y, and radius, plus a draw method. * */ public class SimpleCircle { private int x, y, radius; public SimpleCircle(int x, int y, int radius) { setX(x); setY(y); setRadius(radius); } /** Given a Graphics, draw the SimpleCircle * centered around its current position. */ public void draw(Graphics g) { g.fillOval(x - radius, y - radius, radius * 2, radius * 2); } public int getX() { return(x); } public void setX(int x) { this.x = x; } public int getY() { return(y); } public void setY(int y) { this.y = y; } public int getRadius() { return(radius); } public void setRadius(int radius) { this.radius = radius; } } *// DoubleBufferBounce.java An enhancement to the previous applet containing bouncing circles. In this case, double buffering is used to improve the animation; all incremental updating is done in an off-screen image and then the image is drawn to the screen. import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.util.Vector; /** Bounce circles around on the screen, using double buffering * for speed and to avoid problems with overlapping circles. * Overrides update to avoid flicker problems. * */ public class DoubleBufferBounce extends Applet implements Runnable, ActionListener { private Vector circles; private int width, height; private Image offScreenImage; private Graphics offScreenGraphics; private Button startButton, stopButton; private Thread animationThread = null; public void init() { setBackground(Color.white); width = getSize().width; height = getSize().height; offScreenImage = createImage(width, height); offScreenGraphics = offScreenImage.getGraphics(); // Automatic in some systems, not in others. offScreenGraphics.setColor(Color.black); circles = new Vector(); startButton = new Button("Start a circle"); startButton.addActionListener(this); add(startButton); stopButton = new Button("Stop all circles"); stopButton.addActionListener(this); add(stopButton); } /** When the "start" button is pressed, start the animation * thread if it is not already started. Either way, add a * circle to the Vector of circles that are being bounced. * * When the "stop" button is pressed, stop the thread and * clear the Vector of circles. */ public void actionPerformed(ActionEvent event) { if (event.getSource() == startButton) { if (circles.size() == 0) { animationThread = new Thread(this); animationThread.start(); } int radius = 25; int x = radius + randomInt(width - 2 * radius); int y = radius + randomInt(height - 2 * radius); int deltaX = 1 + randomInt(10); int deltaY = 1 + randomInt(10); circles.addElement(new MovingCircle(x, y, radius, deltaX, deltaY)); repaint(); } else if (event.getSource() == stopButton) { if (animationThread != null) { animationThread = null; circles.removeAllElements(); } } } /** Each time around the loop, move each circle based on its * current position and deltaX/deltaY values. These values * reverse when the circles reach the edge of the window. */ public void run() { MovingCircle circle; Thread myThread = Thread.currentThread(); // Really while animationThread not null. while(animationThread==myThread) { for(int j=0; j= NUMIMAGES) { index = 0; } parent.repaint(); try { Thread.sleep(100); } catch (InterruptedException e) { break; // Break while loop. } } } } *// TimedAnimation.java An applet that demonstrates animation of an image by using a Timer. Note that Timer is located in the javax.swing package. import java.awt.*; import javax.swing.*; /** An example of performing animation through Swing timers. * Two timed Dukes are created with different timer periods. * */ public class TimedAnimation extends JApplet { private static final int NUMDUKES = 2; private TimedDuke[] dukes; private int i, index; public void init() { dukes = new TimedDuke[NUMDUKES]; setBackground(Color.white); dukes[0] = new TimedDuke( 1, 100, this); dukes[1] = new TimedDuke(-1, 500, this); } // Start each Duke timer. public void start() { for (int i=0; i= NUMIMAGES) { index = 0; } parent.repaint(); } // Public service to start the timer. public void startTimer() { timer.start(); } // Public service to stop the timer. public void stopTimer() { timer.stop(); } } **//
Aug 25
Driver class that creates three threaded objects (Counter2) that count from 0 to 4.
/** Try out a few instances of the Counter2 class. public class Counter2Test { public static void main(String[] args) { Counter2 c1 = new Counter2(5); Counter2 c2 = new Counter2(5); Counter2 c3 = new Counter2(5); } } /** A Runnable that counts up to a specified * limit with random pauses in between each count. public class Counter2 implements Runnable { private static int totalNum = 0; private int currentNum, loopLimit; public Counter2(int loopLimit) { this.loopLimit = loopLimit; currentNum = totalNum++; Thread t = new Thread(this); t.start(); } private void pause(double seconds) { try { Thread.sleep(Math.round(1000.0*seconds)); } catch(InterruptedException ie) {} } public void run() { for(int i=0; i
Aug 25
Template illustrating the second approach for creating a class with thread behavior.
Template illustrating the second approach for creating a class with thread behavior. In this case, the class implements the Runnable interface while providing a run method for thread execution.
public class ThreadedClass extends AnyClass implements Runnable { public void run() { // Thread behavior here. } public void startThread() { Thread t = new Thread(this); t.start(); // Calls back to the run method in "this." } ... }
Aug 25
Creates and starts three threaded objects
Creates and starts three threaded objects which count from 0 to 4. Uses the following class:
CounterTest.java
Counter.java
/** Try out a few instances of the Counter class.
public class CounterTest {
public static void main(String[] args) {
Counter c1 = new Counter(5);
Counter c2 = new Counter(5);
Counter c3 = new Counter(5);
c1.start();
c2.start();
c3.start();
}
}
Counter.java:A class that inherits from Thread and defines a run method that counts up to a specified value, pausing for a random time interval in between
value counts. /** A subclass of Thread that counts up to a specified * limit with random pauses in between each count. public class Counter extends Thread { private static int totalNum = 0; private int currentNum, loopLimit; public Counter(int loopLimit) { this.loopLimit = loopLimit; currentNum = totalNum++; } private void pause(double seconds) { try { Thread.sleep(Math.round(1000.0*seconds)); } catch(InterruptedException ie) {} } /** When run finishes, the thread exits. */ public void run() { for(int i=0; i
Aug 25
DOM example that represents the basic structure of an XML document as a JTree
//XMLTree.java //Uses the following files Uses the following files: * XMLFrame.java:Swing application to select an XML document and display in a JTree. ExtensionFileFilter.java Allows you to specify which file extensions will be displayed in a JFileChooser. test.xml Default file loaded if none selected by user. perennials.xml and perennials.dtd Data on daylilies and corresponding DTD. WindowUtilities.java ExitListener.java. //XMLTree.java as follows import java.awt.*; import javax.swing.*; import javax.swing.tree.*; import java.io.*; import org.w3c.dom.*; import javax.xml.parsers.*; /** Given a filename or a name and an input stream, * this class generates a JTree representing the * XML structure contained in the file or stream. * Parses with DOM then copies the tree structure * (minus text and comment nodes). * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * * © 2001 Marty Hall and Larry Brown; * may be freely used or adapted. */ public class XMLTree extends JTree { public XMLTree(String filename) throws IOException { this(filename, new FileInputStream(new File(filename))); } public XMLTree(String filename, InputStream in) { super(makeRootNode(in)); } // This method needs to be static so that it can be called // from the call to the parent constructor (super), which // occurs before the object is really built. private static DefaultMutableTreeNode makeRootNode(InputStream in) { try { // Use JAXP's DocumentBuilderFactory so that there // is no code here that is dependent on a particular // DOM parser. Use the system property // javax.xml.parsers.DocumentBuilderFactory (set either // from Java code or by using the -D option to "java"). // or jre_dir/lib/jaxp.properties to specify this. DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); // Standard DOM code from hereon. The "parse" // method invokes the parser and returns a fully parsed // Document object. We'll then recursively descend the // tree and copy non-text nodes into JTree nodes. Document document = builder.parse(in); document.getDocumentElement().normalize(); Element rootElement = document.getDocumentElement(); DefaultMutableTreeNode rootTreeNode = buildTree(rootElement); return(rootTreeNode); } catch(Exception e) { String errorMessage = "Error making root node: " + e; System.err.println(errorMessage); e.printStackTrace(); return(new DefaultMutableTreeNode(errorMessage)); } } private static DefaultMutableTreeNode buildTree(Element rootElement) { // Make a JTree node for the root, then make JTree // nodes for each child and add them to the root node. // The addChildren method is recursive. DefaultMutableTreeNode rootTreeNode = new DefaultMutableTreeNode(treeNodeLabel(rootElement)); addChildren(rootTreeNode, rootElement); return(rootTreeNode); } private static void addChildren (DefaultMutableTreeNode parentTreeNode, Node parentXMLElement) { // Recursive method that finds all the child elements // and adds them to the parent node. We have two types // of nodes here: the ones corresponding to the actual // XML structure and the entries of the graphical JTree. // The convention is that nodes corresponding to the // graphical JTree will have the word "tree" in the // variable name. Thus, "childElement" is the child XML // element whereas "childTreeNode" is the JTree element. // This method just copies the non-text and non-comment // nodes from the XML structure to the JTree structure. NodeList childElements = parentXMLElement.getChildNodes(); for(int i=0; i // JTree Node: blah // XML Element: // JTree Node: blah (foo=bar, baz=quux) private static String treeNodeLabel(Node childElement) { NamedNodeMap elementAttributes = childElement.getAttributes(); String treeNodeLabel = childElement.getNodeName(); if (elementAttributes != null && elementAttributes.getLength() > 0) { treeNodeLabel = treeNodeLabel + " ("; int numAttributes = elementAttributes.getLength(); for(int i=0; i 0) { treeNodeLabel = treeNodeLabel + ", "; } treeNodeLabel = treeNodeLabel + attribute.getNodeName() + "=" + attribute.getNodeValue(); } treeNodeLabel = treeNodeLabel + ")"; } return(treeNodeLabel); } } XMLFrame.java Swing application to select an XML document and display in a JTree. import java.awt.*; import javax.swing.*; import java.io.*; /** Invokes an XML parser on an XML document and displays * the document in a JTree. Both the parser and the * document can be specified by the user. The parser * is specified by invoking the program with * java -Djavax.xml.parsers.DocumentBuilderFactory=xxx XMLFrame * If no parser is specified, the Apache Xerces parser is used. * The XML document can be supplied on the command * line, but if it is not given, a JFileChooser is used * to interactively select the file of interest. * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * © 2001 Marty Hall and Larry Brown; * may be freely used or adapted. */ public class XMLFrame extends JFrame { public static void main(String[] args) { String jaxpPropertyName = "javax.xml.parsers.DocumentBuilderFactory"; // Pass the parser factory in on the command line with // -D to override the use of the Apache parser. if (System.getProperty(jaxpPropertyName) == null) { String apacheXercesPropertyValue = "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"; System.setProperty(jaxpPropertyName, apacheXercesPropertyValue); } String filename; if (args.length > 0) { filename = args[0]; } else { String[] extensions = { "xml", "tld" }; WindowUtilities.setNativeLookAndFeel(); filename = ExtensionFileFilter.getFileName(".", "XML Files", extensions); if (filename == null) { filename = "test.xml"; } } new XMLFrame(filename); } public XMLFrame(String filename) { try { WindowUtilities.setNativeLookAndFeel(); JTree tree = new XMLTree(filename); JFrame frame = new JFrame(filename); frame.addWindowListener(new ExitListener()); Container content = frame.getContentPane(); content.add(new JScrollPane(tree)); frame.pack(); frame.setVisible(true); } catch(IOException ioe) { System.out.println("Error creating tree: " + ioe); } } } ExtensionFileFilter.java Allows you to specify which file extensions will be displayed in a JFileChooser. import java.io.File; import java.util.*; import javax.swing.*; import javax.swing.filechooser.FileFilter; /** A FileFilter that lets you specify which file extensions * will be displayed. Also includes a static getFileName * method that users can call to pop up a JFileChooser for * a set of file extensions. * * Adapted from Sun SwingSet demo. * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * © 2001 Marty Hall and Larry Brown; * may be freely used or adapted. */ public class ExtensionFileFilter extends FileFilter { public static final int LOAD = 0; public static final int SAVE = 1; private String description; private boolean allowDirectories; private Hashtable extensionsTable = new Hashtable(); private boolean allowAll = false; public ExtensionFileFilter(boolean allowDirectories) { this.allowDirectories = allowDirectories; } public ExtensionFileFilter() { this(true); } public static String getFileName(String initialDirectory, String description, String extension) { String[] extensions = new String[]{ extension }; return(getFileName(initialDirectory, description, extensions, LOAD)); } public static String getFileName(String initialDirectory, String description, String extension, int mode) { String[] extensions = new String[]{ extension }; return(getFileName(initialDirectory, description, extensions, mode)); } public static String getFileName(String initialDirectory, String description, String[] extensions) { return(getFileName(initialDirectory, description, extensions, LOAD)); } /** Pops up a JFileChooser that lists files with the * specified extensions. If the mode is SAVE, then the * dialog will have a Save button; otherwise, the dialog * will have an Open button. Returns a String corresponding * to the file's pathname, or null if Cancel was selected. */ public static String getFileName(String initialDirectory, String description, String[] extensions, int mode) { ExtensionFileFilter filter = new ExtensionFileFilter(); filter.setDescription(description); for(int i=0; i WindowUtilities.java import javax.swing.*; import java.awt.*; // For Color and Container classes. /** A few utilities that simplify using windows in Swing. * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * * © 2001 Marty Hall and Larry Brown; * may be freely used or adapted. */ public class WindowUtilities { /** Tell system to use native look and feel, as in previous * releases. Metal (Java) LAF is the default otherwise. */ public static void setNativeLookAndFeel() { try { UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) { System.out.println("Error setting native LAF: " + e); } } public static void setJavaLookAndFeel() { try { UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName()); } catch(Exception e) { System.out.println("Error setting Java LAF: " + e); } } public static void setMotifLookAndFeel() { try { UIManager.setLookAndFeel( "com.sun.java.swing.plaf.motif.MotifLookAndFeel"); } catch(Exception e) { System.out.println("Error setting Motif LAF: " + e); } } /** A simplified way to see a JPanel or other Container. Pops * up a JFrame with specified Container as the content pane. */ public static JFrame openInJFrame(Container content, int width, int height, String title, Color bgColor) { JFrame frame = new JFrame(title); frame.setBackground(bgColor); content.setBackground(bgColor); frame.setSize(width, height); frame.setContentPane(content); frame.addWindowListener(new ExitListener()); frame.setVisible(true); return(frame); } /** Uses Color.white as the background color. */ public static JFrame openInJFrame(Container content, int width, int height, String title) { return(openInJFrame(content, width, height, title, Color.white)); } /** Uses Color.white as the background color, and the * name of the Container's class as the JFrame title. */ public static JFrame openInJFrame(Container content, int width, int height) { return(openInJFrame(content, width, height, content.getClass().getName(), Color.white)); } } EXITListener.java import java.awt.*; import java.awt.event.*; /** A listener that you attach to the top-level JFrame of * your application, so that quitting the frame exits the * application. * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * * © 2001 Marty Hall and Larry Brown; * may be freely used or adapted. */ public class ExitListener extends WindowAdapter { public void windowClosing(WindowEvent event) { System.exit(0); } }