Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Scrollpane in Java Swing 
 

In this section, you will study how to scroll the drawing on canvas. For this, we have used class Canvas which extends the class JPanel.

 

Scrollpane in Java Swing

                         

Use Canvas with ScrollPane in swing.

In this section, you will study how to scroll the drawing on canvas. For this, we have used class Canvas which extends the class JPanel. The method setSize() resizes the component by calling the method getPreferredSize() of Component class which returns the dimension object showing the component's preferred size. The class Ellipse2D is used to draw a circle.


Following code adds a scrollpane on canvas:

getContentPane().add(new JScrollPane(new Canvas()));

The package javax.swing implements a class JScrollPane which provides the scrollable view of the component. 

Here is the code of ScrollPaneOnCanvas.java

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;

public class ScrollPaneOnCanvas extends JFrame {
   class Canvas extends JPanel {
   public Canvas () {
   setSize(getPreferredSize());
   Canvas.this.setBackground(Color.white);
   }
   public Dimension getPreferredSize() {
   return new Dimension(600, 400);
   }

   public void paintComponent(Graphics g) {
   super.paintComponent(g);
   Graphics2D g2d = (Graphics2D) g;
   Ellipse2D ellipse = new Ellipse2D.Double(0, 0, 100, 100);
   g2d.setColor(Color.cyan);
   g2d.translate(10, 10);
   g2d.draw(ellipse);
   g2d.fill(ellipse);
        }
    }
   public ScrollPaneOnCanvas() {
   super("Swing Application");
   setSize(300, 300);
   getContentPane().add(new JScrollPane(new Canvas()));
    }
   public static void main(String[] args) {
   ScrollPaneOnCanvas canvas = new ScrollPaneOnCanvas();
   canvas.show();
    }

Output will be displayed as:

Download Source Code

 

                         

» View all related tutorials
Related Tags: c com image class list object diff components stack icons user sed entity icon order component ai define container show

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.