• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Java

TextLayout Java Example

By Dinesh Thakur

A much more powerful mechanism for drawing text is provided  the TextLayout class.This class not only allows you to draw text in a similar way  to as was done be fore but also provides a number of methods that  let youk now what is specific to draw features.

The following tables hows the methods involved in this section:

Method

Description

getFontRenderContext()

Returns information about therendering context.

TextLayout(String s, Font f,

FontRenderContext frc)

Creates anobject fromText Layouta chain, a source andtextrendering context

Font(String s, intstyle, int size)

Createa fontfrom aname,andplot stylesize.

draw(Graphics2D, float x,

float y)

Drawtext onGraphics2Dandindicates thedisplacementto the axisofcoordinates.

[Read more…] about TextLayout Java Example

Set operations Add Java Example

By Dinesh Thakur

In this example we show how the Set operations going to make a Addition of 2 circles. An example show the code below:

import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.*;
public class DrawaddExample extends Applet {
public static void main(String[] args) {
Frame Drawadd = new Frame("Draw add Example");
Drawadd.setSize(350, 250);
Applet DrawaddExample = new DrawaddExample();
Drawadd.add(DrawaddExample);
Drawadd.setVisible(true);
Drawadd.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
      }
    });
  }
  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawString("Draw add Example", 50, 40);
    g.setFont(new Font("Arial",Font.BOLD,10));
    g.drawString("http://ecomputernotes.com", 200, 205);
                Graphics2D G2D = (Graphics2D)g;
                GradientPaint GPaint = new GradientPaint(50.0f, 50.0f, Color.red,200.0f, 50.0f, Color.green);
                G2D.setPaint(GPaint);
                Ellipse2D E2D = new Ellipse2D.Double(50.0, 50.0, 80.0, 80.0);
                Ellipse2D E2D2 = new Ellipse2D.Double(100.0, 50.0, 80.0, 80.0);
                Area area1 = new Area(E2D);
                Area area2 = new Area(E2D2);
                area1.add(area2);
                G2D.fill(area1);
  }
}

Set operations Add Java Example

Set operations Intersection Java Example

By Dinesh Thakur

In this example we show how the Set operations going to make a Intersection of 2 circles . Intersection examples how the code as follows:

import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.*;
public class DrawintersectExample extends Applet {
public static void main(String[] args) {
Frame Drawintersect = new Frame("Draw intersect Example");
Drawintersect.setSize(350, 250);
Applet DrawintersectExample = new DrawintersectExample();
Drawintersect.add(DrawintersectExample);
Drawintersect.setVisible(true);
Drawintersect.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
      }
    });
  }
  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawString("Draw intersect Example", 50, 40);
    g.setFont(new Font("Arial",Font.BOLD,10));
    g.drawString("http://ecomputernotes.com", 200, 205);
                Graphics2D G2D = (Graphics2D)g;
                GradientPaint GPaint = new GradientPaint(50.0f, 50.0f, Color.red,200.0f, 50.0f, Color.green);
                G2D.setPaint(GPaint);
                Ellipse2D E2D = new Ellipse2D.Double(50.0, 50.0, 80.0, 80.0);
                Ellipse2D E2D2 = new Ellipse2D.Double(100.0, 50.0, 80.0, 80.0);
                Area area1 = new Area(E2D);
                Area area2 = new Area(E2D2);
                area1.intersect(area2);
                G2D.fill(area1);
  }
}

Set operations Intersection Java Example

Set operations Subtraction Java Example

By Dinesh Thakur

In this example we show how the Set operations going to make a Subtraction of 2 circles . Subtraction examples how the code as follows:

import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.*;
public class DrawsubtractExample extends Applet {
public static void main(String[] args) {
Frame Drawsubtract = new Frame("Draw subtract Example");
Drawsubtract.setSize(350, 250);
Applet DrawsubtractExample = new DrawsubtractExample();
Drawsubtract.add(DrawsubtractExample);
Drawsubtract.setVisible(true);
Drawsubtract.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
      }
    });
  }
  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawString("Draw subtract Example", 50, 40);
    g.setFont(new Font("Arial",Font.BOLD,10));
    g.drawString("http://ecomputernotes.com", 200, 205);
                Graphics2D G2D = (Graphics2D)g;
                GradientPaint GPaint = new GradientPaint(50.0f, 50.0f, Color.red,200.0f, 50.0f, Color.green);
                G2D.setPaint(GPaint);
                Ellipse2D E2D = new Ellipse2D.Double(50.0, 50.0, 80.0, 80.0);
                Ellipse2D E2D2 = new Ellipse2D.Double(100.0, 50.0, 80.0, 80.0);
                Area area1 = new Area(E2D);
                Area area2 = new Area(E2D2);
                area1.subtract(area2);
                G2D.fill(area1);
  }
}

Set operations Subtraction Java Example

Set operations exclusiveOr Java Example

By Dinesh Thakur

In this example we show how the Set operations going to make a union of 2 circles. Union exclusive (XOR ) examples how the code as follows: [Read more…] about Set operations exclusiveOr Java Example

GeneralPath Java Example

By Dinesh Thakur

Another way is to build figures using GeneralPath class, which but also inherits from Shapeisan interface, but a class. This class allows  define a shape by a sequence oflines that can be established through  functions in the following table:

Method

Description

GeneralPath

(constructor)

Representsa geometric figureconstructed fromstraight linesandquadratic andcubic curves

moveTo

Movethe pen tipto a particularpoint

curveTo

a cubic curvefrom a starting pointtoa final one

lineTo

definesa straight linefrom start pointtoend you

quadTo

representsa quadratic curvefrom a starting pointtoa final one

closePath

Closes pathof figurebya straight linejoiningthecurrent pen positiontothe start of thedrawing.

 

