SearchSpec.java ******************* /** Small class that encapsulates how to construct a * search string for a particular search engine. * Taken from Core Web Programming Java 2 Edition * from Prentice Hall and Sun Microsystems Press, * May be freely used or adapted. */ public class SearchSpec { private String name, baseURL, numResultsSuffix; …
Category: Code . Programming Samples . প্রোগ্রাম উদাহরন
Code . Programming Samples . প্রোগ্রাম উদাহরন
Permanent link to this article: http://bangla.sitestree.com/a-class-the-encapsulates-the-urls-used-by-various-search-engines/
DebugExample.jsp Page that uses the DebugTag custom tag
# DebugExample.jsp Page that uses the DebugTag custom tag <!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”> <!– Illustration of SimplePrimeTag tag. Taken from Core Web Programming Java 2 Edition from Prentice Hall and Sun Microsystems Press, . May be freely used or adapted. –> <HTML> <HEAD> <TITLE>Using the Debug Tag</TITLE> <LINK REL=STYLESHEET HREF=”JSP-Styles.css” …
Permanent link to this article: http://bangla.sitestree.com/debugexample-jsp-page-that-uses-the-debugtag-custom-tag/
ShearExample.java. Illustrates the effect of applying a shear transformation prior to drawing a square
import javax.swing.*; import java.awt.*; import java.awt.geom.*; /** An example of shear transformations on a rectangle. * *********************** public class ShearExample extends JPanel { private static int gap=10, width=100; private Rectangle rect = new Rectangle(gap, gap, 100, 100); public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D)g; for (int …
Permanent link to this article: http://bangla.sitestree.com/shearexample-java-illustrates-the-effect-of-applying-a-shear-transformation-prior-to-drawing-a-square/
LineStyles.java Provides examples of the available styles for joining line segments
import javax.swing.*; import java.awt.*; import java.awt.geom.*; /** A demonstration of different controls when joining two line * segments. The style of the line end point is controlled * through the capStyle parameter. * ************************************ public class LineStyles extends JPanel { private GeneralPath path; private static int x = 30, deltaX = 150, y …
Permanent link to this article: http://bangla.sitestree.com/linestyles-java-provides-examples-of-the-available-styles-for-joining-line-segments/
Illustrates using a local font (Goudy Handtooled BT) to perform drawing in Java 2D
import java.awt.*; /** An example of using local fonts to perform drawing in * Java 2D. * ********************** public class FontExample extends GradientPaintExample { public FontExample() { GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); env.getAvailableFontFamilyNames(); setFont(new Font(“Goudy Handtooled BT”, Font.PLAIN, 100)); } protected void drawBigString(Graphics2D g2d) { g2d.setPaint(Color.black); g2d.drawString(“Java …
Permanent link to this article: http://bangla.sitestree.com/illustrates-using-a-local-font-goudy-handtooled-bt-to-perform-drawing-in-java-2d/
Draws a circle with a gradient fill
GradientPaintExample.java Draws a circle with a gradient fill. Inherits from ShapeExample.java. ************************************** import java.awt.*; /** An example of applying a gradient fill to a circle. The * color definition starts with red at (0,0), gradually * changing to yellow at (175,175). * ********************************** public class GradientPaintExample extends ShapeExample { private GradientPaint gradient = …
Permanent link to this article: http://bangla.sitestree.com/draws-a-circle-with-a-gradient-fill/
An applet that permits freehand drawing
import java.applet.Applet; import java.awt.*; import java.awt.event.*; /** An applet that lets you perform freehand drawing. * **************** public class SimpleWhiteboard extends Applet { protected int lastX=0, lastY=0; public void init() { setBackground(Color.white); setForeground(Color.blue); addMouseListener(new PositionRecorder()); addMouseMotionListener(new LineDrawer()); } protected void record(int x, int y) { …
Permanent link to this article: http://bangla.sitestree.com/an-applet-that-permits-freehand-drawing/
A TextField that uses key events to correct the spelling of the names of computer languages entered into it
import java.awt.*; import java.awt.event.*; /** A spelling-correcting TextField for entering * a language name. * ******************* public class LanguageField extends TextField { private String[] substrings = { “”, “J”, “Ja”, “Jav”, “Java” }; public LanguageField() { addKeyListener(new SpellingCorrector()); addActionListener(new WordCompleter()); addFocusListener(new SubliminalAdvertiser()); } // Put caret …
Permanent link to this article: http://bangla.sitestree.com/a-textfield-that-uses-key-events-to-correct-the-spelling-of-the-names-of-computer-languages-entered-into-it/
DebugTag.java Custom tag that optionally makes use of a tag body
DebugTag.java Custom tag that optionally makes use of a tag body package cwp.tags; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; import javax.servlet.*; /** A tag that includes the body content only if * the “debug” request parameter is set. * <P> * Taken from Core Web Programming Java 2 Edition * from Prentice Hall and Sun …
Permanent link to this article: http://bangla.sitestree.com/debugtag-java-custom-tag-that-optionally-makes-use-of-a-tag-body/
HeadingTag.java Custom tag that makes use of a tag body
HeadingTag.java Custom tag that makes use of a tag body package cwp.tags; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; /** Generates an HTML heading with the specified background * color, foreground color, alignment, font, and font size. * You can also turn on a border around it, which normally * just barely encloses the heading, but …
Permanent link to this article: http://bangla.sitestree.com/headingtag-java-custom-tag-that-makes-use-of-a-tag-body/
PrimeTag.java Custom tag that outputs a random prime number of a user-specifiable approximate length
PrimeTag.java Custom tag that outputs a random prime number of a user-specifiable approximate length package cwp.tags; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; /** Generates an N-digit random prime (default N = 50). * Extends SimplePrimeTag, adding a length attribute * to set the size of the prime. The doStartTag * method of the parent class …
Permanent link to this article: http://bangla.sitestree.com/primetag-java-custom-tag-that-outputs-a-random-prime-number-of-a-user-specifiable-approximate-length/
SimplePrimeTag.java Custom tag that outputs a random prime number of a fixed approximate length
SimplePrimeTag.java Custom tag that outputs a random prime number of a fixed approximate length package cwp.tags; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; import java.math.*; import cwp.*; /** Generates a prime of approximately 50 digits. * (50 is actually the length of the random number * generated — the first prime above that number will * …
Permanent link to this article: http://bangla.sitestree.com/simpleprimetag-java-custom-tag-that-outputs-a-random-prime-number-of-a-fixed-approximate-length/
cwp-taglib.tld Tag Library Descriptor file used by the custom tags in this chapter.
cwp-taglib.tld Tag Library Descriptor file used by the custom tags in this chapter. <?xml version=”1.0″ encoding=”ISO-8859-1″ ?> <!DOCTYPE taglib PUBLIC “-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN” “http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd”> <!– a tag library descriptor –> <taglib> <!– after this the default space is “http://java.sun.com/j2ee/dtds/jsptaglibrary_1_2.dtd” –> <tlibversion>1.0</tlibversion> <jspversion>1.1</jspversion> <shortname>cwp</shortname> <!– …
Permanent link to this article: http://bangla.sitestree.com/cwp-taglib-tld-tag-library-descriptor-file-used-by-the-custom-tags-in-this-chapter/
SharedCounts1.jsp, SharedCounts2.jsp, and SharedCounts3.jsp Three pages that all use the AccessCountBean. Results you get depend on which page is hit first and how many total hits the combined pages receive.
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”> <!– Example of sharing beans. Taken from Core Web Programming Java 2 Edition from Prentice Hall and Sun Microsystems Press, . May be freely used or adapted. –> <HTML> <HEAD> <TITLE>Shared Access Counts: Page 1</TITLE> <LINK REL=STYLESHEET HREF=”JSP-Styles.css” TYPE=”text/css”> </HEAD> <BODY> <TABLE BORDER=5 ALIGN=”CENTER”> <TR><TH …
Permanent link to this article: http://bangla.sitestree.com/sharedcounts1-jsp-sharedcounts2-jsp-and-sharedcounts3-jsp-three-pages-that-all-use-the-accesscountbean-results-you-get-depend-on-which-page-is-hit-first-and-how-many-total-hits-the-combined-pages-r/
SaleEntry3.jsp Page that uses the SaleEntry bean, using property=”*” to read request parameters and assign them to bean properties
SaleEntry3.jsp Page that uses the SaleEntry bean, using property=”*” to read request parameters and assign them to bean properties <!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”> <!– Example of using jsp:setProperty and a general association with the input parameters. See SaleEntry1.jsp and SaleEntry2.jsp for alternatives. Taken from Core Web Programming Java 2 Edition …
Permanent link to this article: http://bangla.sitestree.com/saleentry3-jsp-page-that-uses-the-saleentry-bean-using-property-to-read-request-parameters-and-assign-them-to-bean-properties/
SaleEntry1.jsp Page that uses the SaleEntry bean, using explicit Java code to read request parameters and assign them to bean properties.
SaleEntry1.jsp Page that uses the SaleEntry bean, using explicit Java code to read request parameters and assign them to bean properties. <!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”> <!– Example of using jsp:setProperty with an explicit value supplied to the “value” attribute. See SaleEntry2.jsp and SaleEntry3.jsp for alternatives. Taken from Core Web Programming Java …
Permanent link to this article: http://bangla.sitestree.com/saleentry1-jsp-page-that-uses-the-saleentry-bean-using-explicit-java-code-to-read-request-parameters-and-assign-them-to-bean-properties/
StringBean.jsp Page that manipulates the StringBean bean with both jsp:useBean (i.e., XML-style) syntax
StringBean.jsp Page that manipulates the StringBean bean with both jsp:useBean (i.e., XML-style) syntax package cwp; /** Simple bean to illustrate sharing beans through * use of the scope attribute of jsp:useBean. * <P> * Taken from Core Web Programming Java 2 Edition * from Prentice Hall and Sun Microsystems Press, * . * May be …
Permanent link to this article: http://bangla.sitestree.com/stringbean-jsp-page-that-manipulates-the-stringbean-bean-with-both-jspusebean-i-e-xml-style-syntax/
RepeatTag.java Custom tag that repeats the tag body a specified number of times
RepeatTag.java Custom tag that repeats the tag body a specified number of times package cwp.tags; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; /** A tag that repeats the body the specified * number of times. * <P> * Taken from Core Web Programming Java 2 Edition * from Prentice Hall and Sun Microsystems Press, * . …
Permanent link to this article: http://bangla.sitestree.com/repeattag-java-custom-tag-that-repeats-the-tag-body-a-specified-number-of-times/
An applet that reads arrays of strings packaged inside a QueryCollection and places them in a scrolling TextArea.
import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.net.*; /** Applet reads arrays of strings packaged inside * a QueryCollection and places them in a scrolling * TextArea. The QueryCollection obtains the strings * by means of a serialized object input stream * connected to the QueryGenerator servlet. * * Taken from Core Web Programming …
Permanent link to this article: http://bangla.sitestree.com/an-applet-that-reads-arrays-of-strings-packaged-inside-a-querycollection-and-places-them-in-a-scrolling-textarea/
FilterTag.java Custom tag that modifies the tag body
FilterTag.java Custom tag that modifies the tag body package cwp.tags; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; import cwp.*; /** A tag that replaces <, >, “, and & with their HTML * character entities (<, >, “, and &). * After filtering, arbitrary strings can be placed * in either the page body or in …
Permanent link to this article: http://bangla.sitestree.com/filtertag-java-custom-tag-that-modifies-the-tag-body/
An applet that searches multiple search engines, displaying the results in side-by-side frame cells.
Using Applets as Front Ends to Server-Side Programs ************************************************** SearchApplet.java An applet that searches multiple search engines, displaying the results in side-by-side frame cells. Uses the following files: SearchSpec.javaParallelSearches.htmlSearchAppletFrame.htmlGoogleResultsFrame.htmlInfoseekResultsFrame.htmlLycosResultsFrame.html *************************************************** // import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.net.*; // /** An applet that reads a value from a TextField, * then uses it to …
Permanent link to this article: http://bangla.sitestree.com/an-applet-that-searches-multiple-search-engines-displaying-the-results-in-side-by-side-frame-cells/
RotationExample.java An example of translating and rotating the coordinate system prior to drawing
import java.awt.*; /** An example of translating and rotating the coordinate * system before each drawing. * ******************************* public class RotationExample extends StrokeThicknessExample { private Color[] colors = { Color.white, Color.black }; public void paintComponent(Graphics g) { clear(g); Graphics2D g2d = (Graphics2D)g; drawGradientCircle(g2d); drawThickCircleOutline(g2d); // Move the origin …
Permanent link to this article: http://bangla.sitestree.com/rotationexample-java-an-example-of-translating-and-rotating-the-coordinate-system-prior-to-drawing/
Demonstrates setting the pen width (in pixels) using a BasicStroke prior to drawing. Inherits from FontExample.java.
StrokeThicknessExample.java >>>>>>>>>>>>>>>>>>>>>>>>>>> import java.awt.*; /** An example of controlling the Stroke (pen) widths when * drawing. * ****************** */ public class StrokeThicknessExample extends FontExample { public void paintComponent(Graphics g) { clear(g); Graphics2D g2d = (Graphics2D)g; drawGradientCircle(g2d); drawBigString(g2d); drawThickCircleOutline(g2d); } protected void drawThickCircleOutline(Graphics2D g2d) { g2d.setPaint(Color.blue); …
Permanent link to this article: http://bangla.sitestree.com/demonstrates-setting-the-pen-width-in-pixels-using-a-basicstroke-prior-to-drawing-inherits-from-fontexample-java/
ListFonts.java Lists all local fonts available for graphical drawing.
ListFonts.java Lists all local fonts available for graphical drawing. *********************** import java.awt.*; /** Lists the names of all available fonts. * ****************** public class ListFonts { public static void main(String[] args) { GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); String[] fontNames = env.getAvailableFontFamilyNames(); System.out.println(“Available Fonts:”); for(int i=0; i>>>>>>>>>>>>>>>>>>>>>>>
Permanent link to this article: http://bangla.sitestree.com/listfonts-java-lists-all-local-fonts-available-for-graphical-drawing/
Illustrates the effect of different transparency
~~~~~~~~~~~~~~~~~~~ TransparencyExample.java Illustrates the effect of different transparency (alpha) values when drawing a shape. ~~~~~~~~~~~~~~~~~~~ import javax.swing.*; import java.awt.*; import java.awt.geom.*; /** An illustration of the use of AlphaComposite to make * partially transparent drawings. * ********************************** public class TransparencyExample extends JPanel { private static int gap=10, width=60, offset=20, deltaX=gap+width+offset; private Rectangle …
Permanent link to this article: http://bangla.sitestree.com/illustrates-the-effect-of-different-transparency/
Handling Events
*********************************** * ActionExample1.java Inherits from CloseableFrame.java and uses SetSizeButton.java. * ActionExample2.java Inherits from CloseableFrame.java. ********************************************************** ActionExample1.java ******************* import java.awt.*; public class ActionExample1 extends CloseableFrame { public static void main(String[] args) { new ActionExample1(); } public ActionExample1() { super(“Handling Events in Component”); setLayout(new FlowLayout()); setFont(new Font(“Serif”, …
Permanent link to this article: http://bangla.sitestree.com/handling-events/
A Frame that uses the Confirm dialog to verify quit
ConfirmTest.java **************** import java.awt.*; import java.awt.event.*; /** A Frame that uses the Confirm dialog to verify that * users really want to quit. * public class ConfirmTest extends Frame { public static void main(String[] args) { new ConfirmTest(); } public ConfirmTest() { super(“Confirming QUIT”); setSize(200, 200); addWindowListener(new …
Permanent link to this article: http://bangla.sitestree.com/a-frame-that-uses-the-confirm-dialog-to-verify-quit/
CircleDrawer3.java Applet that uses a named nested class to handle mouse events and draw circles. Illustrates third approach to event-handling with listeners: using inner classes.
import java.applet.Applet; import java.awt.*; import java.awt.event.*; /** Draw circles centered where the user clicks. * Uses named inner classes. * ****************** public class CircleDrawer3 extends Applet { public void init() { setForeground(Color.blue); addMouseListener(new CircleListener()); } private class CircleListener extends MouseAdapter { private int radius = 25; public …
Permanent link to this article: http://bangla.sitestree.com/circledrawer3-java-applet-that-uses-a-named-nested-class-to-handle-mouse-events-and-draw-circles-illustrates-third-approach-to-event-handling-with-listeners-using-inner-classes/
Applet handle mouse events
ClickReporter.java A simple applet that uses the class to handle mouse events import java.applet.Applet; import java.awt.*; /** Prints a message saying where the user clicks. * Uses an external listener. * public class ClickReporter extends Applet { public void init() { setBackground(Color.yellow); addMouseListener(new ClickListener()); } }
Permanent link to this article: http://bangla.sitestree.com/applet-handle-mouse-events/
Custom AWT Slider
***************** Custom AWT Slider * LabeledCostSlider.java. A numeric slider class with attached label. * CostSlider.java. A slider class that lets you read numeric values. Used in the LabeledCostSlider class. * Slider.java. A slider class: a combination of Scrollbar and TextField. Used in the CostSlider class. * ScrollbarPanel.java A Panel with adjustable …
Permanent link to this article: http://bangla.sitestree.com/custom-awt-slider/
Layout of a complicated GUI interface with GridLayout
################################## GridBagTest.java Layout of a complicated GUI interface with GridLayout. Uses WindowUtilities.java and ExitListener.java. ################################## import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.border.*; /** An example demonstrating a GridBagLayout GUI with * input text area and multiple buttons. * ********* public class GridBagTest extends JPanel { private JTextArea textArea; private JButton …
Permanent link to this article: http://bangla.sitestree.com/layout-of-a-complicated-gui-interface-with-gridlayout/
TextAreas
TextAreas.java ************** import java.applet.Applet; import java.awt.*; /././././././,/././././ public class TextAreas extends Applet { public void init() { setBackground(Color.lightGray); add(new TextArea(3, 10)); add(new TextArea(“Some\nInitial\nText”, 3, 10)); } }
Permanent link to this article: http://bangla.sitestree.com/textareas/
Lists.java
Lists.java Inherits from CloseableFrame.java. /./././././././././ import java.awt.*; /*****************/ public class Lists extends CloseableFrame { public static void main(String[] args) { new Lists(); } public Lists() { super(“Lists”); setLayout(new FlowLayout()); setBackground(Color.lightGray); setFont(new Font(“SansSerif”, Font.BOLD, 18)); List list1 = new List(3, false); list1.add(“Vanilla”); list1.add(“Chocolate”); list1.add(“Strawberry”); …
Permanent link to this article: http://bangla.sitestree.com/lists-java/
ChoiceTest
import java.applet.Applet; import java.awt.*; /*******/ public class ChoiceTest extends Applet { private Choice choice; public void init() { setFont(new Font(“SansSerif”, Font.BOLD, 36)); choice = new Choice(); choice.addItem(“Choice 1”); choice.addItem(“Choice 2”); choice.addItem(“Choice 3”); add(choice); } }
Permanent link to this article: http://bangla.sitestree.com/choicetest/
ButtonExample.java Uses the following
/./././././././././ # ButtonExample.java Uses the following classes: * CloseableFrame.java * FgReporter.java * BgReporter.java * SizeReporter.java ****************** ButtonExample.java ****************** import java.awt.*; import java.awt.event.*; /././././././././././ public class ButtonExample extends CloseableFrame { public static void main(String[] args) { new ButtonExample(); } public ButtonExample() { super(“Using ActionListeners”); setLayout(new FlowLayout()); …
Permanent link to this article: http://bangla.sitestree.com/buttonexample-java-uses-the-following/
Uses a FileDialog to choose the file to display
DisplayFile.java **************** import java.awt.*; import java.awt.event.*; import java.io.*; /** Uses a FileDialog to choose the file to display. *************** public class DisplayFile extends CloseableFrame implements ActionListener { public static void main(String[] args) { new DisplayFile(); } private Button loadButton; private TextArea fileArea; private FileDialog loader; …
Permanent link to this article: http://bangla.sitestree.com/uses-a-filedialog-to-choose-the-file-to-display/
Applet that uses a anonymous nested class to handle mouse events and draw circles. Variation on third approach to event-handling: using inner classes.
import java.applet.Applet; import java.awt.*; import java.awt.event.*; /** Draw circles centered where the user clicks. * Uses anonymous inner classes. * ********************** public class CircleDrawer4 extends Applet { public void init() { setForeground(Color.blue); addMouseListener (new MouseAdapter() { private int radius = 25; public void mousePressed(MouseEvent event) { Graphics …
Permanent link to this article: http://bangla.sitestree.com/applet-that-uses-a-anonymous-nested-class-to-handle-mouse-events-and-draw-circles-variation-on-third-approach-to-event-handling-using-inner-classes/
Tiny applet that uses CircleListener to handle mouse events.
import java.applet.Applet; import java.awt.*; /** Draw circles centered where the user clicks. * Uses an external listener. * *********** public class CircleDrawer1 extends Applet { public void init() { setForeground(Color.blue); addMouseListener(new CircleListener()); } }
Permanent link to this article: http://bangla.sitestree.com/tiny-applet-that-uses-circlelistener-to-handle-mouse-events/
Subclass of MouseAdapter
!!!!!!!!!!!! ClickListener.java A simple subclass of MouseAdapter that reports where the mouse was pressed. When attached to an applet, look for the report in the Java Console. !!!!!!!!!!!! import java.awt.event.*; /** The listener used by ClickReporter. * ************** public class ClickListener extends MouseAdapter { public void mousePressed(MouseEvent event) { System.out.println(“Mouse pressed at …
Permanent link to this article: http://bangla.sitestree.com/subclass-of-mouseadapter/
Create PopupMenu and add MenuItems
import java.applet.Applet; import java.awt.*; import java.awt.event.*; ************************ /** Simple demo of pop-up menus. * ******************** public class ColorPopupMenu extends Applet implements ActionListener { private String[] colorNames = { “White”, “Light Gray”, “Gray”, “Dark Gray”, “Black” }; private Color[] colors = { Color.white, Color.lightGray, Color.gray, Color.darkGray, Color.black }; private PopupMenu …
Permanent link to this article: http://bangla.sitestree.com/create-popupmenu-and-add-menuitems/
ReverseLabels.java Inherits from CloseableFrame.java and uses ReversibleLabel.java.
ReverseLabels.java Inherits from CloseableFrame.java and uses ReversibleLabel.java. ********************** ReverseLabels.java ********************** import java.awt.*; ****************** public class ReverseLabels extends CloseableFrame { public static void main(String[] args) { new ReverseLabels(); } public ReverseLabels() { super(“Reversible Labels”); setLayout(new FlowLayout()); setBackground(Color.lightGray); setFont(new Font(“Serif”, Font.BOLD, 18)); ReversibleLabel label1 = new ReversibleLabel(“Black …
Permanent link to this article: http://bangla.sitestree.com/reverselabels-java-inherits-from-closeableframe-java-and-uses-reversiblelabel-java/
TextFields
import java.applet.Applet; import java.awt.*; /** A TextField from each of the four constructors. * ********************* public class TextFields extends Applet { public void init() { add(new TextField()); add(new TextField(30)); add(new TextField(“Initial String”)); add(new TextField(“Initial”, 30)); } }
Permanent link to this article: http://bangla.sitestree.com/textfields/
ChoiceTest2
ChoiceTest2.java /././././././././ import java.applet.Applet; import java.awt.*; import java.awt.event.*; /***********************/ public class ChoiceTest2 extends Applet implements ItemListener { private Choice choice; public void init() { setFont(new Font(“SansSerif”, Font.BOLD, 36)); choice = new Choice(); choice.addItem(“Choice 1”); choice.addItem(“Choice 2”); choice.addItem(“Choice 3”); choice.addItemListener(this); add(choice); } public void …
Permanent link to this article: http://bangla.sitestree.com/choicetest2/
CheckboxGroups
CheckboxGroups.java /./././././././././ ///////////////////// import java.applet.Applet; import java.awt.*; //////////////////// public class CheckboxGroups extends Applet { public void init() { setLayout(new GridLayout(4, 2)); setBackground(Color.lightGray); setFont(new Font(“Serif”, Font.BOLD, 16)); add(new Label(“Flavor”, Label.CENTER)); add(new Label(“Toppings”, Label.CENTER)); CheckboxGroup flavorGroup = new CheckboxGroup(); add(new Checkbox(“Vanilla”, flavorGroup, true)); add(new Checkbox(“Colored Sprinkles”)); …
Permanent link to this article: http://bangla.sitestree.com/checkboxgroups/
A textfield and three buttons arranged by a verticle BoxLayout
BoxLayoutTest.java A textfield and three buttons arranged by a verticle BoxLayout. Uses WindowUtilities.java and ExitListener.java. ################## import java.awt.*; import java.awt.event.*; import javax.swing.*; /** An example of BoxLayout. * *********** public class BoxLayoutTest extends JPanel implements ActionListener{ BoxLayout layout; JButton topButton, middleButton, bottomButton; public BoxLayoutTest() { layout = new BoxLayout(this, BoxLayout.Y_AXIS); …
Permanent link to this article: http://bangla.sitestree.com/a-textfield-and-three-buttons-arranged-by-a-verticle-boxlayout/
Implementation of a simple browser in Swing (The user can specify a URL to load into the browser (JEditorPane))
Browser.java Implementation of a simple browser in Swing. The user can specify a URL to load into the browser (JEditorPane). By attaching an Hyperlink Listener, the editor pane is responsive to hyperlinks selected by the user. Uses the following class and image: import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*; /** …
Permanent link to this article: http://bangla.sitestree.com/implementation-of-a-simple-browser-in-swing-the-user-can-specify-a-url-to-load-into-the-browser-jeditorpane/
A simple applet (JApplet) created in Swing.
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() …
Permanent link to this article: http://bangla.sitestree.com/a-simple-applet-japplet-created-in-swing/
Illustrates the insertion of menu entries in Frame menu bars.
************** ColorMenu.java ************** import java.awt.*; import java.awt.event.*; /** Illustrates the insertion of menu entries in Frame * menu bars. * public class ColorMenu extends CloseableFrame implements ActionListener { private String[] colorNames = { “Black”, “White”, “Light Gray”, “Medium Gray”, “Dark Gray” }; private Color[] colorValues = …
Permanent link to this article: http://bangla.sitestree.com/illustrates-the-insertion-of-menu-entries-in-frame-menu-bars/
JTable Examples
# 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(); } …
Permanent link to this article: http://bangla.sitestree.com/jtable-examples/
Eight buttons: four each in two panels
mport java.applet.Applet; import java.awt.*; *************************** /** Eight buttons: four each in two panels. * */ public class ButtonTest2 extends Applet { public void init() { String[] labelPrefixes = { “Start”, “Stop”, “Pause”, “Resume” }; Panel p1 = new Panel(); for (int i=0; i<4; i++) { p1.add(new Button(labelPrefixes[i] + ” Thread1″)); } Panel p2 = new …
Permanent link to this article: http://bangla.sitestree.com/eight-buttons-four-each-in-two-panels/
JList Examples
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) …
Permanent link to this article: http://bangla.sitestree.com/jlist-examples/
Position circles down the diagonal so that their borders
import java.awt.*; import java.applet.Applet; /** Position circles down the diagonal so that their borders * just touch. Illustrates that AWT components are * rectangular and opaque. */ public class CircleTest2 extends Applet { public void init() { setBackground(Color.lightGray); setLayout(null); // Turn off layout manager. Circle circle; int radius = …
Permanent link to this article: http://bangla.sitestree.com/position-circles-down-the-diagonal-so-that-their-borders/
Insert three circles into an Applet using FlowLayout
import java.awt.*; import java.applet.Applet; /** Insert three circles into an Applet using FlowLayout. * */ public class CircleTest extends Applet { public void init() { setBackground(Color.lightGray); add(new Circle(Color.white, 30)); add(new Circle(Color.gray, 40)); add(new Circle(Color.black, 50)); } }
Permanent link to this article: http://bangla.sitestree.com/insert-three-circles-into-an-applet-using-flowlayout/
Message.java Applet that reads customization parameters from an HTML file
******************* Message.java Applet that reads customization parameters from an HTML file ******************* import java.applet.Applet; import java.awt.*; **************** public class Message extends Applet { private int fontSize; private String message; public void init() { setBackground(Color.black); setForeground(Color.white); // Base font size on window height. fontSize = …
Permanent link to this article: http://bangla.sitestree.com/message-java-applet-that-reads-customization-parameters-from-an-html-file/
Accesses methods in a Ship2 object
********************************************* Test2.java Accesses methods in a Ship2 object ********************************************* // Give the ship public move and printLocation methods. class Ship2 { public double x=0.0, y=0.0, speed=1.0, direction=0.0; public String name = “UnnamedShip”; private double degreesToRadians(double degrees) { return(degrees * Math.PI / 180.0); } public void move() { double …
Permanent link to this article: http://bangla.sitestree.com/accesses-methods-in-a-ship2-object/
Placement of buttons in a BoxLayout using rigid areas, struts, and glue
############### InvisibleComponentTest.java Placement of buttons in a BoxLayout using rigid areas, struts, and glue ############### import java.awt.*; import javax.swing.*; import javax.swing.border.*; /** Example of using rigid areas, struts, and glue to * produce the effect of invisible components. * ****************** public class InvisibleComponentTest extends JPanel { Component spacer; public InvisibleComponentTest() { setLayout(new …
Permanent link to this article: http://bangla.sitestree.com/placement-of-buttons-in-a-boxlayout-using-rigid-areas-struts-and-glue/
Adds typing to the freehand drawing.
import java.applet.Applet; import java.awt.*; import java.awt.event.*; /** A better whiteboard that lets you enter * text in addition to freehand drawing. * ****************** public class Whiteboard extends SimpleWhiteboard { protected FontMetrics fm; public void init() { super.init(); Font font = new Font(“Serif”, Font.BOLD, 20); setFont(font); fm = getFontMetrics(font); …
Permanent link to this article: http://bangla.sitestree.com/adds-typing-to-the-freehand-drawing/
java Nested container where the top-level panels are positioned by hand
###################### ButtonCol.java Nested container where the top-level panels are positioned by hand ###################### import java.applet.Applet; import java.awt.*; /** An example of a layout performed manually. The top-level * panels are positioned by hand, after you determine the size * of the applet. Since applets can’t be resized in most * browsers, setting the size once …
Permanent link to this article: http://bangla.sitestree.com/java-nested-container-where-the-top-level-panels-are-positioned-by-hand/
Layout of complicated GUI by taking advantage of nested containers
################# NestedLayout.java Layout of complicated GUI by taking advantage of nested containers. Uses WindowUtilities.java and ExitListener.java. ################## import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.border.*; import javax.swing.event.*; /** An example demonstrating the use of nested containers * to lay out the components. See GridBagTest.java for * implementation by a single layout manager, GridBagLayout. …
Permanent link to this article: http://bangla.sitestree.com/layout-of-complicated-gui-by-taking-advantage-of-nested-containers/
A demo providing multiple buttons to select a playing card-A Panel, using CardLayout control which of four possible subpanels, holding a different card, to display
####################### # CardDemo.java A demo providing multiple buttons to select a playing card. A Panel, using CardLayout control which of four possible subpanels, holding a different card, to display.Uses the following class and images: * CardPanel.java A Panel that displays a playing card. * ImageLabel.java A Canvas for displaying images. * Ace.gif, …
Permanent link to this article: http://bangla.sitestree.com/a-demo-providing-multiple-buttons-to-select-a-playing-card-a-panel-using-cardlayout-control-which-of-four-possible-subpanels-holding-a-different-card-to-display/
BorderLayout divides the window into five regions
# BorderTest.java Five buttons arranged by BorderLayout BorderLayout divides the window into five regions: NORTH, SOUTH, EAST, WEST, and CENTER. /./././././././././././././ import java.applet.Applet; import java.awt.*; /** An example of BorderLayout. * &&&&&&&&&&&&&&&&&&&&&&&&&&& public class BorderTest extends Applet { public void init() { setLayout(new BorderLayout()); add(new Button(“Button 1”), BorderLayout.NORTH); add(new Button(“Button 2”), …
Permanent link to this article: http://bangla.sitestree.com/borderlayout-divides-the-window-into-five-regions/
Checkboxes
Checkboxes.java Inherits from CloseableFrame.java. ****************** import java.awt.*; /./././././././././ public class Checkboxes extends CloseableFrame { public static void main(String[] args) { new Checkboxes(); } public Checkboxes() { super(“Checkboxes”); setFont(new Font(“SansSerif”, Font.BOLD, 18)); setLayout(new GridLayout(0, 2)); Checkbox box; for(int i=0; i<12; i++) { box = new Checkbox(“Checkbox …
Permanent link to this article: http://bangla.sitestree.com/checkboxes/
Places a Panel holding 100 buttons in a ScrollPane
import java.applet.Applet; import java.awt.*; /** Places a Panel holding 100 buttons in a ScrollPane that is * too small to hold it. * */ public class ScrollPaneTest extends Applet { public void init() { setLayout(new BorderLayout()); ScrollPane pane = new ScrollPane(); Panel bigPanel = new Panel(); bigPanel.setLayout(new GridLayout(10, 10)); …
Permanent link to this article: http://bangla.sitestree.com/places-a-panel-holding-100-buttons-in-a-scrollpane/
Five buttons arranged by FlowLayout
FlowTest.java ************* FlowTest.java Five buttons arranged by FlowLayout By default, FlowLayout arranges components in rows, left to right, and centered. /././././././././././././ import java.applet.Applet; import java.awt.*; /** FlowLayout puts components in rows. * ************************************ public class FlowTest extends Applet { public void init() { for(int i=1; i<6; i++) { add(new Button(“Button ” + …
Permanent link to this article: http://bangla.sitestree.com/five-buttons-arranged-by-flowlayout/
A Frame that lets you draw circles with mouse clicks
SavedFrame.java **************** A Frame that lets you draw circles with mouse clicks //************** import java.awt.*; import java.awt.event.*; import java.io.*; /** A Frame that lets you draw circles with mouse clicks * and then save the Frame and all circles to disk. * public class SavedFrame extends CloseableFrame implements ActionListener { /** If …
Permanent link to this article: http://bangla.sitestree.com/a-frame-that-lets-you-draw-circles-with-mouse-clicks/
FrameExample1.java and 2
****************** # FrameExample1.java ****************** import java.awt.*; /** */ public class FrameExample1 { public static void main(String[] args) { Frame f = new Frame(“Frame Example 1”); f.setSize(400, 300); f.setVisible(true); } } ********************* # FrameExample2.java ********************* import java.awt.*; /** */ public class FrameExample2 extends Frame { public static void main(String[] args) …
Permanent link to this article: http://bangla.sitestree.com/frameexample1-java-and-2/
ThreadedRSAKey.java Illustrates converting a method in an existing class from a single-threaded method to a multi-threaded method.
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 …
Permanent link to this article: http://bangla.sitestree.com/threadedrsakey-java-illustrates-converting-a-method-in-an-existing-class-from-a-single-threaded-method-to-a-multi-threaded-method/
Eight ungrouped buttons in an Applet using FlowLayout
mport java.applet.Applet; import java.awt. *; ************************** /** Eight ungrouped buttons in an Applet using FlowLayout. * */ public class ButtonTest1 extends Applet { public void init() { String[] labelPrefixes = { “Start”, “Stop”, “Pause”, “Resume” }; for (int i=0; i<4; i++) { add(new Button(labelPrefixes[i] + ” Thread1″)); } for (int i=0; i<4; i++) { add(new …
Permanent link to this article: http://bangla.sitestree.com/eight-ungrouped-buttons-in-an-applet-using-flowlayout/
A Circle component built using a Canvas
import java.awt.*; /** A Circle component built using a Canvas. * */ public class Circle extends Canvas { private int width, height; public Circle(Color foreground, int radius) { setForeground(foreground); width = 2*radius; height = 2*radius; setSize(width, height); } public void paint(Graphics g) { …
Permanent link to this article: http://bangla.sitestree.com/a-circle-component-built-using-a-canvas/
Simplifies the setting of native look and feel
WindowUtilities.java Simplifies the setting of native look and feel. #################### 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 …
Permanent link to this article: http://bangla.sitestree.com/simplifies-the-setting-of-native-look-and-feel/
Using the this reference in class Ship3
/./././././././././. // Give Ship3 a constructor to let the instance variables // be specified when the object is created. /./././././././././ class Ship3 { public double x, y, speed, direction; public String name; public Ship3(double x, double y, double speed, double direction, String name) { this.x = x; // “this” differentiates …
Permanent link to this article: http://bangla.sitestree.com/using-the-this-reference-in-class-ship3/
DashedStrokeExample.java Draws a circle with a dashed line segment (border). Inherits from FontExample.java.
>>>>>>>>>>>>>>>>> import java.awt.*; /** An example of creating a custom dashed line for drawing. * ********************* public class DashedStrokeExample extends FontExample { public void paintComponent(Graphics g) { clear(g); Graphics2D g2d = (Graphics2D)g; drawGradientCircle(g2d); drawBigString(g2d); drawDashedCircleOutline(g2d); } protected void drawDashedCircleOutline(Graphics2D g2d) { g2d.setPaint(Color.blue); // 30-pixel line, …
Permanent link to this article: http://bangla.sitestree.com/dashedstrokeexample-java-draws-a-circle-with-a-dashed-line-segment-border-inherits-from-fontexample-java/
draws a circle wherever mouse was pressed
CircleListener.java A subclass of MouseAdapter that draws a circle wherever mouse was pressed. Illustrates first approach to event-handling with listeners: attaching a separate listener *********** import java.applet.Applet; import java.awt.*; import java.awt.event.*; /** The listener used by CircleDrawer1. Note call * to getSource to obtain reference to the applet. * *************** public class CircleListener extends …
Permanent link to this article: http://bangla.sitestree.com/draws-a-circle-wherever-mouse-was-pressed/
Six buttons arranged in a 2 row x 3 column grid by GridLayout
/././././././ GridTest.java Six buttons arranged in a 2 row x 3 column grid by GridLayout.GridLayout divides the window into equal-sized rectangles based upon the number of rows and columns specified. ****************** import java.applet.Applet; import java.awt.*; /** An example of GridLayout. * /./././././. public class GridTest extends Applet { public void init() { setLayout(new …
Permanent link to this article: http://bangla.sitestree.com/six-buttons-arranged-in-a-2-row-x-3-column-grid-by-gridlayout/
ListEvent2.java
# ListEvents.java Uses the following classes: * CloseableFrame.java * SelectionReporter.java * ActionReporter.java /././././././././././././ import java.awt.event.*; /././././././ public class ListEvents2 extends ListEvents { public static void main(String[] args) { new ListEvents2(); } /** Extends ListEvents with the twist that * typing any of the letters of “JAVA” or …
Permanent link to this article: http://bangla.sitestree.com/listevent2-java/
A Frame that can actually quit
import java.awt.*; import java.awt.event.*; /** A Frame that you can actually quit. Used as the starting * point for most Java 1.1 graphical applications. * public class CloseableFrame extends Frame { public CloseableFrame(String title) { super(title); enableEvents(AWTEvent.WINDOW_EVENT_MASK); } /** Since we are doing something permanent, we need * to …
Permanent link to this article: http://bangla.sitestree.com/a-frame-that-can-actually-quit/
creating a simple Swing application using a JFrame
JFrameExample.java Demonstrates creating a simple Swing application using a JFrame. As with a JApplet, components must be added to the content pane, instead of the window directly.import java.awt.*; import javax.swing.*; /** Tiny example showing the main difference in using * JFrame instead of Frame: using the content pane * and getting the Java (Metal) look …
Permanent link to this article: http://bangla.sitestree.com/creating-a-simple-swing-application-using-a-jframe/
Explicit placement of five buttons with the layout manager turned off
NullTest.java Explicit placement of five buttons with the layout manager turned off (set to null) ########################## import java.applet.Applet; import java.awt.*; /** Layout managers are intended to help you, but there * is no law saying you have to use them. * Set the layout to null to turn them off. * ******************* public class NullTest …
Permanent link to this article: http://bangla.sitestree.com/explicit-placement-of-five-buttons-with-the-layout-manager-turned-off/
JTree Examples
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(); …
Permanent link to this article: http://bangla.sitestree.com/jtree-examples/
Printing in Java 2
* 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 * …
Permanent link to this article: http://bangla.sitestree.com/printing-in-java-2/
BetterCircleTest.java
********************** BetterCircleTest.java ********************** import java.awt.*; import java.applet.Applet; /** Position circles down the diagonal so that their borders * just touch. Illustrates that Java 1.1 lightweight * components can be partially transparent. * */ public class BetterCircleTest extends Applet { public void init() { setBackground(Color.lightGray); setLayout(null); BetterCircle circle; int radius …
Permanent link to this article: http://bangla.sitestree.com/bettercircletest-java/
RMI Example – Numerical Integration, a more realistic RMI example that sends an evaluatable object (function) from a client to a server for numerical integration.
Integral.java Performs actual numerical integration of the function (evaluatable object). /** A class to calculate summations and numeric integrals. The * integral is calculated according to the midpoint rule. * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * . * © 2001 Marty Hall and Larry Brown; * …
Permanent link to this article: http://bangla.sitestree.com/rmi-example-numerical-integration-a-more-realistic-rmi-example-that-sends-an-evaluatable-object-function-from-a-client-to-a-server-for-numerical-integration/
A JPanel that displays six radio buttons with labels.
A JPanel that displays six radio buttons with labels. import java.awt.*; import javax.swing.*; /** A JPanel that displays six JRadioButtons. * *. */ public class SixChoicePanel extends JPanel { public SixChoicePanel(String title, String[] buttonLabels) { super(new GridLayout(3, 2)); setBackground(Color.lightGray); setBorder(BorderFactory.createTitledBorder(title)); ButtonGroup group = new ButtonGroup(); JRadioButton option; int …
Permanent link to this article: http://bangla.sitestree.com/a-jpanel-that-displays-six-radio-buttons-with-labels/
ThreadedEchoServer.java A multithreaded version of EchoServer, where each client request is serviced on a separate thread. Requires the following classes
import java.net.*; import java.io.*; /** A multithreaded variation of EchoServer. * * 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 ThreadedEchoServer extends EchoServer implements Runnable { public static …
Permanent link to this article: http://bangla.sitestree.com/threadedechoserver-java-a-multithreaded-version-of-echoserver-where-each-client-request-is-serviced-on-a-separate-thread-requires-the-following-classes/
Implementing a Server : Network Server
NetworkServerTest.java Establishes a network Server that listens for client requests on the port specified (command-line argument). Uses the following classes: /** 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 …
Permanent link to this article: http://bangla.sitestree.com/implementing-a-server-network-server/
UrlTest.java Demonstrates the ease in which the various components of an URL can be determined (host, port, protocol, etc.)
জা import java.net.*; /** Read a URL from the command line, then print * the various components. * * 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 UrlTest { …
Permanent link to this article: http://bangla.sitestree.com/urltest-java-demonstrates-the-ease-in-which-the-various-components-of-an-url-can-be-determined-host-port-protocol-etc/
Counter2Test.java Driver class that creates three threaded objects (Counter2) that count from 0 to 4.
/** 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 { …
Permanent link to this article: http://bangla.sitestree.com/counter2test-java-driver-class-that-creates-three-threaded-objects-counter2-that-count-from-0-to-4/
Template illustrating the first approach for creating a class with thread behavior.
** 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 ThreadClass extends Thread { public void run() { // Thread behavior here. } }
Permanent link to this article: http://bangla.sitestree.com/template-illustrating-the-first-approach-for-creating-a-class-with-thread-behavior/
Creates three radio buttons and illustrates handling
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[] …
Permanent link to this article: http://bangla.sitestree.com/creates-three-radio-buttons-and-illustrates-handling/
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); …
Permanent link to this article: http://bangla.sitestree.com/simple-button-that-the-user-can-select-to-load-the-entered-url/
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 * …
Permanent link to this article: http://bangla.sitestree.com/a-simple-button-that-contains-an-image-and-a-label-for-use-in-a-toolbar/
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 …
Permanent link to this article: http://bangla.sitestree.com/demonstrates-the-use-of-a-jcolorchooser-which-presents-a-dialog-with-three-different-tabbed-panes-to-allow-the-user-to-select-a-color-preference-2/
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(); …
Permanent link to this article: http://bangla.sitestree.com/creates-various-buttons-in-swing/
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; …
Permanent link to this article: http://bangla.sitestree.com/multithreaded-graphics-and-double-buffering/
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 …
Permanent link to this article: http://bangla.sitestree.com/driver-class-that-creates-three-threaded-objects-counter2-that-count-from-0-to-4/
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() { …
Permanent link to this article: http://bangla.sitestree.com/template-illustrating-the-second-approach-for-creating-a-class-with-thread-behavior/
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(); …
Permanent link to this article: http://bangla.sitestree.com/creates-and-starts-three-threaded-objects/
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. …
Permanent link to this article: http://bangla.sitestree.com/dom-example-that-represents-the-basic-structure-of-an-xml-document-as-a-jtree/
Simple example illustrating the use of check boxes
JCheckBoxTest.java Simple example illustrating the use of check boxes. import javax.swing.*; import java.awt.event.*; */ public class JCheckBoxTest extends JPanel implements ItemListener, ActionListener{ JCheckBox checkBox1, checkBox2; public JCheckBoxTest() { checkBox1 = new JCheckBox(“Java Servlets”); checkBox2 = new JCheckBox(“JavaServer Pages”); checkBox1.setContentAreaFilled(false); checkBox2.setContentAreaFilled(false); checkBox1.addItemListener(this); …
Permanent link to this article: http://bangla.sitestree.com/simple-example-illustrating-the-use-of-check-boxes/
Basic tool bar for holding multiple buttons.
BrowserToolBar.java A basic tool bar for holding multiple buttons. import java.awt.*; import javax.swing.*; /** Part of a small example showing basic use of JToolBar. * Creates a small dockable toolbar that is supposed to look * vaguely like one that might come with a Web browser. * Makes use of ToolBarButton, a small extension of …
Permanent link to this article: http://bangla.sitestree.com/basic-tool-bar-for-holding-multiple-buttons/