Solved

Custom Scroll Component

Posted on 2004-10-02
13
191 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 300 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Checkbox and ListView in Android Layout 4 56
Cipher Configuration on Apache HTTPD 4 53
servlet  URL Rewriting 1 27
map related example 6 36
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now