import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.*;
public class DrawGeneralPathExample extends Applet {
public static void main(String[] args) {
Frame DrawGeneralPath = new Frame("Draw GeneralPath Example");
DrawGeneralPath.setSize(350, 250);
Applet DrawGeneralPathExample = new DrawGeneralPathExample();
DrawGeneralPath.add(DrawGeneralPathExample);
DrawGeneralPath.setVisible(true);
DrawGeneralPath.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
      }
    });
  }
  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawString("Draw GeneralPath Example", 50, 40);
    g.setFont(new Font("Arial",Font.BOLD,10));
    g.drawString("http://ecomputernotes.com", 200, 205);
                Graphics2D G2D = (Graphics2D)g;
                G2D.setColor(Color.green);
                GeneralPath GPath = new GeneralPath();
                GPath.moveTo(50.0f,50.0f);
                GPath.lineTo(100.0f,50.0f);
                GPath.curveTo(120.0f,30.0f,120.0f,100.0f,180.0f,125.0f);
                GPath.lineTo(50.0f,150.0f);
                GPath.closePath();
                G2D.fill(GPath);
                G2D.setColor(Color.blue);
                G2D.draw(GPath);
  }
}

GeneralPath Java Example

Point2D Java Example

By Dinesh Thakur

Point2D class does not draw anything; it is the representation of points Java2D. But yes you can draw shapes from points. Following is at able that lists some of the Constructors and methods of Point2D class:

Constructors and Methods

Description

Point2D.Double ()

Creates a Point2D at (0, 0).

Point2D.Double (double X, double Y)

Creates a Point2D at (X, Y).

Point2D.Float ()

Creates a Point2D at (0, 0).

Point2D.Float (float X, float Y)

Creates a Point2D at (X, Y).

getX ()

Returns the horizontal coordinate of the point.

getY ()

Returns the vertical coordinate of the point.

Point2D.Float (float X, float Y)

Sets the location of the Point as (X, Y).

Drawingan Point2D exampleshownthecodeas follows:

import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.*;
public class DrawPoint2DExample extends Applet {
public static void main(String[] args) {
Frame DrawPoint2D = new Frame("Draw Point2D Example");
DrawPoint2D.setSize(350, 250);
Applet DrawPoint2DExample = new DrawPoint2DExample();
DrawPoint2D.add(DrawPoint2DExample);
DrawPoint2D.setVisible(true);
DrawPoint2D.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
      }
    });
  }
  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawString("Draw Point2D Example", 50, 40);
    g.setFont(new Font("Arial",Font.BOLD,10));
    g.drawString("http://ecomputernotes.com", 200, 205);
                Graphics2D G2D = (Graphics2D)g;
                G2D.setStroke(new BasicStroke(3.0f));
                Point2D P2D = new Point2D.Float(23.5f, 48.9f);
                Point2D P2D2 = new Point2D.Float(158.0f, 173.0f);
                Line2D L2D = new Line2D.Float(P2D, P2D2);
                G2D.draw(L2D);
  }
}

Point2D Java Example

CubicCurve2D Java Example

By Dinesh Thakur

CubicCurve2D which also shows the segments joining  the ends with the points control and the set together. An example of use appears in the following code, which also have highlighted the four reference points. The result can be seen in CubicCurve2D Java Example.

Following is a table that lists some of the Constructors and methods of CubicCurve2D class:

Constructors and Methods

Description

CubicCurve2D.Double ()

Creates a CubicCurve2D from (0, 0) to (0, 0), with control point (0,0)

CubicCurve2D.Double (double X1, double Y1, double CX1, double CY1, double X2, double Y2)

Creates a CubicCurve2D from (X1, Y1) to (X2, Y2), with control point (CX1, CY1).

CubicCurve2D.Float ()

Creates a CubicCurve2D from (0, 0) to (0, 0), with control points (0,0).

CubicCurve2D.Float (float X1, float Y1, float CX1, float CY1, float X2, float Y2)

Creates a CubicCurve2D from (X1, Y1) to (X2, Y2), with control point (CX1, CY1).

boolean intersects (double X, double Y, double Width, double Height)

Returns true if the rectangle described by (X, Y, Width, Height) intersects the CubicCurve

void setCurve (double X1, double Y1, double CX1, double CY1, double X2, double Y2)

Sets the ends of the CubicCurve as (X1, Y1) and (X2, Y2).

Drawingan CubicCurve exampleshownthecodeas follows:

import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.*;
public class DrawCubicCurve2DExample extends Applet {
public static void main(String[] args) {
Frame DrawCubicCurve2D = new Frame("Draw CubicCurve2D Example");
DrawCubicCurve2D.setSize(350, 250);
Applet DrawCubicCurve2DExample = new DrawCubicCurve2DExample();
DrawCubicCurve2D.add(DrawCubicCurve2DExample);
DrawCubicCurve2D.setVisible(true);
DrawCubicCurve2D.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
      }
    });
  }
  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawString("Draw CubicCurve2D Example", 50, 40);
    g.setFont(new Font("Arial",Font.BOLD,10));
    g.drawString("http://ecomputernotes.com", 200, 205);
                super.paint(g);
                Graphics2D G2D = (Graphics2D)g;
                G2D.setColor(Color.blue);
                G2D.setStroke(new BasicStroke(3.0f));
                CubicCurve2D CC2D = new CubicCurve2D.Float(40.0f, 60.0f, 60.0f, 120.0f, 140.0f, 130.0f, 150.0f, 210.0f);
                G2D.draw(CC2D);
                G2D.setColor(Color.blue);
                G2D.draw(new Rectangle2D.Float(40.0f, 60.0f, 1.0f, 1.0f));
                G2D.draw(new Rectangle2D.Float(60.0f, 120.0f, 1.0f, 1.0f));
                G2D.draw(new Rectangle2D.Float(140.0f, 130.0f, 1.0f, 1.0f));
                G2D.draw(new Rectangle2D.Float(150.0f, 210.0f, 1.0f, 1.0f));
  }
}

CubicCurve2D Java Example

QuadCurve2D Java Example

By Dinesh Thakur

The QuadCurve2D class lets build a curved segment based on mathematical equations. The curve generated is also called curve quadratic Bezier and based on a very simple idea is to establish two points that define the ends of a curved segment and a third point, called checkpoint that allows “stretching” more or less the curvature of the segment.

Following is at able that lists some of the Constructors and methods of QuadCurve2D class:

Constructors and Methods

Description

QuadCurve2D.Double ()

Creates a QuadCurve2D from (0, 0) to (0, 0), with control point (0,0)

QuadCurve2D.Double (double X1, double Y1, double CX1, double CY1, double X2, double Y2)

Creates a QuadCurve2D from (X1, Y1) to (X2, Y2), with control point (CX1, CY1).

QuadCurve2D.Float ()

