JScrollPane vertical bar is defaulting to the bottom



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.

.



Relevant Pages

  • Re: Frame packing problem with JTextArea containing wrapped text
    ... Moreover, you can add a JTextArea to a JScrollPane, if desired: ... I recall being misled by this not long ago. ... Apparent success hinged on running on a slow or ...
    (comp.lang.java.gui)
  • Re: Wie erweitere ich die JTextArea so, dass sie automatisch die Scrollbalken anzeigt?
    ... > Eine Möglichkeit wäre zwar von JScrollPane abzuleiten und dann die ... > JTextArea der Klasse weitergeleitet werden aber das finde ich nicht so ... Prev by Date: ... Next by Date: ...
    (de.comp.lang.java)
  • Scroll problem.
    ... It's an extended JFrame with a ... JTextField in the SOUTH for input, and a JScrollPane with a JTextArea ...
    (comp.lang.java.gui)
  • Re: JScrollPane content sizing
    ... What I want to do is have a JScrollPane containing a JPanel. ... experiment, create a JTextArea using new JTextArea, and add it to a ... scroll pane because it is 1 by 1 pixel in size. ... members to the card layout and only show the object you want to show. ...
    (comp.lang.java.gui)
  • Re: JComboBox problem with new java 6u10 on ubuntu
    ... The arrow is not painted correctly on all the JComboBox's and it looks ... like there might be a problem with the JScrollPane since the scroll ... bar is sized correctly. ...
    (comp.lang.java.programmer)