Adding fixed position content to a scrolled panel

Discussion in 'Java' started by Simon Andrews, Dec 7, 2006.

  1. I have a problem with a drawn panel which is embedded into a
    JScrollPane. Most of the content needs to scroll with the pane, but I'd
    also like to be able to add a small label at a fixed position in the
    viewport.

    It seems like I should be able to do this using the getVisibleRect()
    function to see what we're looking at and moving the position of the
    fixed component to compensate.

    However when I do this I get odd effects. If I scroll by clicking on
    the empty parts of the scroll bar everything works fine, but if I drag
    the scrollbar handle or use the arrows at the end the fixed text scrolls
    with the window, leaving a ghost trail behind it. I'm assuming this is
    an optimisation the ScrollPane makes, but I've looked and can't find how
    to turn it off.

    I've attached a short program which demonstrates this. Any suggestions
    for how to get this working would be most appreciated.

    Cheers

    Simon.

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

    public class ScrollBug extends JFrame {

    private JScrollPane scrollPane;

    public static void main(String[] args) {
    new ScrollBug();
    }

    public ScrollBug () {
    scrollPane = new JScrollPane(new BigPanel());
    setContentPane(scrollPane);
    setSize(500,100);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    }

    private class BigPanel extends JPanel {

    public Dimension getPreferredSize () {
    return new Dimension(10000,100);
    }

    public void paintComponent (Graphics g) {
    super.paintComponent(g);
    Rectangle r = getVisibleRect();
    int x = r.x;

    for (int i=1;i<100;i++) {
    g.drawString("Moves "+i, i*100, 40);
    }
    g.drawString("Fixed Position", x, 20);
    }
    }
    }
    Simon Andrews, Dec 7, 2006
    #1
    1. Advertising

  2. Simon Andrews

    hiwa Guest

    Simon Andrews wrote:
    > I have a problem with a drawn panel which is embedded into a
    > JScrollPane. Most of the content needs to scroll with the pane, but I'd
    > also like to be able to add a small label at a fixed position in the
    > viewport.
    >
    > It seems like I should be able to do this using the getVisibleRect()
    > function to see what we're looking at and moving the position of the
    > fixed component to compensate.
    >
    > However when I do this I get odd effects. If I scroll by clicking on
    > the empty parts of the scroll bar everything works fine, but if I drag
    > the scrollbar handle or use the arrows at the end the fixed text scrolls
    > with the window, leaving a ghost trail behind it. I'm assuming this is
    > an optimisation the ScrollPane makes, but I've looked and can't find how
    > to turn it off.
    >
    > I've attached a short program which demonstrates this. Any suggestions
    > for how to get this working would be most appreciated.
    >
    > Cheers
    >
    > Simon.
    >
    > import java.awt.*;
    > import javax.swing.*;
    >
    > public class ScrollBug extends JFrame {
    >
    > private JScrollPane scrollPane;
    >
    > public static void main(String[] args) {
    > new ScrollBug();
    > }
    >
    > public ScrollBug () {
    > scrollPane = new JScrollPane(new BigPanel());
    > setContentPane(scrollPane);
    > setSize(500,100);
    > setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    > setVisible(true);
    > }
    >
    > private class BigPanel extends JPanel {
    >
    > public Dimension getPreferredSize () {
    > return new Dimension(10000,100);
    > }
    >
    > public void paintComponent (Graphics g) {
    > super.paintComponent(g);
    > Rectangle r = getVisibleRect();
    > int x = r.x;
    >
    > for (int i=1;i<100;i++) {
    > g.drawString("Moves "+i, i*100, 40);
    > }
    > g.drawString("Fixed Position", x, 20);
    > }
    > }
    > }

    I don't recommend to tackle these kind of challenge.
    You/we should use simple multi-container solution.
    Implementing a non-scrolling part in a scrolling component is a waste
    of time and stamina.
    hiwa, Dec 7, 2006
    #2
    1. Advertising

  3. Simon Andrews wrote:
    > I have a problem with a drawn panel which is embedded into a
    > JScrollPane. Most of the content needs to scroll with the pane, but I'd
    > also like to be able to add a small label at a fixed position in the
    > viewport.
    >
    > It seems like I should be able to do this using the getVisibleRect()
    > function to see what we're looking at and moving the position of the
    > fixed component to compensate.
    >
    > However when I do this I get odd effects. If I scroll by clicking on
    > the empty parts of the scroll bar everything works fine, but if I drag
    > the scrollbar handle or use the arrows at the end the fixed text scrolls
    > with the window, leaving a ghost trail behind it. I'm assuming this is
    > an optimisation the ScrollPane makes, but I've looked and can't find how
    > to turn it off.
    >
    > I've attached a short program which demonstrates this. Any suggestions
    > for how to get this working would be most appreciated.
    >
    > Cheers
    >
    > Simon.
    >
    > import java.awt.*;
    > import javax.swing.*;
    >
    > public class ScrollBug extends JFrame {
    >
    > private JScrollPane scrollPane;
    >
    > public static void main(String[] args) {
    > new ScrollBug();
    > }
    >
    > public ScrollBug () {
    > scrollPane = new JScrollPane(new BigPanel());
    > setContentPane(scrollPane);
    > setSize(500,100);
    > setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    > setVisible(true);
    > }
    >
    > private class BigPanel extends JPanel {
    >
    > public Dimension getPreferredSize () {
    > return new Dimension(10000,100);
    > }
    >
    > public void paintComponent (Graphics g) {
    > super.paintComponent(g);
    > Rectangle r = getVisibleRect();
    > int x = r.x;
    >
    > for (int i=1;i<100;i++) {
    > g.drawString("Moves "+i, i*100, 40);
    > }
    > g.drawString("Fixed Position", x, 20);
    > }
    > }
    > }


    You might consider drawing the fixed part on the GlassPane.

    http://java.sun.com/docs/books/tutorial/uiswing/components/rootpane.html#glasspane

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Dec 7, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. johnp
    Replies:
    4
    Views:
    3,668
    Toby Inkster
    May 23, 2005
  2. Lo?c Mah?
    Replies:
    2
    Views:
    1,687
    Lo?c Mah?
    Nov 29, 2004
  3. Astan Chee
    Replies:
    1
    Views:
    407
  4. Replies:
    0
    Views:
    107
  5. Replies:
    1
    Views:
    120
    Thomas 'PointedEars' Lahn
    Nov 15, 2007
Loading...

Share This Page