Creates a QuadCurve2D from (0, 0) to (0, 0), with control points (0,0).

QuadCurve2D.Float (float X1, float Y1, float CX1, float CY1, float X2, float Y2)

Creates a QuadCurve2D from (X1, Y1) to (X2, Y2), with control point (CX1, CY1).

boolean intersects (double X, double Y, double Width, double Height)

Returns true iff the rectangle described by (X, Y, Width, Height) intersects the QuadCurve

void setCurve (double X1, double Y1, double CX1, double CY1, double X2, double Y2)

Sets the ends of the QuadCurve as (X1, Y1) and (X2, Y2).

Drawingan Curve exampleshownthecodeas follows:

import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.*;
public class DrawQuadCurve2DExample extends Applet {
public static void main(String[] args) {
Frame DrawQuadCurve2D = new Frame("Draw QuadCurve2D Example");
DrawQuadCurve2D.setSize(350, 250);
Applet DrawQuadCurve2DExample = new DrawQuadCurve2DExample();
DrawQuadCurve2D.add(DrawQuadCurve2DExample);
DrawQuadCurve2D.setVisible(true);
DrawQuadCurve2D.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
      }
    });
  }
  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawString("Draw QuadCurve2D Example", 50, 40);
    g.setFont(new Font("Arial",Font.BOLD,10));
    g.drawString("http://ecomputernotes.com", 200, 205);
                super.paint(g);
                Graphics2D G2D = (Graphics2D)g;
                G2D.setColor(Color.green);
                G2D.setStroke(new BasicStroke(3.0f));
                QuadCurve2D QC2D = new QuadCurve2D.Float(40.0f, 70.0f, 40.0f, 170.0f, 190.0f, 220.0f);
                G2D.draw(QC2D);
                G2D.setColor(Color.red);
                G2D.draw(new Rectangle2D.Float(40.0f, 70.0f, 1.0f, 1.0f));
                G2D.draw(new Rectangle2D.Float(40.0f, 170.0f, 1.0f, 1.0f));
                G2D.draw(new Rectangle2D.Float(190.0f, 220.0f, 1.0f, 1.0f));
  }
}

QuadCurve2D Java Example

Arc2D Java Example

By Dinesh Thakur

The class allows Arc2D draw an arc of an ellipse or a circle. For this, first the rectangle that contains the ellipse whose arc you want to draw is defined ; then assuming that the center of the rectangle sets the point (0,0) of a Cartesian coordinate axis , the angles start and end are specified the arc in degrees . Finally , indicate whether you want to close the bow joining ends or not. This closure can be of three types :

• Arc2D.OPEN : the arc is open .
• Arc2D.CHORD : the ends of the arch are connected by a line segment .
• Arc2D.PIE : Each end of the arc joined by a segment with point ( 0 , 0 ) of the coordinate axis .
The following code draws a rectangle reference . Then, and within a rectangle equal to the reference , an arc that starts in the draw angle 0 ° and ends at 135 . Finally the arc closes like a piece of a pie chart ( Arc2D.PIE ) .

Following isatablethat listssome of the Constructors and methods of Ellipse2Dclass:

Constructors and Methods

Description

Arc2D.Double ()

Creates an OPEN Arc2D with a size of (0, 0) at a location of (0, 0), and angle (0, 0). 

Arc2D.Double (int Type)

Creates an Arc2D of the specified Type with a size of (0, 0) at a location of (0, 0), and angle (0, 0). 

Arc2D.Double (double X, double Y, double Width, double Height, double Theta, double Delta, int Type)

Creates an Arc2D of the specified Type with a size of (Width, Height) at location (X, Y), and angle (Theta, Delta). 

Arc2D.Float ()

Creates an OPEN Arc2D with a size of (0, 0) at a location of (0, 0), and angle (0, 0). 

Arc2D.Float (int Type)

Creates an Arc2D of the specified Type with a size of (0, 0) at a location of (0, 0), and angle (0, 0)

Arc2D.Float (float X, float Y, float Width, float Height, float Theta, float Delta, int Type)

Creates an Arc2D of the specified Type with a size of (Width, Height) at location (X, Y), and angle (Theta, Delta). 

contains (double X, double Y)

Returns true if the point (X, Y) is within the Arc. 

contains (double X, double Y, double Width, double Height)

Returns true if the Arc (X, Y, Width, Height) is entirely within the Arc.

intersects (double X, double Y, double Width, double Height)

Returns true if the Arc (X, Y, Width, Height) intersects the Arc.

setArc (double X, double Y, double Width, double Height, double Theta, double Delta, int Type)

Sets the location, bounding size, arc size, and type of the arc. 

Drawinganarcexampleshownthecodeas follows:

import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.*;
public class DrawArc2DExample extends Applet {
public static void main(String[] args) {
Frame DrawArc2D = new Frame("Draw Arc2D Example");
DrawArc2D.setSize(350, 250);
Applet DrawArc2DExample = new DrawArc2DExample();
DrawArc2D.add(DrawArc2DExample);
DrawArc2D.setVisible(true);
DrawArc2D.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
      }
    });
  }
  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawString("Draw Arc2D Example", 50, 40);
    g.setFont(new Font("Arial",Font.BOLD,10));
    g.drawString("http://ecomputernotes.com", 200, 205);
                super.paint(g);
                Graphics2D G2D = (Graphics2D)g;
                G2D.setColor(Color.blue);
                G2D.setStroke(new BasicStroke(1.0f));
                Rectangle2D Rect2D = new Rectangle2D.Float(100.0f, 75.0f, 50.0f, 100.0f);
                G2D.draw(Rect2D);
                G2D.setColor(Color.green);
                G2D.setStroke(new BasicStroke(3.0f));
                Arc2D Ar2D = new Arc2D.Float(100.0f, 75.0f, 50.0f, 100.0f, 0.0f, 135.0f, Arc2D.PIE);
               G2D.draw(Ar2D);
  }
}

Arc2D Java Example

Ellipse2D Java Example

By Dinesh Thakur

The ellipses or ovals are drawn by Ellipse2D class.This requires you defined to be a rectangle enclosing the boundaries between the ellipse.There the concept of a circle, so that it must be obtained by an ellipse enclosed in a square.

