What causes refresh in TreeSelectionListener?

R

R W

I am trying to figure out what would make a command work in one place
but not in another. In my code I have a scrollPane initialized to hold
some text.

myTextPane = new JTextPane();
StyledDocument doc = myTextPane.getStyledDocument();
try {
doc.insertString(doc.getLength(),
"stuff\n\n\n\n\n\n\n\n\nstuff 2\n\n\n\n\n\n\n\n\n\n\n\n\nstuff 3\n",
null);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
JPanel myPanel = new JPanel(new BorderLayout());
myPanel.add(myTextPane);
htmlView = new JScrollPane(myPanel);
htmlView.getVerticalScrollBar().setValue(0); //The stubborn command!

After the code is first set up the scroll bar is scrolled all the way
to the bottom. Then I have a listener that runs.

public void valueChanged(TreeSelectionEvent e) {
htmlView.getVerticalScrollBar().setValue(0);
}

When you click on something all it does is run the command to set the
scroller to the top. The first time it doesn't work but in the listener
it works! What is going on? It seems like something different is
happening when you click on something in the gui than what is happening
when you set up the JScrollPane at first. What is making it run the
setValue command the second time but not the first? Is there some
sneaky sort of refresh going on that I haven't figured out?

Thanks for any thoughts. I've been banging my head on this scroll
problem for a while.
 
R

R W

After five days of banging my head on this problem I have found
somewhat of an answer. For some reason swing only wants to refresh the
scrollbar if an event occurs. When someone clicked in my tree and
event occurred and it was happy about running the setValue command to
scroll the bar to the top. However, my ultimate goal was to change the
text each time someone clicked in the tree, which would set the scroll
bar back to the bottom. So I needed another type of event to occur
that would not involve the user clicking. After all, what I am trying
to avoid in the first place is making the user drag the scrollbar back
to the top. So it occurred to me to do the following...
Code:
//Declare this at the top
Timer timer;

Code:
//Fix stubborn scrollbar problem
timer = new javax.swing.Timer(1, new ActionListener( ) {
public void actionPerformed(ActionEvent e) {
htmlView.getVerticalScrollBar().setValue(0);
stop();
}
});
timer.start();

....and add a method like this.

Code:
private void stop() {
timer.stop();
}

If I ever want to put the scroll bar at the top I just call
timer.start();. The setValue() method will run inside the event even
though it won't other places. I hope that helps someone in the future.
Does anybody with any better knowledge of the way this all work have
an explanation as to why it behaves this way? Is it a bug in Swing?
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top