Solved

Custom Scroll Component

Posted on 2004-10-02
13
209 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle SQL syntax check  without executing 6 108
Selenium docs api java index 3 69
What browser will run Java? 7 127
activeMQ Queue Messages Enqueued and Messages Dequeued 1 39
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

726 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