In Ellipse2D Java Example we are using package java.awt.geom.*. Following is a table that lists some of the Constructors and methods of Ellipse2D class:

 

Constructors and Methods

Description

Ellipse2D.Double ()

Creates a Ellipse2D with a size of (0, 0) at a location of (0, 0).

Ellipse2D.Double (double X, double Y, double W, double H)

Creates a Ellipse2D with a size of (W, H) at location (X, Y).

Ellipse2D.Float ()

Creates a Ellipse2D with a size of (0, 0) at a location of (0, 0).

Ellipse2D.Float (float X, float Y, float W, float H)

Creates a Ellipse2D with a size of (W, H) at location (X, Y).

contains (double X, double Y)

Returns true if the point (X, Y) is within the Ellipse.

contains (double X, double Y, double W, double H)

Returns true if the Ellipse (X, Y, W, H) is entirely within the Ellipse.

intersects (double X, double Y, double W, double H)

Returns true if the Ellipse (X, Y, W, H) intersects the Ellipse.

setFrame (double X, double Y, double W, double H)

Sets the location and size of the Ellipse.

Drawinganellipseexampleshownthecodeas follows:

import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.*;
public class Draw2DEllipseExample extends Applet {
public static void main(String[] args) {
Frame Draw2DEllipse = new Frame("Draw 2DEllipse Example");
Draw2DEllipse.setSize(350, 250);
Applet Draw2DEllipseExample = new Draw2DEllipseExample();
Draw2DEllipse.add(Draw2DEllipseExample);
Draw2DEllipse.setVisible(true);
Draw2DEllipse.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
      }
    });
  }
  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawString("Draw Ellipse2D Example", 50, 40);
    g.setFont(new Font("Arial",Font.BOLD,10));
    g.drawString("http://ecomputernotes.com", 200, 205);
                super.paint(g);
                Graphics2D G2D = (Graphics2D)g;
                G2D.setColor(Color.orange);
                G2D.setStroke(new BasicStroke(3.0f));
                Ellipse2D E2D = new Ellipse2D.Float(100.0f,75.0f,50.0f,100.0f);
                G2D.draw(E2D);
  }
}

Ellipse2D Java Example

Line2D Java Example

By Dinesh Thakur

Draw a straight line is one of the easiest things to do with Java2D. This is achieved through the Line2D class, whose constructor accepts four parameters, namely the coordinates of the start and end respectively.

In Line2D Java Example we are using package java.awt.geom.*. This class provides a line segment in (x, y) coordinate space. Following is at able that lists some of the Constructors and methods of Line2D class:

 

Constructors and Methods

Description

Line2D.Double ()

Creates a Line2D from (0, 0) to (0, 0).

Line2D.Double (double X1, double Y1, double X2, double Y2)

Creates a Line2D from (X1, Y1) to (X2, Y2).

Line2D.Float ()

Creates a Line2D from (0, 0) to (0, 0).

Line2D.Float (float X1, float Y1, float X2, float Y2)

Creates a Line2D from (X1, Y1) to (X2, Y2).

intersects (double X, double Y, double W, double H)

Returns true if the rectangle described by (X, Y, Width, Height) intersects the Line.

intersectsLine (double X1, double Y1, double X2, double Y2)

Returns true if the line from (X1, Y1) to (X2, Y2) intersects the Line.

intersectsLine (Line2D L)

Returns true if the line L intersects the Line.

setLine (double X1, double Y1, double X2, double Y2)

Sets the ends of the line as (X1, Y1) and (X2, Y2).

 

import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.*;
public class Draw2DLineExample extends Applet {
public static void main(String[] args) {
Frame Draw2DLine = new Frame("Draw Line2D Example");
Draw2DLine.setSize(350, 250);
Applet Draw2DLineExample = new Draw2DLineExample();
Draw2DLine.add(Draw2DLineExample);
Draw2DLine.setVisible(true);
Draw2DLine.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
      }
    });
  }
  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawString("Draw 2D Line Example", 50, 40);
    g.setFont(new Font("Arial",Font.BOLD,10));
    g.drawString("http://ecomputernotes.com", 200, 205);
                super.paint(g);
                Graphics2D G2D = (Graphics2D)g;
                G2D.setColor(Color.pink);
                G2D.setStroke(new BasicStroke(3.0f));
                Line2D L2D = new Line2D.Float(50.0f, 50.0f, 200.0f, 200.0f);
                G2D.draw(L2D);
  }
}

Line2D Java Example

Rectangle2D Java Example

By Dinesh Thakur

The class that is used to draw rectangles and squares is the Rectangle2D. The constructor specifies the first two parameters of the corner position upper left relative to the coordinate system of the window.

These four parameters can be specified using float or double values, using for this builders Rectangle2D. Float() and Rectangle2D. Double() respectively.This possibility to construct geometric figures using coordinates in float or double is a constant that is repeated in all the shapes. A rectangle can use the fill method of the java.awt.Graphics2D class. Following is at able that lists some of the Constructors and methods of Rectangle2D class:

Constructors and Methods

Description

Rectangle2D.Double ()

Creates a Rectangle2D with a size of (0, 0) at a location of (0, 0).

Rectangle2D.Double (double X, double Y, double W, double H)

Creates a Rectangle2D with a size of (W, H) at location (X, Y)

Rectangle2D.Float ()

Creates a Rectangle2D with a size of (0, 0) at a location of (0, 0).

Rectangle2D.Float (float X, float Y, float W, float H)

Creates a Rectangle2D with a size of (W, H) at location (X, Y)

contains (double X, double Y)

Returns true if the point (X, Y) is within the rectangle.

contains (double X, double Y, double W, double H)

Returns true if the rectangle (X, Y, W, H) is entirely within the rectangle.

intersects (double X, double Y, double W, double H)

Returns true if the rectangle (X, Y, W, H) intersects the rectangle.

intersectsLine (double X1, double Y1, double X2, double Y2)

setRect (double X, double Y, double W, double H)

Returns true if the line from (X1, Y1) to (X2, Y2) intersects the rectangle.

Sets the location and size of the rectangle.

 

