Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / August 2006

Tip: Looking for answers? Try searching our database.

JScrollPane vertical bar is defaulting to the bottom

Thread view: 
Debs - 31 Aug 2006 09:45 GMT
Hi,

I have a JScrollPane for a large panel, when it displays the vertical
bar is at the bottom. I want it at the top - I have various other
JScrollPanes elsewhere that default to the top as expected.

I believe the difference may be that this panel contains JTextAreas,
and I have read that setting the caret position can help. However, with
it having multiple, I'm not sure what to do. I have tried setting the
focus and caret position in the first JTextArea, but this did not work.

Sample code:

=======================================
...

JPanel innerDetailPanel = new JPanel(new GridBagLayout());
innerDetailPanel.setBorder(null);

int gridy=0;

GridBagConstraints detailPanelConstraints = new GridBagConstraints();
detailPanelConstraints.gridx = 0;
detailPanelConstraints.gridy = gridy;
detailPanelConstraints.weightx=0.5;
detailPanelConstraints.fill = GridBagConstraints.HORIZONTAL;
detailPanelConstraints.insets = new Insets(5, 1, 1, 1);

//title
JLabel titleLabel = new JLabel("Title label", JLabel.CENTER);
titleLabel.setBackground(TITLECOLOR);
titleLabel.setOpaque(true);
titleLabel.setFont(TITLEFONT);
innerDetailPanel.add(titleLabel, detailPanelConstraints);
gridy++

for(int i=0;i<names.length; i++) {

    detailPanelConstraints.gridx = 0;
    detailPanelConstraints.gridy = gridy;
    detailPanelConstraints.weightx=0.5;

    String name = names[i];
    JTextArea nameValue = new JTextArea(name);

    nameValue.setLineWrap(true);
    nameValue.setEditable(false);
    nameValue.setBackground(getContentPane().getBackground());
    innerDetailPanel.add(nameValue, detailPanelConstraints);
    gridy++;

}

JPanel northAlign = new JPanel(new BorderLayout());
northAlign.add(innerDetailPanel, BorderLayout.NORTH);
JScrollPane locationDetailPanel = new JScrollPane(northAlign,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
Border compoundBorder = BorderFactory.createCompoundBorder(
    new EmptyBorder(5,20,5,20), new LineBorder(Color.DARK_GRAY));
locationDetailPanel.setBorder(new CompoundBorder(compoundBorder, new
EmptyBorder(5,5,5,5)));

...

=======================================

I tried the same code but using JLabel rather than a textarea, and the
scroll bar is at the top as expected.
The reason I'm using the textareas is to ensure text correctly wraps. I
know you can make JLabels wrap using <html> but the labels steal as
much room as they can, rather than just wrapping within the expected
cell space. The JTextAreas correctly display what I want.

Like I said above, I also tried setting the focus and caret position on
the first (top most) JTextArea,  but this did not work. I've tried a
few methods for setting the position of the scroll bar, but they do not
work either.

Any suggestions would be much appreciated, thank you.
Michael Rauscher - 31 Aug 2006 11:16 GMT
Debs schrieb:
> Hi,
>
[quoted text clipped - 6 lines]
> it having multiple, I'm not sure what to do. I have tried setting the
> focus and caret position in the first JTextArea, but this did not work.

A few days ago someone asked the same in c.l.j.programmer - but for the
horizontal direction.

The behaviour is caused by JComponent that reaches scrollRectToVisible
up the component tree. Knowing this, the solution is obvious: just
intercept the chain.

To intercept the chain you'd have to put JTextArea into a container that
implements it's own version of scrollRectToVisible. E.g. a JViewport or
another panel that overrides this method.

So the solution for the code you gave in your previous post is simple:

JPanel innerDetailPanel = new JPanel(new GridBagLayout()) {
    public void scrollRectToVisible( Rectangle r ) {}
};

Bye
Michael
ickz - 31 Aug 2006 11:34 GMT
> Debs schrieb:
> > Hi,
[quoted text clipped - 27 lines]
> Bye
> Michael

That did the trick!
Your explanation is very clear and useful, and the solution is very
neat.

Thank you!
Debs


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.