Scrolling Programmatically : Scrollpane « Swing JFC « Java

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. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Swing JFC » ScrollpaneScreenshots 
Scrolling Programmatically
Scrolling Programmatically
 
import java.awt.BorderLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JViewport;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class ButtonScroll extends JFrame {
  protected JViewport viewport;

  protected JButton upButton;

  protected JButton downButton;

  protected JButton leftButton;

  protected JButton rightButton;

  protected int pgVertical;

  protected int pgHorzontal;

  public ButtonScroll() {
    super("Scrolling Programmatically");
    setSize(400400);
    getContentPane().setLayout(new BorderLayout());

    ImageIcon shuttle = new ImageIcon("largeJava2sLogo.GIF");
    pgVertical = shuttle.getIconHeight() 5;
    pgHorzontal = shuttle.getIconWidth() 5;
    JLabel lbl = new JLabel(shuttle);

    viewport = new JViewport();
    viewport.setView(lbl);
    viewport.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent e) {
        enableButtons(ButtonScroll.this.viewport.getViewPosition());
      }
    });
    getContentPane().add(viewport, BorderLayout.CENTER);

    JPanel pv = new JPanel(new BorderLayout());
    upButton = createButton("up"'u');
    ActionListener lst = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        movePanel(0, -1);
      }
    };
    upButton.addActionListener(lst);
    pv.add(upButton, BorderLayout.NORTH);

    downButton = createButton("down"'d');
    lst = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        movePanel(01);
      }
    };
    downButton.addActionListener(lst);
    pv.add(downButton, BorderLayout.SOUTH);
    getContentPane().add(pv, BorderLayout.EAST);

    JPanel ph = new JPanel(new BorderLayout());
    leftButton = createButton("left"'l');
    lst = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        movePanel(-10);
      }
    };
    leftButton.addActionListener(lst);
    ph.add(leftButton, BorderLayout.WEST);

    rightButton = createButton("right"'r');
    lst = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        movePanel(10);
      }
    };
    rightButton.addActionListener(lst);
    ph.add(rightButton, BorderLayout.EAST);
    getContentPane().add(ph, BorderLayout.SOUTH);

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

    setVisible(true);
    movePanel(00);
  }

  protected JButton createButton(String name, char mnemonics) {
    JButton btn = new JButton(name );
    btn.setToolTipText("Move " + name);
    btn.setBorderPainted(false);
    btn.setMargin(new Insets(0000));
    btn.setContentAreaFilled(false);
    btn.setMnemonic(mnemonics);
    return btn;
  }

  protected void movePanel(int xmove, int ymove) {
    Point pt = viewport.getViewPosition();
    pt.x += pgHorzontal * xmove;
    pt.y += pgVertical * ymove;

    pt.x = Math.max(0, pt.x);
    pt.x = Math.min(getMaxXExtent(), pt.x);
    pt.y = Math.max(0, pt.y);
    pt.y = Math.min(getMaxYExtent(), pt.y);

    viewport.setViewPosition(pt);
    enableButtons(pt);
  }

  protected void enableButtons(Point pt) {
    if (pt.x == 0)
      enableComponent(leftButton, false);
    else
      enableComponent(leftButton, true);

    if (pt.x >= getMaxXExtent())
      enableComponent(rightButton, false);
    else
      enableComponent(rightButton, true);

    if (pt.y == 0)
      enableComponent(upButton, false);
    else
      enableComponent(upButton, true);

    if (pt.y >= getMaxYExtent())
      enableComponent(downButton, false);
    else
      enableComponent(downButton, true);
  }

  protected void enableComponent(JComponent c, boolean b) {
    if (c.isEnabled() != b)
      c.setEnabled(b);
  }

  protected int getMaxXExtent() {
    return viewport.getView().getWidth() - viewport.getWidth();
  }

  protected int getMaxYExtent() {
    return viewport.getView().getHeight() - viewport.getHeight();
  }

  public static void main(String argv[]) {
    new ButtonScroll();
  }
}


           
         
  
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. Customized ScrollPaneCustomized ScrollPane
8. ScrollPane with imageScrollPane with image
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.