import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.*;
public class DrawRectangle2DExample extends Applet {
public static void main(String[] args) {
Frame Draw2DRect = new Frame("Draw 2D Rectangles Example");
Draw2DRect.setSize(350, 250);
Applet DrawRectangle2DExample = new DrawRectangle2DExample();
Draw2DRect.add(DrawRectangle2DExample);
Draw2DRect.setVisible(true);
Draw2DRect.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
      }
    });
  }
  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawString("Draw 2D Rectangles Example", 50, 40);
    g.setFont(new Font("Arial",Font.BOLD,10));
    g.drawString("http://ecomputernotes.com", 200, 205);
                super.paint(g);
                Graphics2D G2D = (Graphics2D)g;
                G2D.setColor(Color.blue);
                G2D.setStroke(new BasicStroke(3.0f));
                Rectangle2D Rect2D = new Rectangle2D.Float(100.0f, 75.0f, 50.0f, 100.0f);
                G2D.draw(Rect2D);
  }
}
 

Rectangle2D Java Example

Java Vector Example

By Dinesh Thakur

The Vector object store objects of any kind are extremely  although flexible operations on objects of this class are slower than  operations on common vectors (arrays). The capacity of the vector is the maximum amount of  elements that it can hold at a given instant. Its size corresponds to  number of elements actually stored. Having your whole capacity occupied  (size = capacity), adding a new element causes the automatic increase  capacity vector. If an increment is specified via its constructor, this increase occurs with this granularity, otherwise the capacity is doubled.

The main constructors and methods of java.util.Vector class are:

Method

Description

Vector ()

Constructs an initially empty Vector object.

Vector (int)

Constructs a Vector with the ability indicated.

Vector (int, int)

Constructs a Vector with the ability and capacity increase indicated.

addElement (Object)

Adds the object to the vector.

capacity ()

Returns the current capacity of the vector.

elementAt (int)

Returns the object contained in the given position.

insertElementAt (Object, int)

Inserts the given object at the position indicated.

removeAllElements ()

Removes all elements of the vector.

removeElementAt (Int)

Removes the element from the indicated position.

setElementAt (Object, int)

Replaces the element at the position indicated object provided.

setSize (int)

Specifies the capacity of the vector.

size ()

Returns the current number of elements contained in vector.

trim (int)

reduces the ability of the vector to the number of elements in the vector.

[Read more…] about Java Vector Example

JViewport in Java Swing Example

By Dinesh Thakur

[Read more…] about JViewport in Java Swing Example

JTree Events Example in Java

By Dinesh Thakur

Using a JTree is as simple as saying:

add(new JTree( new Object[] {“One”, “Two”, “Three”}));
This displays a primitive tree. The API is huge tree without But, certainly one of the longest of Swing. It seems that you can do everything with trees, but the most sophisticated tasks may require quite a bit of research and experimentation.

import java.awt.*; 
import javax.swing.*;
import java.awt.event.*;
import javax.swing.tree.*;
public class JavaExampleTreeEventInJApplet extends JApplet
  {
    public void init()
     {
        JTree Tree = new JTree();
        getContentPane().add(new JScrollPane(Tree));
        Tree.addMouseListener(new MouseAdapter()
           {
               public void mousePressed(MouseEvent e1)
                  {
                     String OutStrng = null;
                     JTree Tree=(JTree)e1.getSource();
                     int ClckdRow = Tree.getRowForLocation(e1.getX(), e1.getY());
                     if(ClckdRow != -1)
                    {
                            TreePath TreePth = Tree.getPathForRow(ClckdRow);
                            TreeNode TreeNd = (TreeNode)TreePth.getLastPathComponent();
                            OutStrng = "Node " + TreeNd.toString();
                             if(e1.getClickCount() == 1)
                               {
                                   OutStrng += "Clicked Single.";
                               }
                          else
                             {
                                         OutStrng += "Clicked Double.";
                                     }
                                         showStatus(OutStrng);
                         }
                   }
           });
        }
 }
/*<APPLET CODE =JavaExampleTreeEventInJApplet.class WIDTH = 35  HEIGHT = 280 ></APPLET>*/

JTree Events Example in Java

Java JTree Cell Editor Example

By Dinesh Thakur

[Read more…] about Java JTree Cell Editor Example

Tree Data Structure in Java Example

By Dinesh Thakur

[Read more…] about Tree Data Structure in Java Example

JTree Java Swing Example

By Dinesh Thakur

JTree: One of the most interesting new classes that Swing offers is the JTree class. this class implements a tree-like structure that can be used to display data hierarchical. TreeNode interface defines the methods that must be implemented nodes a JTree object. The class provides a DefaulMutableTreeNode default implementation of the TreeNode interface. The trees are created constructing the TreeNode interface objects and then add them all together (through of ()) method add. When all TreeNode objects have been assembled, the object Resulting TreeNode to JTree constructor is passed.

 

The default presentation of a JTree object uses a folder icon with the identify the tree nodes having lower nodes and a file icon to identify tree branches. The setCellRenderer () method of the class JTree is used to identify an alternative delivery tree.

import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
public class JavaExampleTreeInJApplet extends JApplet
{
    public void init()
    {
        JTree Tree = new JTree();
        getContentPane().add(new JScrollPane(Tree));
    }
}
/*<APPLET CODE =JavaExampleTreeInJApplet.class WIDTH=350 HEIGHT=280> </APPLET>*/   

JTree Java Swing Example

setToolTipText() in Java Swing Example

By Dinesh Thakur

JToolTip: Consists of a help tab that arises after one second on the position pointed to by the cursor. Normally it is not necessary to use JToolTip class directly, can be set to any component window by: e.setToolTipText (“This is the label”).

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class JavaExamleTooltipInJApplet extends JApplet
   {
     JButton BtnPrs = new JButton("Press Here");
     JTextField Txt = new JTextField(20);
     public void init()
        {
           Container Cntnr = getContentPane();
           Cntnr.setLayout(new FlowLayout());
           BtnPrs.setToolTipText("That is a Button.");
           Cntnr.add(BtnPrs);
           Cntnr.add(Txt);
           BtnPrs.addActionListener(new ActionListener()
              {
                 public void actionPerformed(ActionEvent e)
                  {
                     Txt.setText("Welcome To Java");
                  }
              });
         }
     }
/*<APPLET CODE=JavaExamleTooltipInJApplet.class WIDTH=300 HEIGHT=200 ></APPLET>*/

JToolBar in Java Swing Example

By Dinesh Thakur

