Adding fixed position content to a scrolled panel

S

Simon Andrews

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);
}
}
}
 
H

hiwa

Simon said:
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.
 
K

Knute Johnson

Simon said:
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
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top