?
Solved

Custom Scroll Component

Posted on 2004-10-02
13
Medium Priority
?
224 Views
Last Modified: 2011-09-20
I have a JPanel that is always a specific height, but now always a specific width.

I have 2 JButtons with arrow Images on them (one pointing each way)

What is the EASIEST way to use the two buttons to scroll the JPanel?
Please include an example of a component that would do this (stubbing out actionPerformed, etc is fine).
<- jpanel ->

Note: I do not want any other scrollbars to be visible, just the two image buttons, thanks.
0
Comment
Question by:darkpegasus5
  • 7
  • 6
13 Comments
 
LVL 92

Accepted Solution

by:
objects earned 1200 total points
ID: 12209573
you should be able to easily adapt the following to meet your needs:

http://www.objects.com.au/java/examples/swing/NoScrollBars.do
0
 
LVL 1

Author Comment

by:darkpegasus5
ID: 12210478
I wasn't able to access the site
0
 
LVL 92

Expert Comment

by:objects
ID: 12210530
try again now, sorry I've been having some ISP problems.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:darkpegasus5
ID: 12213089
I ran the code and substituted a JPanel for the JTextArea. I added 20 JLabels to the JPanel, and then added the JPanel to the JViewPort. Clicking the buttons does not do anything to the JViewPort. :-(
0
 
LVL 92

Expert Comment

by:objects
ID: 12213843
can you post your code
0
 
LVL 1

Author Comment

by:darkpegasus5
ID: 12213985
I just used the code that was on that page, but even if you try to run this, it doesn't scroll the JTextArea up & down if the lineWrap is set to true.

package au.com.objects.examples;

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

public class NoScrollBarsExample extends JFrame
      implements ActionListener
{
      private JViewport VP = new JViewport();
      
      private JButton Up = new JButton("Up");
      private JButton Down = new JButton("Down");
      private JButton Left = new JButton("Left");
      private JButton Right = new JButton("Right");

      public NoScrollBarsExample()
      {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            Up.addActionListener(this);
            Down.addActionListener(this);
            Left.addActionListener(this);
            Right.addActionListener(this);
            
            VP.setView(new JTextArea());
            
            getContentPane().add(Up, BorderLayout.NORTH);
            getContentPane().add(Down, BorderLayout.SOUTH);
            getContentPane().add(Left, BorderLayout.WEST);
            getContentPane().add(Right, BorderLayout.EAST);
            getContentPane().add(VP, BorderLayout.CENTER);
            
            setSize(300, 300);
            
      }
      
      public void actionPerformed(ActionEvent event)
      {
            Object source = event.getSource();
            Point p = VP.getViewPosition();
            if (source==Up)
            {
                  p.translate(0, 1);
            }
            else if (source==Down)
            {
                  p.translate(0, -1);
            }
            else if (source==Left)
            {
                  p.translate(1, 0);
            }
            else if (source==Right)
            {
                  p.translate(-1, 0);
            }
            VP.setViewPosition(p);
      }
      
      public static void main(String[] args)
      {
            JFrame f = new NoScrollBarsExample();
            f.show();
      }
}


0
 
LVL 92

Expert Comment

by:objects
ID: 12214017
scrolls fine when i run it here.
0
 
LVL 1

Author Comment

by:darkpegasus5
ID: 12214074
you're right, I messed up by trying to type it on my other computer. I had an extra statement that messed up my viewport. thanks! I'm trying to add some extra points for the trouble...
0
 
LVL 92

Expert Comment

by:objects
ID: 12214098
had me confused there for a bit :)
thanks for the points
0
 
LVL 1

Author Comment

by:darkpegasus5
ID: 12214406
Why is it that if I translate by 10 instead of 1, I get such an ugly repaint?
0
 
LVL 92

Expert Comment

by:objects
ID: 12214476
are u scrolling so the component in the viewport does not completely cover the viewport?
0
 
LVL 1

Author Comment

by:darkpegasus5
ID: 12214495
I just took your code and changed the up and down scrolling from a p.translate(0,1) to p.translate(0,10) & I started to get text that didn't look like it was double-buffered anymore.
0
 
LVL 92

Expert Comment

by:objects
ID: 12215163
that code was only intended as an example of how to work with a viewport without scroll panes.
It doesn't include any checking of the viewport position, try checking if the requested view position change is valid.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question