JToolBar: A container that allows you to group other components, usually buttons with icons in a row or column. Bars tools are unique in that the user can place them in different configurations on the main frame.

In Program, the toolbox is created using the class JButtons by passing the icons as the parameters of the constructors. Moreover, the icons can be set after creating the buttons also. Another option that can be used in Jbutton is that of setting the icon roll-over. Icon roll-over ensures that the icon changes when the mouse rolls over the tool bar. Table gives constructors and methods of the JToolBar class.

                          Constructors and methods of the JToolBar class.

 

Constructors and Methods

Description

JtoolBar()

Constructs a new toolbar.

JtoolBar(int orientation)

Constructs a new toolbar with orientation specified by the parameter.

JtoolBar(String str)

Constructs a new toolbar with title specified by the parameter.

JToolBar(String str, int orientation)

Constructs a new toolbar with the title and orientation specified by the parameters.

JButton add (Action a)

Adds a JButton object that initializes the specified action.

void addSeparator()

Adds or appends a separator of default dimension to the end of the toolbar.

void addSeparator(dimension dim)

Adds or appends a separator of specified dimension (that is, the dimension specified by the parameter passed to this function) to the end of this toolbar.

Component getComponentAtlndex (int index)

Returns the component at the index specified by the parameter.

Component getComponentAtindex (int index)

Returns the component at the index specified by the parameter

int getComponentindex (Component c)

Returns the index of the component specified by the parameter.

Insets getMargin()

Returns the margin between the border of the toolbar and its buttons.

void remove(Component c)

Removes the component specified in the parameter from the toolbar.

void setMargin(lnsets insets)

Sets the margin specified by the parameter, between the tool bar ‘s border and its buttons.

 

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class JavaExampleToolBarInJApplet extends JApplet implements ActionListener,ItemListener
{
    JButton BtnOne = new JButton("First Button", new ImageIcon("Koala.jpg"));
    JButton BtnTwo = new JButton("Second Button", new ImageIcon("Koala.jpg"));
    JComboBox CmbBox = new JComboBox();
    public void init()
    {
        Container Cntnr = getContentPane();
        JToolBar TlBr = new JToolBar();
        BtnOne.addActionListener(this);
        BtnTwo.addActionListener(this);
        CmbBox.addItem("Item One");
        CmbBox.addItem("Item Two");
        CmbBox.addItem("Item Three");
        CmbBox.addItem("Item Four");
        CmbBox.addItemListener(this);
        TlBr.add(BtnOne);
        TlBr.addSeparator();
        TlBr.add(BtnTwo);
        TlBr.addSeparator();
        TlBr.add(CmbBox);
        //jcombobox.setMaximumSize(jcombobox.getPreferredSize());
        Cntnr.add(TlBr, BorderLayout.NORTH);
    }
        public void actionPerformed(ActionEvent e1)
        {
          if(e1.getSource() == BtnOne)
            {
              showStatus("You Pressed Button One");
            }
                 else if (e1.getSource() == BtnTwo)
                 {
                        showStatus("You Pressed Button Two");
                     }
         }
                  public void itemStateChanged(ItemEvent e2)
                   {
                      String OutStrng = "";
                      if(e2.getStateChange() == ItemEvent.SELECTED)
                      OutStrng += "Selected: " + (String)e2.getItem();
                      else
                      OutStrng += "De Selected: " + (String)e2.getItem();
                      showStatus(OutStrng);
                   }
 }
/*<APPLET CODE =JavaExampleToolBarInJApplet.class WIDTH=500 HEIGHT=280> </APPLET>*/

JToolBar in Java Swing Example

Java JToggleButton Group Example

By Dinesh Thakur

The ButtonGroup class allows to manage a set of buttons ensuring that only one button in the group will selected. To use the ButtonGroup class, simply instantiate an object and add buttons (objects that inherit from the class AbstractButton) through the add () method. It is preferable to use objects JToggleButton class or one of its subclasses because they are able to manage their states.

import java.awt.*; 
import javax.swing.*;
import java.awt.event.*;
public class JavaExampleToggleGroupInJApplet extends JApplet
 {
    public JavaExampleToggleGroupInJApplet()
      {
        Container Cntnr = getContentPane();
        ButtonGroup BtnGrp = new ButtonGroup();
        JToggleButton[] Btns = new JToggleButton[]
          {
             new JToggleButton(new ImageIcon("Koala.jpg")),
             new JToggleButton(new ImageIcon("Koala.jpg")),
             new JToggleButton(new ImageIcon("Koala.jpg")),
             new JToggleButton(new ImageIcon("Koala.jpg")),
             new JToggleButton(new ImageIcon("Koala.jpg"))
           };
             Cntnr.setLayout(new FlowLayout());
             for(int l=0; l < Btns.length; ++l)
               {
                   BtnGrp.add(Btns[l]);
                   Cntnr.add(Btns[l]);
                }
      }
 }
/*<APPLET CODE=JavaExampleToggleGroupInJApplet.class WIDTH=300 HEIGHT=200 > </APPLET>*/

Java JToggleButton Group Example

JToggleButton Java Example

By Dinesh Thakur

Swing provides a variant of push button called toggle button which has two states: pushed and released. When toggle button is pressed for the first time, it remains pressed and it is released only when it is pressed for the second time. This button toggles between pushed and released state. Toggle button is an object of JToggleButton class.

Some of the constructors defined by JToggleButton class are as follows.

JToggleButton ()

JToggleButton(String string)

JToggleButton(String string, boolean state)

where,

string specifies the text

state can have one of the two values: true, if the button is initially selected, otherwise false (default value)

import java.awt.*; 
import javax.swing.*;
import java.awt.event.*;
public class JavaExampleToggleInJApplet extends JApplet
{
    public JavaExampleToggleInJApplet()
    {
        Container Cntnr = getContentPane();
        Icon Icn = new ImageIcon("Koala.jpg");
        JToggleButton TglOne = new JToggleButton(Icn);
        JToggleButton TglTwo = new JToggleButton(Icn, true);
        JToggleButton TglThree = new JToggleButton("Toggle It!");
        JToggleButton TglFour = new JToggleButton("Toggle It!", Icn);
        JToggleButton TglFive = new JToggleButton("Toggle It!",Icn,true);
        Cntnr.setLayout(new FlowLayout());
        Cntnr.add(TglOne);
        Cntnr.add(TglTwo);
        Cntnr.add(TglThree);
        Cntnr.add(TglFour);
        Cntnr.add(TglFive);
    }
}
/*<APPLET CODE=JavaExampleToggleInJApplet.class WIDTH=400 HEIGHT=400> </APPLET>*/

