Grab and Drag image scroll label : Label « Swing JFC « Java

Grab and Drag image scroll label

Grab and Drag image scroll label

import java.awt.Container;
import java.awt.Cursor;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;
import javax.swing.event.MouseInputAdapter;

public class GrabAndDragDemo extends JFrame {
  public GrabAndDragDemo() {
    super("Grab and drag Demo");
    ImageIcon ii = new ImageIcon("largeJava2sLogo.jpg");
    JScrollPane jsp = new JScrollPane(new GrabAndScrollLabel(ii));
    setSize(300, 250);

    WindowListener l = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {

  public static void main(String[] args) {
    new GrabAndDragDemo();

class GrabAndScrollLabel extends JLabel {
  public GrabAndScrollLabel(ImageIcon i) {

    MouseInputAdapter mia = new MouseInputAdapter() {
      int xDiff, yDiff;

      boolean isDragging;

      Container c;

      public void mouseDragged(MouseEvent e) {
        c = GrabAndScrollLabel.this.getParent();
        if (c instanceof JViewport) {
          JViewport jv = (JViewport) c;
          Point p = jv.getViewPosition();
          int newX = p.x - (e.getX() - xDiff);
          int newY = p.y - (e.getY() - yDiff);

          int maxX = GrabAndScrollLabel.this.getWidth()
              - jv.getWidth();
          int maxY = GrabAndScrollLabel.this.getHeight()
              - jv.getHeight();
          if (newX < 0)
            newX = 0;
          if (newX > maxX)
            newX = maxX;
          if (newY < 0)
            newY = 0;
          if (newY > maxY)
            newY = maxY;

          jv.setViewPosition(new Point(newX, newY));

      public void mousePressed(MouseEvent e) {
        xDiff = e.getX();
        yDiff = e.getY();

      public void mouseReleased(MouseEvent e) {


Related examples in the same category

1.Create a JLabel with an image icon
2.Create JLabel component
3.Create a JLabel component
4.Putting HTML text on Swing componentsPutting HTML text on Swing components
5.Shows how displayed Mnemonic and labelFor properties work togetherShows how displayed Mnemonic and labelFor properties work together
6.A quick application to show a simple JLabelA quick application to show a simple JLabel
7.A JLabel that uses inline HTML to format its textA JLabel that uses inline HTML to format its text
8.Variations on a text and icon labelVariations on a text and icon label
9.A simple demonstration of text alignment in JLabelsA simple demonstration of text alignment in JLabels
10.Label with Image Label with Image
11.Label with HTML SampleLabel with HTML Sample
12.Label Text PositionLabel Text Position
13.LabelFor DemoLabelFor Demo
14.Label with IconLabel with Icon
15.Active Label SampleActive Label Sample
16.Swing Label DemoSwing Label Demo
17.Label alignmentLabel alignment
18.HTML labelHTML label
19.Label background, icon, alignLabel background, icon, align
20.Scrollable LabelScrollable Label
21.Animation label
22.Label with various effectsLabel with various effects
23.Label for Label for
24.LabelFocusSample: setLabelForLabelFocusSample: setLabelFor
25.Adding Drag-and-Drop Support to a JLabel Component
26.Setting the Focus of a JTextField Component Using a JLabel Component
27.Adding an Icon to a JLabel Component
28.The text is horizontally and vertically centered
29.The text is right-justified and vertically centered
30.The text is left-justified and top-aligned
31.The text is right-justified and top-aligned
32.The text is left-justified and bottom-aligned
33.The text is right-justified and bottom-aligned
34.JLabel with more than one row
35.JLabel is for displaying text, images or both. It does not react to input events.
36.JSlider lets the user graphically select a value by sliding a knob within a bounded interval.
37.JLabel with lined border
38.Label with ImageIcon (Icon)
39.Multiline label (HTML)
40.Disable a label
41.Set the Preferred Size
42.Horizontal Alignment: CENTER
43.Vertical Alignment: CENTER
44.Horizontal Alignment: LEFT
45.Vertical Alignment: TOP
46.Horizontal Alignment: RIGHT
47.Vertical Alignment: BOTTOM
48.Associate JLabel component with a JTextField
49.A Label that uses inline HTML to format its text
50.A label with no indication it has been clicked
51.Solve the problem of text alignment in JLabels
52.Use a ScrollBar in both vertical and horizontal direction