Solved

Custom Scroll Component

Posted on 2004-10-02
13
205 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse shortcuts 9 63
spring jars download 1 35
maven module vs maven project 3 47
Crystal Reports Licensing Questions 4 34
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

861 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