JToggleButton Java Example

Swing – Show image in JTextPane Java Example

By Dinesh Thakur

[Read more…] about Swing – Show image in JTextPane Java Example

JTextPane in Java Example

By Dinesh Thakur

[Read more…] about JTextPane in Java Example

Java Swing JTextField Scrollbar Example

By Dinesh Thakur

Textfield control creates a single-line text area. The text field allows the user to enter and edit the text using cut, copy and paste keys, arrow keys and mouse selections. It is an object of JTextField class which is a subclass of JTextcomponent.

Some of the constructors defined by JTextField class are as follows.

JTextField ()

JTextField(int cols)

JTextField(String string, int cols)

JTextField(String string)

where,

string is the initial string contained in the text field

cols is the width of the text field in terms of columns

[Read more…] about Java Swing JTextField Scrollbar Example

JTextField Alignment in Java Example

By Dinesh Thakur

The JTextField is a text field of a single line. Class inherits from the javax.swing.text.JTextComponent. Its methods include:

• public JTextField (int NumCols): creates a JTextField NumCols occupying columns. If it contains the text exceeds this size will not be lost even if it can not display all simultaneously;
• public JTextField (String text, int NumCols): same as above but also present an initial text;
• public void setFont (Font font): sets the font with which the text is displayed;
• public String getText () returns its content in left;
• public String getSelectedText (): returns the selected text inside;
• public void copy (): Copies the selected text to the system clipboard;
• public void cut (): same as above but also removes the selected text;
• public void setEditable (boolean b) Enables or disables the ability to change the text content.

[Read more…] about JTextField Alignment in Java Example

JTable Image Cell Example in Java

By Dinesh Thakur

The JTable controls how the data is displayed, but the TableModel (table model) controls the data itself. So to create a JTable will typically create a TableModel first. It can be implemented TableModel interface completely, but is usually simpler to inherit the kind of help AbtractTableModel:

DataModel contains an array of data, but can also get data from another source like a database. The constructor adds a TableModelListener that prints the array each time the table is modified. The rest of the methods follow the naming conventions Beans, and are used by JTable when it wants to present the information in DataModel. AbstractTableModel provides methods default for setValue () and isCellEditable () that prevents changes in data, so if you will be editing the data layers, should overwrite these methods.
Once you have a TableModel, you just need to handle the JTable constructor. It should take care of all the details of deployment editing and updating. This example also sets the JTable in JScrollPane. 

import java.awt.*; 
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class JavaExampleTableImageInJApplet extends JApplet
{
    String[] Clmns = {"Burger","Avail","Rate","Date","Picture"};
    Date Dt = (new GregorianCalendar(2000,9,2)).getTime();
    Object[][] Dta = {
                                 {"Chili", new Boolean(false), new Float(4.99), Dt, new ImageIcon("table.jpg")},
                                 {"BBQ", new Boolean(true), new Float(5.99), Dt, new ImageIcon("table.jpg")},
                                 {"Roman", new Boolean(false), new Float(4.99), Dt, new ImageIcon("table.jpg")},
                                 {"Watercress", new Boolean(true), new Float(4.99), Dt, new ImageIcon("table.jpg")},
                                 {"Cheese", new Boolean(false), new Float(4.99), Dt, new ImageIcon("table.jpg")},
                                 {"Beefed", new Boolean(true), new Float(4.99), Dt, new ImageIcon("table.jpg")}
                             };
      JTable Tbl = new JTable(new newModel(Dta,Clmns));
      public void init()
      {
          getContentPane().add(new JScrollPane(Tbl));
      }
}
         class newModel extends DefaultTableModel
        {
           public newModel(Object[][] DATA, Object[] Clmn)
            {
               super(DATA,Clmn);
            }
               public boolean isCellEditable(int row, int Clmn)
                {
                  return false;
                }
                  public Class getColumnClass(int Clmn)
                 {
                    Vector Vctr = (Vector) dataVector.elementAt(0);
                    return Vctr.elementAt(Clmn).getClass();
                 }
        }
/*
<APPLET CODE=JavaExampleTableImageInJApplet.class WIDTH=610 HEIGHT=290> </APPLET>*/

JTable Image Cell Example in Java

DefaultTableModel Java Example

By Dinesh Thakur

[Read more…] about DefaultTableModel Java Example

JTable in Java Swing Example

By Dinesh Thakur

The JTable class is another Swing component that has no equivalent in AWT. JTable provides a very flexible possibility to create and display tables. Lets build tables from arrays or vectors of objects or from objects that implement the TableModel interface.

The JTableModel interface defines methods for objects that specify the contents of a table. The class provides an implementation AbstractTableModel the JTableModel predetermined interface. This class is typically extended to provide a custom implementation of model table.
The JTable class provides the ability to edit tables. the method setCellEditor () allows an object of the interface is TableCellEditor identified as the editor table cells.

                       Constructors and methods of the class JTable.

Constructors and Methods

Description

JTable()

Constructs a default JTable Initialized to default data model, default column and selection model.

JTable(int nrows, int ncols)

Constructs a new JTable with nrows and neols of empty cells using DelaultTableModel.

JTable(Object[][] rowdata,

Object[ ] colnames)

Constructs a JTable to display the data contained in the two-dimensional array, rowdata, having the column names specified in the array, colnames.

JTable(Vector rowvector, Vector colvector)

Constructs a JTable to display the data available in vector of vectors, rowvector, with column names available in colvector.

JTable(TableModel dm)

Constructs a new JTable initialized to specify data model, dm, with the default column model and the default selection model.

jT able(TableModel dm, TableColumnModel cm)

Constructs a JTable initialized to specified data model, dm, also initialized to specified column model, cm, with default selection model.

JTable(TableModel dm, TableColumn

Model cm, ListSelectlonModel lsm)

Constructs a JTable initialized to specified data model, dm. specified column model, cm, and specified list selection model, Ism.

void addColumn(TableColumn coI)

