JScrollPane, JTextPane, Weird scrolling happening

The code below shows the problem I am having: In short, I have a textPane in a scrollPane but when I try to getViewPort.setViewPosition(0,0); then the scroll pane briefly flashes to position 0,0 then re positions itself to display the bottom of the text pane. It is driving me wild! If I removed the InsertString function call used on the Document of the textPane, then it works as I thought it should (apart from an initial glitch where the slider bar is placed near the top...- see example)
in the example I have a 'tall' JTextPane (ie, lot's of linefeeds) with a red panel all in a JPanel which inturn lies in a JScrollPane. Click on the 'Force Update' button to force the Text pane to be updated (in my real java class, there is other stuff to change the values of the textpane) and to set the display position to 0,0. Notice how the slider button flashes to the top of the vertical bar then returns to the bottom. If you cick on the 'toggle InsetString Active' button, then the code to Insert the String to the textArea is by-passed. Now clicking on the 'Force Update' button (after the initial glitch) does send the view to the top of the Jpanel(which now contains an empty textarea and a red panel).
Can anyone give any clues as to what is causing the Scroll pane to redraw to the bottom of the panel? (and what is causing the glitch after the toggle InsertString button is pressed?)
I have tried it on JDK 1.3.1 and 1.4 and scoured the groups for similar problems but found none. Lots have people had prblems getting the JScrollpane to the bottom of the textpane but I can't seem to stop it going down!
I'd appreciate any help as I'm quickly going bald....
Marc
code begins....
-------------------------cut here--------------------------
import javax.swing.*;
import java.awt.*; //for layout managers
import java.awt.event.*; //for action and window events
import javax.swing.text.*;
public class WierdScroll extends JFrame
ScrollPane scrollPane = null;
JButton button = null;
JButton b2 = null;
JPanel screen = null;
// below is the style to be associated with the textpane
static private SimpleAttributeSet normalStyle = new SimpleAttributeSet(StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE));
static
StyleConstants.setFontFamily(normalStyle, "Courier");
StyleConstants.setFontSize(normalStyle, 12);
public WierdScroll()
super("Wierd Scrolling \'feature\'");
// create and setup the components
screen = new JPanel();
scrollPane = new ScrollPane();
scrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(250, 155));
scrollPane.setMinimumSize(new Dimension(10, 10));
scrollPane.updateConstraintView();
button = new JButton("Force update");
button.addActionListener(new java.awt.event.ActionListener()
public void actionPerformed(ActionEvent e)
scrollPane.updateConstraintView();
b2 = new JButton("toggle InsertString Active");
b2.addActionListener(new java.awt.event.ActionListener()
public void actionPerformed(ActionEvent e)
JButton b = (JButton) e.getSource();
scrollPane.allowInsertString = !scrollPane.allowInsertString;
scrollPane.updateConstraintView();
// add the component to the frame
screen.add(scrollPane);
screen.add(button);
screen.add(b2);
getContentPane().add(screen);
public static void main(String[] args) {
JFrame frame = new WierdScroll();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
frame.pack();
frame.setVisible(true);
// this extended class is the scrollpane. The problem occurrs when the doc.insertString function is called;
// When insertString is called, the scroll pane flicks to the bottom of the scroll view but when insertString
// is not called then the setViewPostion works OK....
public class ScrollPane extends JScrollPane
public JPanel panel;
public JTextPane margin;
public boolean allowInsertString = true;
/** Creates a new instance of ConstraintView */
public ScrollPane()
// create the pane
margin = new JTextPane();
// create the label
JPanel jp = new JPanel();
jp.setBackground(Color.red);
jp.setPreferredSize(new Dimension(500,500));
panel = new JPanel();
panel.setBackground(Color.white);
// add the label and pane to the panel
panel.add(margin);
panel.add(jp);
// set the scroll voew to be the panel
setViewportView(panel);
public void updateConstraintView()
// if doc has stuff in it, then clear contents
Document doc = margin.getDocument();
if (doc.getLength() > 0)
try
doc.remove(0, doc.getLength());
catch (BadLocationException ble) {}
// the allowInsertString is toggled via button on screen to stop insertString call
if (allowInsertString)
try {
doc.insertString(0, "1\n2\n3\n4\n5\n6\n7\n8\n9\n0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n0\n", normalStyle);
catch (BadLocationException ble) {}
// set viewport so that it scrolls to top
getViewport().setViewPosition(new Point(0,0));

Cheers!
using the setCaretPosition() of the JTextPane instead of the getViewport().setViewPosition(new Point(0,0)) method of the JScrollPane works a treat.
But I would have thought that the scroll pane should have 'complete' control (unless specified by programmer) of what it displays. It seems strange to have a component of a scrollpane dictate to the scrollpane what it should show...
Thanks for your assistance.
On a different note, has anyone noticed that the word 'c l a s s' has become cl***?? Is this a victim of sun's new swear filter to go with the new site look?

Similar Messages

  • Forcing a JTextPane to scroll down

    Hello I have a JTextPane inside a JScrollpane
    how can I force the JTextPane to scroll down when text is added to it?

    Why is it so hard to do a search before posting a question??????
    I took 3 words from you topic, "+jtextpane +scroll +down" and did a search and found that this question has been asked and answered dozens of times.

  • Why does my safari browser frequently gives me a gray screen? I resetted my safari many times and it doesn't help. Also sometimes when I scroll it gives me this weird scrolling effect where everything is either upside or messed up.

    Why does my safari browser frequently gives me a gray screen? I resetted my safari many times and it doesn't help. Also sometimes when I scroll it gives me this weird scrolling effect where everything is either upside or messed up.

    This article is for startup but it might help.  http://support.apple.com/kb/ts2570

  • Help:Jtextpane in Jscrollpane , the vertical scroll bar  at bottom

    Immediate help needed if possible.
    The scrollpane that contains the textpane is showing the vertical bar at the bottom of the page even if use :-
    scrollRectToVisible(new Rectangle(0,0,1,1));
    Here is my code:-------------------------
    JPanel panel = new JPanel();
    JTextPane textPane = createTextPane();
    textPane.setEditable(false);
    textPane.scrollRectToVisible(new Rectangle(0,0,1,1));
    JScrollPane paneScrollPane = new JScrollPane(textPane);
    paneScrollPane.setPreferredSize(new Dimension(525, 285));
    paneScrollPane.setMinimumSize(new Dimension(14, 48));
    gc.addCentered(paneScrollPane);
    add ( panel );

    From
    http://search.java.sun.com/search/java/index.jsp?qp=&nh=10&qt=%2Btitle%3Ajtextpane+%2Btitle%3Atop&col=javaforums
    scrollRectToVisible(new Rectangle(0,0,1,1))

  • JScrollPane, JTextPane and JPanel... wtf?

    i am working on a extending a JPanel to have a JTextPane in a JScrollPane. When i add the JTextPane to the JScrollPane inside of the JPanel object it doesn't scroll horizontally, but when i add the whole panel to a JScrollPane in the JFrame that i have testing it, it does scroll horizontally, the JScrollPane in both instances the JScrollBar has both horizontal and vertical scrolling set to as needed.
    why is this happening?

    Thanks for your answers!
    Revalidating did the trick, however I still have a problem with the JScrollPane... I created a new JScrollPane and wrote so:
    myScrollPane = new JScrollPane(internalPanel);
    myScrollPane.setPreferredSize(new java.awt.Dimension(2, 205));
    myScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    //Added Code
    handScrollPane.setViewportView(handPanel);
    //End Added Code
    mainPanel.add(myScrollPane, BorderLayout.SOUTH);Then whenever items were added to the internalPanel I changed the scroll-bar's maximum value to the height of the internalPanel by writing:
    myScrollPane.getVerticalScrollBar().setMaximum(internalPanel.getHeight());But when I run my application, the scroll-bar's maximum value doesn't change even though items are added to the internalPanel. Why is this?
    With Thanks,
    laginimaineb.
    B.U.M.P (Sorry)

  • JTree in JScrollPane doesn't scroll

    I have a JTree in a JScrollPane that doesn't scroll.
    Background: it used to!
    History: I moved the creation of the JTree to inside an Object that returns it with a ".getTree()" method. Since then, it hasn't been scrolling. I even set the JScrollPane to always have scrollbars and they just sit there inactive.
    Does anyone know what this would happen and/or how to fix it?
    thanks.

    That actaully worked, but it was way to big. I'm not sure what I did, but I changed the values to Studio ONE's defaults and now it works ok.

  • JTextPane settext scrolling bahaviour

    Hi
    I have a JTextPane in a scrollpane to show HTML text with some hyperlinks and images. What i want to do is update some parts of it, and keep the viewport at the same position. The new content in the textpane has the same size, i just change the background of some cells in a HTML table. The code looks something like this:
    JTextPane infoPane = new ...
    JScrollPane infoScroll = new JScrollPane(infoPane);
    // Save the current view
    Rectangle rect = infoScroll.getViewport().getViewRect();
    // Set the new content
    infoPane.setText(".....");
    // Restore the old view
    infoPane.scrollRectToVisible(rect);
    // Make sure the rectangle has relevant data for debugging
    System.out.println(rect);The problem is: settext() seems to force the scrollpane to scroll to the end of the document a while after the text is set. So, the position is right some milliseconds after i call settext and scrollrecttovisible, and then it scrolls to the end. An ugly workaround i found was creating a timer and calling infoPane.scrollRectToVisible(rect) 500 ms or so after i set the text.
    It looks like settext has some listeners (or something else) that perform the scrolling a while after it is called, but i just cant find a way to prevent it. I have spent many hours looking on google without success, so help is much appreciated.
    Thanks in advice.

    Thank you so much!!
    After i read that i fixed the problem in 2 minutes, and before i spent many hours on it without success. Everything i needed to do was adding:
    DefaultCaret caret = (DefaultCaret)infoPane.getCaret();
    caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);to the beginning of my code, and now i dont need to save any rectangle at all. It just works the way i hoped it would.

  • JTextPane - horizontal scrolling

    hey all,
    this is, I'm sure, a stupid question... but it has me stumped.
    I've got an exception dialog for handling uncaught exceptions - and I want to display the stack trace there. I'm using a JTextPane for this, but my problem is that it doesn't seem to be capable ot horizontal scrolling (or rather I cant make it work!).
    I have the scrollbars on - but thats on the scroll pane, and I think there's probably something I need to do to the text pane to make this work....
    simple answer someone?
    ta
    dim

    Here is a program the shows two ways of having the horizontal scrollBar appear when required:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.text.*;
    public class TestNoWrap extends JFrame
         JTextPane textPane;
         JScrollPane scrollPane;
         public TestNoWrap()
              JPanel panel = new JPanel();
              panel.setLayout( new BorderLayout() );
              setContentPane( panel );
              // no wrap by adding text pane to a panel using border layout
              textPane = new JTextPane();
              textPane.setText("1234567890 1234567890 1234567890");
              JPanel noWrapPanel = new JPanel();
              noWrapPanel.setLayout( new BorderLayout() );
              noWrapPanel.add( textPane );
              scrollPane = new JScrollPane( noWrapPanel );
              scrollPane.setPreferredSize( new Dimension( 200, 100 ) );
              panel.add( scrollPane, BorderLayout.NORTH );
              // no wrap by overriding text pane methods
              textPane = new JTextPane()
                   public void setSize(Dimension d)
                        if (d.width < getParent().getSize().width)
                             d.width = getParent().getSize().width;
                        super.setSize(d);
                   public boolean getScrollableTracksViewportWidth()
                        return false;
              textPane.setText("1234567890 1234567890 1234567890");
              scrollPane = new JScrollPane( textPane );
              scrollPane.setPreferredSize( new Dimension( 200, 100 ) );
              panel.add( scrollPane );
         public static void main(String[] args)
              TestNoWrap frame = new TestNoWrap();
              frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
              frame.pack();
              frame.setVisible(true);
    }

  • MacBook won't turn on 98% of the time, help! Very weird things happening!

    Hi there, any advice you have for me would me much appreciated. What happened with my Macbook is all very strange.
    So, the morning before last, I was on my Macbook (which I bought in 2006 so it's on the older side, but otherwise worked great) and it was running low on battery. I was late for work so didn't plug it in before leaving. I left it open. However I have no roomates, pets, etc.
    When I get home that evening, I plugged in the powercord and it wouldn't turn on. After trying a million different things, like taking the battery out, reseting the PRAM, etc, there were a few times where I got it to turn on, went through the log on screen, and was able to use my computer like normal for about 5 minutes or so, and then it would just shut off.
    There were also many times when I would pres the power button and the screen would flash like it was about to turn on, and then it would make the noise that the computer makes when you're typically on 0% battery and it shuts down. Later in the night, when the computer was closed but plugged into the power outlet, it was by itself (meaning I wasn't pressing anything) making the noise that it was restarting. It made that noise about every 30 seconds for a few minutes. Weird.
    It was also doing inconsistent things re: the sleep light. At times it was on, including when the computer was closed, and sometimes it would go from on to off. Also, the battery/charing was doing weird things. The few times I was able to get past the log in screen it showed that it was at 0%, even though the light on the adapter was green and it had been plugged in for a long time. Another time the battery icon had an X through it. I have 2 different batteries and neither one of them seemed to make a difference.
    Also, yesterday I brought my computer into the Apple store and they put in a battery that was charged and it didn't seem to make a difference either. One more weird thing: when I did get the computer on in the spotlight bar it said it was indexing??
    Anyways, I'm just confused/upset, etc. because I literally did nothing to the computer. I didn't drop it/spill on it, etc. I'm also frustrated because the guy at the genius bar litteraly only spent like 5 minutes looking at before he said i was SOL. He said it was the "main board" that was the problem. One of the things like I'm just confused by is if the main board really does need to be replaced, then how was I able to get the computer on and working those few times???
    Sorrry this is so long, any help would be awesome!! I just don't have the money to buy a new computer, and I'm not sure it will be worth it for me to pay $300 to have it fixed when my computer's already pretty old and other things haven't been working for awhile (like my disc drive, camera). THANK YOU!!!

    EmadJamal,
    the battery won’t melt because of long connection to the AC charger, although that has the potential of reducing the battery’s life.
    Do you have access to a second 60 W AC charger to test with your MacBook Pro, or access to a second 13-inch MacBook Pro against which your AC charger could be tested?

  • Weird things happen in Edit view...

    Some (but NOT all)of my photos do weird things when you Double-Click them to get into Edit view. Most of my photos zoom in to their appropriate sizes and are viewed 'normally', however, other photos initially come up in their normal zoomed size then in a second of two, zoom in again several times larger than they should be, and the colors will pop your eyes out, like a reverse negative or something and the photos gets all 'chunky' or 'blocky'. Fortunately, only a small number of my photos zoom in wildly like this.
    Anyone seen this, have a clue what causes it and most importantly, can it be fixed to STOP doing this?
    Thanks

    Well, I used to see this problem way back in the v7 days of the purple app. It's not an FCP exclusive problem, though I have seen it happen one time with Final Cut.
    Glad to help you solve this one, Jamie. You can help others who come to this thread later, by marking the Helpful & Solved posts in the thread. That way, when others are searching for solutions to this problem, they can scan down quickly and easily to those posts, and see the yellow and green stars.
    I've posted this request on a number of threads, both those I've helped or solved, and those I had no helpful contributions for. I'm just trying to help make the new forum as usable as ut can be.

  • Creating JScrollPane with vertical scroll only...

    Anyone got any idea how to use a JScrollPane to perform vertical scrolling only?
    The components should be stacked vertically, as wide as the scrollpane and as high as they need to be.
    Is it only possible by adding a ComponentListener to the JScrollPane (or the JViewPort) and setting the maximum width of the components to the width of the viewport?
    Incidentally, anyone know how a JLabel (or any text component) calculates its preferred size when it can wrap the text contained? Ie, how do you get the preferred height for a specified width?

    Doh - implement Scrollable to return true for getScrollableTracksViewportWidth()!

  • Weird thing happening with my iCal

    A weird thing is happening with my iCal, I don't know if it's a bug... Today I was printing the months of September and October with all my appointments, and when I was checking October I have noticed that 14th was printed as Saturday and as Sunday too, and 15th became Monday, 16th became Tuesday, and so on.
    But in my computer screen the calendar was correct: 14 Saturday, 15 Sunday, 16 Monday, etc.
    I cannot figure out what could be happening and how to solve this.
    Any idea?
    Any help will be appreciated.
    PowerMac G4 1.5 AGP Graphics   Mac OS X (10.4.7)   1 GB RAM

    Today a friend of mine said that he had a similar problem with iCal when printing the month of October, and that he fixed this by changing the Time and Zone setting in System Preferences. When he changed the setting from Sao Paulo, Brazil to Buenos Aires, Argentina (the both have the same time zone) the problem was solved.
    Today I did this and my problem dissapeared too. But I would like to know why is this happening.

  • Weird scrolling question again

    I'll try this posting again with a clearer description of what's happening.
    Every time I copy from one drive to another, the copied-to drive's scroll bar always shoots back up to the top.  For example, I have 4 data drives (all four are the same, redundant backup, etc.) that hold all my files, A, B, C and D.  I open A and one of the others, match up the scroll bar positions, then begin copying revised files.  Once one goes to B, B immediately scrolls back to the top each time and then I have to scroll back down to match where A is again.
     I'm on Mountain Lion, all updates current.  Snow Leopard never showed this behavior.
    Thanks for anyone's two cents on this one, this is aggravating to say the least.

    i cant scroll in personalize either. how did you get vertical scrolling in iTunes?
    T400
    Intel® Core™2 Duo T9400 2.53 GHz__Windows 7 Professional__3GB Memory__160GB, 7200RPM Hard Drive__14.1” WXGA+TFT-CFL__ATI Mobility Radeon™ HD 3470--

  • JScrollPane - change vertical scroll-bar

    I am developing an application to be run on a touch-screen monitor. The customer has asked that I make the vertical scroll-bar on the tables easier to use. How do I set the width of the scroll-bar(s) in a JScrollPane?

    Do you want to set the width of all vertical scroll bars (and in that case, probably also the height of all horizontal scroll bars too) OR the width of one specific scroll bar of one specific scroll pane?
    db

  • Weird things happen when i resize.

    when i use the marquee tool and i select a box portion of something. then i got to transform selection and i set the dimensions to, lets say, 100px by 100px.
    then i copy and paste into a new document sometimes it decides to make the new document  1 or 2 pixels wider which is weird and annoying cause then i have this white line going down the side after pasting.
    and then using that image as a background pattern looks weird because i need to resize the canvas and get rid of that empty line. why does this happen its getting annoying.
    and feathering is set to 0

    If I understand correctly Selection > Transform Selection is basically the same as editing a Mask in that Interpolation has to be employed.
    If you are sure you want a rectangular Selection of certain pixel-dimensions you should probably set Style in the Options Bar to Fixed Size before making it.

Maybe you are looking for

  • Header and Item consolidation delta

    Hi all, If I do a lookup on header DSO while loading into a consolidated DSO from a item level DSO, how do we make sure that deltas are managed if only changes occur in the header DSO? In this case since the driver is the item level DSO, I would imag

  • Power mac 7300 connection to monitor

    The connection to the monitor is made through an adaptator with 10 buttons. During a transport of the mac and the monitor, some buttons have been put "off". Can you tell me what buttons should be "on"?

  • How to specify ranges in value mapping?

    Hi, Is there any possibility of specifying ranges in a value mapping?

  • IPod won't mount in iTunes

    Hello there, I have a 20gb iPod that's nearly 3 years old & my PC is running Windows XP Professional with SP2. And I've got the latest version of iTunes (7.4.3). Recently my iPod started behaving strangely, skipping during songs sometimes, and someti

  • Time machine does not respond when entered for file recovery

    When I enter time machine, the expected display appears but there is no response when trying to move time back, select a file or exit, even with Cmd Q. I can only get back to a useable screen by powering down or using Cmd+Alt+Esc. I was able to use T