Scrollbar Problem

B

bruce

I am trying to get Scrollbars to work. With the following code, a
horizontal bar appears and the slide works. But the data on the page
does not scroll.

setLayout(new BorderLayout());

JScrollBar hbar = new JScrollBar(
JScrollBar.HORIZONTAL, 30, 20, 0, 300);
hbar.setUnitIncrement(2);
hbar.addAdjustmentListener(new MyAdjustmentListener());
add(hbar, BorderLayout.SOUTH);

class MyAdjustmentListener implements AdjustmentListener {
public void adjustmentValueChanged(AdjustmentEvent e) {
repaint();
}

Thanks for the help....

Bruce
 
F

Fred

I am trying to get Scrollbars to work. With the following code, a
horizontal bar appears and the slide works. But the data on the page
does not scroll.

        setLayout(new BorderLayout());

        JScrollBar hbar = new JScrollBar(
                JScrollBar.HORIZONTAL, 30, 20, 0, 300);
        hbar.setUnitIncrement(2);
        hbar.addAdjustmentListener(new MyAdjustmentListener());
        add(hbar, BorderLayout.SOUTH);

        class MyAdjustmentListener implements AdjustmentListener {
               public void adjustmentValueChanged(AdjustmentEvent e) {
            repaint();
        }

Thanks for the help....

You haven't hooked the scrollbar up to do anything.

You probably want to use a JScrollPane instead of a bare JScrollbar.
 
B

bruce

You haven't hooked the scrollbar up to do anything.

You probably want to use a JScrollPane instead of a bare JScrollbar.

Yah, I woke up this morning about 3am with that thought. The layout I
have is a base JFrame with 6 JPanels that contain input data, (text
boxes, text area, dropdown lists, etc.). So, I think I have to connect
to my base JFrame. Right???

I guess I have some other options. Since I'm new to this Java stuff,
I'm not sure which is correct.

1) Overlay my JFrame with a JPanel and then place the 6 JPanels on top
of this new base JPanel.

3) Drop the JFrame and make my base a JPanel, then place the 6 JPanels
on top of this new base JPanel.

3) Make no changes except to connect the JScrollBar to my base JFrame.

Which do you think I should use? Or do you have a better suggestion..

Thanks for the response...

Bruce
 
K

Knute Johnson

Yah, I woke up this morning about 3am with that thought. The layout I
have is a base JFrame with 6 JPanels that contain input data, (text
boxes, text area, dropdown lists, etc.). So, I think I have to connect
to my base JFrame. Right???

I guess I have some other options. Since I'm new to this Java stuff,
I'm not sure which is correct.

1) Overlay my JFrame with a JPanel and then place the 6 JPanels on top
of this new base JPanel.

3) Drop the JFrame and make my base a JPanel, then place the 6 JPanels
on top of this new base JPanel.

3) Make no changes except to connect the JScrollBar to my base JFrame.

Which do you think I should use? Or do you have a better suggestion..

Thanks for the response...

Bruce

Create your GUI in a JPanel or JComponent, add that to a JScrollPane and
then add the JScrollPane to your top level container, the JFrame,
JWindow or JApplet.

There are some issues that will arise with preferred sizes and different
layout managers. In the example I provided below, packing the frame
instead of setting its size will cause the scroll bars not to appear.
In my example if the base JPanel were resized smaller than the
GridLayout can display the JLabels, the layout manager puts ... into the
display. Take out the JScrollPane and resize the frame to see.

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

public class test extends JPanel {
public test() {
super(new GridLayout(6,6));

for (int i=0; i<6; i++)
for (int j=0; j<6; j++) {
JLabel l = new JLabel(Integer.toString(i*j*100));
l.setFont(new Font("Arial",Font.PLAIN,40));
l.setBorder(BorderFactory.createLineBorder(Color.BLUE,5));
add(l);
}
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test t = new test();
JScrollPane sp = new JScrollPane(t);
f.add(sp,BorderLayout.CENTER);
f.setSize(400,300);
f.setVisible(true);
}
});
}
}
 
B