Appends a column to the array of columns.

int getColumnCount()

Returns the number of columns in the column model. (Note: number of columns here may differ from the number of columns In the table model).

int getRowCount()

Returns the number of rows in the table.

int getSelectedColumn()

Returns the index of first selected column or returns -1 if no column is selected.

int getSelectedColumnCount()

Returns the number of columns selected.

int[ ] getSelectedColumns()

Returns the indices of all columns selected.

int getSelectedRow()

Returns the index of first selected row or returns -1 if no row is selected.

int getSelectedRowCount()

Returns the number of rows selected.

int[ ] getSelectedRows()

Returns the index of all the rows selected.

Object getValueAt(int r, int c)

Returns the value at rth row and cth column of JTable.

void removeColumn(Table

Column tc)

Deletes or removes a column from JTable’s array of columns.

import java.awt.*; 
import javax.swing.*;
import javax.swing.table.*;
public class JavaExampleTableInJApplet extends JApplet
 {
    Object[] Dt = new Object[5];
    DefaultTableModel DfltTblModl = new DefaultTableModel();
    JTable Tbl = new JTable(DfltTblModl);
    public void init()
     {
        for(int clmn = 0; clmn < 5; clmn++)
           {
             DfltTblModl.addColumn("Column"+clmn);
           }
             for(int row = 0; row < 10; row++)
             {
                     for(int clmn = 0; clmn < 5; clmn++)
                  {
                          Dt[clmn] = "Cell " + row + "," + clmn;
                      }
                          DfltTblModl.addRow(Dt);
                 }
                    getContentPane().add(new JScrollPane(Tbl));
      }
  }
/*<APPLET CODE=JavaExampleTableInJApplet.class WIDTH=360 HEIGHT=290 ></APPLET>*/

JTable in Java Swing Example

« Previous Page
Next Page »

Primary Sidebar

Java Tutorials

Java Tutorials

  • Java - Home
  • Java - IDE
  • Java - Features
  • Java - History
  • Java - this Keyword
  • Java - Tokens
  • Java - Jump Statements
  • Java - Control Statements
  • Java - Literals
  • Java - Data Types
  • Java - Type Casting
  • Java - Constant
  • Java - Differences
  • Java - Keyword
  • Java - Static Keyword
  • Java - Variable Scope
  • Java - Identifiers
  • Java - Nested For Loop
  • Java - Vector
  • Java - Type Conversion Vs Casting
  • Java - Access Protection
  • Java - Implicit Type Conversion
  • Java - Type Casting
  • Java - Call by Value Vs Reference
  • Java - Collections
  • Java - Garbage Collection
  • Java - Scanner Class
  • Java - this Keyword
  • Java - Final Keyword
  • Java - Access Modifiers
  • Java - Design Patterns in Java

OOPS Concepts

  • Java - OOPS Concepts
  • Java - Characteristics of OOP
  • Java - OOPS Benefits
  • Java - Procedural Vs OOP's
  • Java - Polymorphism
  • Java - Encapsulation
  • Java - Multithreading
  • Java - Serialization

Java Operator & Types

  • Java - Operator
  • Java - Logical Operators
  • Java - Conditional Operator
  • Java - Assignment Operator
  • Java - Shift Operators
  • Java - Bitwise Complement Operator

Java Constructor & Types

  • Java - Constructor
  • Java - Copy Constructor
  • Java - String Constructors
  • Java - Parameterized Constructor

Java Array

  • Java - Array
  • Java - Accessing Array Elements
  • Java - ArrayList
  • Java - Passing Arrays to Methods
  • Java - Wrapper Class
  • Java - Singleton Class
  • Java - Access Specifiers
  • Java - Substring

Java Inheritance & Interfaces

  • Java - Inheritance
  • Java - Multilevel Inheritance
  • Java - Single Inheritance
  • Java - Abstract Class
  • Java - Abstraction
  • Java - Interfaces
  • Java - Extending Interfaces
  • Java - Method Overriding
  • Java - Method Overloading
  • Java - Super Keyword
  • Java - Multiple Inheritance

Exception Handling Tutorials

  • Java - Exception Handling
  • Java - Exception-Handling Advantages
  • Java - Final, Finally and Finalize

Data Structures

  • Java - Data Structures
  • Java - Bubble Sort

Advance Java

  • Java - Applet Life Cycle
  • Java - Applet Explaination
  • Java - Thread Model
  • Java - RMI Architecture
  • Java - Applet
  • Java - Swing Features
  • Java - Choice and list Control
  • Java - JFrame with Multiple JPanels
  • Java - Java Adapter Classes
  • Java - AWT Vs Swing
  • Java - Checkbox
  • Java - Byte Stream Classes
  • Java - Character Stream Classes
  • Java - Change Color of Applet
  • Java - Passing Parameters
  • Java - Html Applet Tag
  • Java - JComboBox
  • Java - CardLayout
  • Java - Keyboard Events
  • Java - Applet Run From CLI
  • Java - Applet Update Method
  • Java - Applet Display Methods
  • Java - Event Handling
  • Java - Scrollbar
  • Java - JFrame ContentPane Layout
  • Java - Class Rectangle
  • Java - Event Handling Model

Java programs

  • Java - Armstrong Number
  • Java - Program Structure
  • Java - Java Programs Types
  • Java - Font Class
  • Java - repaint()
  • Java - Thread Priority
  • Java - 1D Array
  • Java - 3x3 Matrix
  • Java - drawline()
  • Java - Prime Number Program
  • Java - Copy Data
  • Java - Calculate Area of Rectangle
  • Java - Strong Number Program
  • Java - Swap Elements of an Array
  • Java - Parameterized Constructor
  • Java - ActionListener
  • Java - Print Number
  • Java - Find Average Program
  • Java - Simple and Compound Interest
  • Java - Area of Rectangle
  • Java - Default Constructor Program
  • Java - Single Inheritance Program
  • Java - Array of Objects
  • Java - Passing 2D Array
  • Java - Compute the Bill
  • Java - BufferedReader Example
  • Java - Sum of First N Number
  • Java - Check Number
  • Java - Sum of Two 3x3 Matrices
  • Java - Calculate Circumference
  • Java - Perfect Number Program
  • Java - Factorial Program
  • Java - Reverse a String

Other Links

  • Java - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW