Solved

Custom Scroll Component

Posted on 2004-10-02
13
179 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

708 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