Using JScrollbar on a JComponent


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Using JScrollbar on a JComponent

  1. #1
    Join Date
    Jul 2005
    Posts
    3

    Question Using JScrollbar on a JComponent

    Hi,

    I have a JComponent (fixed size) that shows graphics. Now I want the user to scroll the content of the component (not scroll the actual componen though)

    I cant use a JScrollPance because i cant hold all the graphics in memory. I just want the user to see and use the scrollbars, capture the events and direct them to my graphics engine.

    I tried JComponent.add() and JComponent.getrootPane().add() but the scrollbars dont want to show...same for the events

    Any ideas how it can be done? Thanks!

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    What scrollbars are you expecting to see ? A JComponent has no hidden scrollbar
    feature.
    If You use a JScrollPane the bar(s) will show if the panel you add to it exceeds the scrollpane size, and you invoke its method for user interface update.
    eschew obfuscation

  3. #3
    Join Date
    Jul 2005
    Posts
    3
    I tried to add normal JScrollBar 's to my jcomponent

    here is what im trying to do:

    class MyClass extends JComponent{
    JScrollBar jsb;

    MyClass()
    {
    jsb = new Scrollbar(JScrollbar.HORIZONTAL, 0,10,0,100);
    this.add(jsb);
    }
    }

    and the MyClass is added to the rootPane of my JFrame (Main Application)

    ...
    MyClass mc = new MyClass();
    this.getRootPane().add(mc);
    ...

    Now after this i qould expect the ScrollBars to show up, but hey dont...
    Makes no difference weather i setsize or setpreferredsize etc.

    ?!?

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    This should solve some problem(s) ?

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    
    public class ScrollFrame extends JFrame implements AdjustmentListener {
      BorderLayout borderLayout1 = new BorderLayout();
      JScrollBar vScroll = new JScrollBar();
      JScrollBar hScroll = new JScrollBar();
      public ScrollFrame() {
        try {
          jbInit();
          validate();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      public static void main(String[] args) {
        ScrollFrame sf = new ScrollFrame();
        sf.setBounds(20,20,600,500);
        sf.setVisible(true);
        sf.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
        });
      }
      private void jbInit() throws Exception {
        this.getContentPane().setLayout(borderLayout1);
        hScroll.setOrientation(JScrollBar.HORIZONTAL);
        this.getContentPane().add(vScroll, BorderLayout.EAST);
        this.getContentPane().add(hScroll, BorderLayout.SOUTH);
    
        vScroll.addAdjustmentListener(this);
        hScroll.addAdjustmentListener(this);
      }
      public void adjustmentValueChanged(AdjustmentEvent e) {
        if (e.getSource()==vScroll) {
          System.out.println("vertical:"+e.getValue());
        } else if (e.getSource()==hScroll) {
          System.out.println("horizontal:"+e.getValue());
        }
    
      }
    }
    If you place your image display panel in CENTER as a JPanel extension you
    could modify some drawing coordinates and do a repaint on each invokation
    of the adjustmentValueChanged method.
    eschew obfuscation

  5. #5
    Join Date
    Jul 2005
    Posts
    3
    Hi, that helped, its working now =)

    Big thanks !

Similar Threads

  1. JComponent Drag and Drop help needed
    By smihs in forum Java
    Replies: 3
    Last Post: 04-05-2005, 09:19 AM
  2. JScrollBar
    By Panos in forum Java
    Replies: 1
    Last Post: 09-12-2000, 01:35 PM
  3. bug found in JComponent
    By Alan Shiers in forum Java
    Replies: 0
    Last Post: 05-17-2000, 01:06 PM
  4. bug found regarding JComponent
    By Alan Shiers in forum Java
    Replies: 0
    Last Post: 05-17-2000, 01:00 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center