Results 1 to 2 of 2

Thread: JScrollBar

  1. #1
    solomon_13000 is offline Member
    Join Date
    Jun 2009
    Posts
    1
    Rep Power
    0

    Default JScrollBar

    The code below implements the JScrollBar. The problem is when I scroll the verticle bar the information in the label does not change position. How do I resolve this problem?.

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

    public class IMPL_ScrollBar implements AdjustmentListener
    {
    JFrame frame;
    JScrollBar vScrollBar,hScrollBar;
    JPanel p1;
    JLabel lb1;

    public IMPL_ScrollBar()
    {
    frame = new JFrame("This is a scrollbar");
    vScrollBar = new JScrollBar(JScrollBar.VERTICAL,30,40,0,300);
    hScrollBar = new JScrollBar(JScrollBar.HORIZONTAL,30,20,0,300);
    vScrollBar.setBlockIncrement(1);
    vScrollBar.setUnitIncrement(2);

    vScrollBar.addAdjustmentListener(this);
    hScrollBar.addAdjustmentListener(this);

    lb1 = new JLabel("Hello");
    p1 = new JPanel(new FlowLayout());
    p1.add(lb1);
    frame.getContentPane().add(vScrollBar,BorderLayout .EAST);
    frame.getContentPane().add(hScrollBar,BorderLayout .SOUTH);
    frame.getContentPane().add(p1,BorderLayout.CENTER) ;
    frame.setVisible(true);
    }

    public void adjustmentValueChanged(AdjustmentEvent ae)
    {
    lb1.setText("Hello");
    }

    public static void main(String[] args)
    {
    IMPL_ScrollBar sb = new IMPL_ScrollBar();
    }
    }

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    I'm guessing you are new to Swing. First, you should learn about the EventQueue class and the event dispatcher thread (EDT). For example, you should always do the initial setVisible(true) as follows:

    Java Code:
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            frame.setVisible(true);
        }
    });
    Check out the Sun's Java Tutorial home page; it has a link to the Swing Tutorial. You will hate life (or Swing) if you do not understand the EDT and use it correctly.

    As far as scroll bars, they are available for "low-level" coding, where you want complete control over the screen components. For ordinary purposes, I suggest the following:

    First, set the LayoutManager on JFrame to GridLayout, with no parameters. Add one main panel or pane to hold the contents of all the other components. Then set the LayoutManager for the panel or pane.

    The easiest way to implement scrolling is to use JScrollPane. You can use that as the top-level pane. It allows you to control when scroll bars appear. Note that JScrollPane will not actually scroll unless its contents take up more space than the view port.

Similar Threads

  1. problem with jscrollbar in my applet
    By leonard in forum Java Applets
    Replies: 1
    Last Post: 08-03-2007, 11:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •