import java.awt.*; import javax.swing.*; /** Tiny example showing the main differences in using * JApplet instead of Applet: using the content pane, * getting Java (Metal) look and feel by default, and * having BorderLayout be the default instead of FlowLayout. * */ public class JAppletExample extends JApplet { public void init() { WindowUtilities.setNativeLookAndFeel(); Container …
Category: FromSitesTree.com
May 13
JTree Examples #Programming Code Examples #Java/J2EE/J2ME #Advanced Swing
SimpleTree.java Basic tree built out of DefaultMutableTreeNodes. A DefualtMutableTreeNode is a starting point for a root node, in which children nodes can be added. import java.awt.*; import javax.swing.*; import javax.swing.tree.*; /** Example tree built out of DefaultMutableTreeNodes. * */ public class SimpleTree extends JFrame { public static void main(String[] args) { new SimpleTree(); } public …
May 13
JTable Examples #Programming Code Examples #Java/J2EE/J2ME #Advanced Swing
# JTableSimpleExample.java Simple table that takes column names and data from arrays of Strings. import java.awt.*; import javax.swing.*; /** Simple JTable example that uses a String array for the * table header and table data. * */ public class JTableSimpleExample extends JFrame { public static void main(String[] args) { new JTableSimpleExample(); } private final int …
May 13
Printing in Java 2 #Programming Code Examples #Java/J2EE/J2ME #Advanced Swing
* o PrintExample.java Demonstrates printing a Graphics2D object. import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.awt.print.*; /** An example of a printable window in Java 1.2. The key point * here is that any component is printable in Java 1.2. * However, you have to be careful to turn off double buffering * globally (not …
May 13
Buggy Counter Applet.java Demonstrates that data shared by multiple threads is candidate for a potential race condition #Programming Code Examples #Java/J2EE/J2ME #Advanced Swing
import java.applet.Applet; import java.awt.*; /** Emulates the Counter and Counter2 classes, but this time * from an applet that invokes multiple versions of its own run * method. This version is likely to work correctly * except when an important customer is visiting. public class BuggyCounterApplet extends Applet implements Runnable{ private int totalNum = 0; …
May 13
ThreadedRSAKey.java Illustrates converting a method in an existing class from a single-threaded method to a multi-threaded method. #Programming Code Examples #Java/J2EE/J2ME #Advanced Swing
ThreadedRSAKey.java Illustrates converting a method in an existing class from a single-threaded method to a multi-threaded method. In this example, RSAKey computes an RSA public-private key pair, where the key size has a specified number of digits. As large prime numbers require considerable CPU time, ThreadedRSAKey converts the original computeKey method in RSAKey to a …
May 13
JList Examples #Programming Code Examples #Java/J2EE/J2ME #Advanced Swing
All examples, except for FileTransfer use WindowUtilities.java and ExitListener.java. WindowUtilities.java: import javax.swing.*; import java.awt.*; // For Color and Container classes. /** A few utilities that simplify using windows in Swing. * */ public class WindowUtilities { /** Tell system to use native look and feel, as in previous * releases. Metal (Java) LAF is the …
May 13
Creates and starts three threaded objects #Programming Code Examples #Java/J2EE/J2ME #Advanced Swing
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(); …
May 13
Template illustrating the second approach for creating a class with thread behavior. #Programming Code Examples #Java/J2EE/J2ME #Advanced Swing
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); …
May 13
Counter2Test.java Driver class that creates three threaded objects (Counter2) that count from 0 to 4. #Programming Code Examples #Java/J2EE/J2ME #Advanced Swing
/** Try out a few instances of the Counter2 class. Driver class that creates three threaded objects (Counter2) that count from 0 to 4. In this case, the driver does not start the threads, as each thread is automatically started in Counter2’s constructor. Uses the following class: Counter2Test.java Counter2.java ************************** public class Counter2Test { public …
