Customized ScrollPane : Scrollpane « Swing JFC « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Swing JFC » Scrollpane 




Customized ScrollPane
Customized ScrollPane
 

import java.awt.Dimension;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;

public class CustomScrollPane extends JPanel {
  protected JScrollBar verticalBar = new JScrollBar(JScrollBar.VERTICAL, 0000);

  protected JScrollBar horizontalBar = new JScrollBar(JScrollBar.HORIZONTAL, 0000);

  protected CustomViewport viewport = new CustomViewport();

  protected JComponent innerComponent;

  public CustomScrollPane(JComponent comp) {
    setLayout(null);
    
    add(viewport);
    innerComponent = comp;
    viewport.add(innerComponent);
    
    verticalBar.setUnitIncrement(5);
    add(verticalBar);

    horizontalBar.setUnitIncrement(5);
    add(horizontalBar);

    AdjustmentListener lst = new AdjustmentListener() {
      public void adjustmentValueChanged(AdjustmentEvent e) {
        viewport.doLayout();
      }
    };
    verticalBar.addAdjustmentListener(lst);
    horizontalBar.addAdjustmentListener(lst);
  }

  public void doLayout() {
    Dimension size = getSize();
    Dimension innerComponentSize = innerComponent.getPreferredSize();
    Dimension verticalBarSize = verticalBar.getPreferredSize();
    Dimension horizontalBarSize = horizontalBar.getPreferredSize();

    int width = Math.max(size.width - verticalBarSize.width - 10);
    int height = Math.max(size.height - horizontalBarSize.height - 10);
    viewport.setBounds(00, width, height);
    verticalBar.setBounds(width + 10, verticalBarSize.width, height);
    horizontalBar.setBounds(0, height + 1, width, horizontalBarSize.height);

    int maxWidth = Math.max(innerComponentSize.width - width, 0);
    horizontalBar.setMaximum(maxWidth);
    horizontalBar.setBlockIncrement(maxWidth / 5);
    horizontalBar.setEnabled(maxWidth > 0);

    int maxHeight = Math.max(innerComponentSize.height - height, 0);
    verticalBar.setMaximum(maxHeight);
    verticalBar.setBlockIncrement(maxHeight / 5);
    verticalBar.setEnabled(maxHeight > 0);

    horizontalBar.setVisibleAmount(horizontalBar.getBlockIncrement());
    verticalBar.setVisibleAmount(verticalBar.getBlockIncrement());
  }

  public Dimension getPreferredSize() {
    Dimension innerComponmentSize = innerComponent.getPreferredSize();
    Dimension verticalBarSize = verticalBar.getPreferredSize();
    Dimension d2 = horizontalBar.getPreferredSize();
    Dimension horizontalBarSize = new Dimension(innerComponmentSize.width
        + verticalBarSize.width, innerComponmentSize.height + d2.height);
    return horizontalBarSize;
  }

  class CustomViewport extends JPanel {
    public CustomViewport(){
      setLayout(null);  
    }
    
    public void doLayout() {
      Dimension innerComponentSize = innerComponent.getPreferredSize();
      int x = horizontalBar.getValue();
      int y = verticalBar.getValue();
      innerComponent.setBounds(-x, -y, innerComponentSize.width,
          innerComponentSize.height);
    }
  }
  public static void main(String[] args) {
    JFrame f = new JFrame("JScrollBar Demo");
    f.setSize(300250);

    ImageIcon icon = new ImageIcon("earth.jpg");
    CustomScrollPane myScrollPane = new CustomScrollPane(new JLabel(icon));
    f.getContentPane().add(myScrollPane);

    WindowListener wndCloser = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    };
    f.addWindowListener(wndCloser);
    f.setVisible(true);
    
  }
  
}

           
         
  














Related examples in the same category
1.Creating a JScrollPane Container
2.Create a scrollable list
3.Controlling the scrollbars in a JScrollPaneControlling the scrollbars in a JScrollPane
4.JViewport: Move and View JViewport: Move and View
5.ScrollPane Sample
6.Scrollpane rulerScrollpane ruler
7.ScrollPane with imageScrollPane with image
8.Scrolling ProgrammaticallyScrolling Programmatically
9.A simple JScrollPane for a JList componentA simple JScrollPane for a JList component
10.JScrollPane with row and column headersJScrollPane with row and column headers
11.A simple JScrollPane demonstrationA simple JScrollPane demonstration
12.Watermark JScrollPane
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.