bruce

Yah, I woke up this morning about 3am with that thought. The layout I
have is a base JFrame with 6 JPanels that contain input data, (text
boxes, text area, dropdown lists, etc.). So, I think I have to connect
to my base JFrame. Right???
I guess I have some other options. Since I'm new to this Java stuff,
I'm not sure which is correct.
1) Overlay my JFrame with a JPanel and then place the 6 JPanels on top
of this new base JPanel.
3) Drop the JFrame and make my base a JPanel, then place the 6 JPanels
on top of this new base JPanel.
3) Make no changes except to connect the JScrollBar to my base JFrame.
Which do you think I should use? Or do you have a better suggestion..
Thanks for the response...

Create your GUI in a JPanel or JComponent, add that to a JScrollPane and
then add the JScrollPane to your top level container, the JFrame,
JWindow or JApplet.

There are some issues that will arise with preferred sizes and different
layout managers.  In the example I provided below, packing the frame
instead of setting its size will cause the scroll bars not to appear.
In my example if the base JPanel were resized smaller than the
GridLayout can display the JLabels, the layout manager puts ... into the
display.  Take out the JScrollPane and resize the frame to see.

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

public class test extends JPanel {
     public test() {
         super(new GridLayout(6,6));

         for (int i=0; i<6; i++)
             for (int j=0; j<6; j++) {
                 JLabel l = new JLabel(Integer.toString(i*j*100));
                 l.setFont(new Font("Arial",Font.PLAIN,40));
                 l.setBorder(BorderFactory.createLineBorder(Color.BLUE,5));
                 add(l);
             }
     }

     public static void main(String[] args) {
         EventQueue.invokeLater(new Runnable() {
             public void run() {
                 JFrame f = new JFrame();
                 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                 test t = new test();
                 JScrollPane sp = new JScrollPane(t);
                 f.add(sp,BorderLayout.CENTER);
                 f.setSize(400,300);
                 f.setVisible(true);
             }
         });
     }

}

Okay. I "THINK" I understand what you did and what you recommended.
I've laid out my first JPanel with some input fields, compiled and
ran. Looks like it's exactly what I need. Thanks you very much.

Tusen Takk..

Bruce
 
M

markspace

Okay. I "THINK" I understand what you did and what you recommended.
I've laid out my first JPanel with some input fields, compiled and
ran. Looks like it's exactly what I need. Thanks you very much.


Have you looked at this yet?

<http://download.oracle.com/javase/tutorial/uiswing/components/scrollpane.html>


The first example gives the basic pattern. Create a JSCrollPane with a
target, and then add the JSCrollPane to a JFrame or another component.

Instead of a JTextArea, like the example shows, use Knute's example of a
JPanel.


JPanel panel6 = new JPanel();
for( int i = 0; i < 6; i++ ) {
// make things to add to panel6
panel6.add( thing );
}
JScrollPane sp = new JScrollPane( panel6 );
JFrame topWindow = new JFrame( "Scroll Test" );
topWindow.add( sp );
// set size, and setVisible( true );

This should do the trick, though I haven't tried it yet.
 
B

bruce

Have you looked at this yet?

<http://download.oracle.com/javase/tutorial/uiswing/components/scrollp...>

The first example gives the basic pattern.  Create a JSCrollPane with a
target, and then add the JSCrollPane to a JFrame or another component.

Instead of a JTextArea, like the example shows, use Knute's example of a
JPanel.

   JPanel panel6 = new JPanel();
   for( int i = 0; i < 6; i++ ) {
      // make things to add to panel6
      panel6.add( thing );
   }
   JScrollPane sp = new JScrollPane( panel6 );
   JFrame topWindow = new JFrame( "Scroll Test" );
   topWindow.add( sp );
   // set size, and setVisible( true );

This should do the trick, though I haven't tried it yet.

Yes, I looked at the URL you suggested. It was one of the first I
looked at. But, at the time, I didn't understand enough about
scrolling. But I've been working on this for a couple of days and
understand the process better

Thanks for the suggestion..

Bruce
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top