repainting problem with JScrollPane & JTextArea

I am having a lot of trouble with repaint() and JScrollPanes

My interface is a panel containing

- north : a Jscrollpane containing a JtextArea

- south : a JTable

The TextArea works as a tree, listing the selected items of the Table.

Whenever we click on one line of the JTable, I remove the JTextArea from the JScrollPane, and add it again after setting the new text (selected line of the table) in the TextArea which grows in number of lines.

I want the ScrollPane to get the size of the JTextArea concerning its height (not its width, in order to show only an horizontal scrolling if the lines of the TextArea are too long).

My problems are with the repaint of the JScrollPane

When I test with system.outs, to inquire about the sizes of the northScrollPane (after its method repaint), whethever I resize the scrollpane or not (setSize, setPreferredSize), it seems that it does not take into account the new size.

I always have to scroll down to find the last line added to the JTextArea.

The weird thing is that when I have a TextArea that does not need to show the scrollbars because the text is sufficiently

short, I have no problem displaying the whole JTextArea full size.

The method is the following :

update(){

northScrollPane.remove(fatherTextArea);

fatherTextArea.setText(fatherText);

fatherTextArea.setCaretPosition(0);

northScrollPane.setViewportView(fatherTextArea);

Dimension dimWithTextAreaHeight =new Dimension(

northScrollPane.getWidth (),fatherTextArea.getPreferredScrollableViewportSize().height);

northScrollPane.getViewport().scrollRectToVisible(

new Rectangle(0,dimWithTextAreaHeight.height , dimWithTextAreaHeight.width,1));

//northScrollPane.setSize(dimWithTextAreaHeight);

//northScrollPane.setPreferredSize(dimWithTextAreaHeight);

northScrollPane.revalidate();

northScrollPane.repaint();

this.repaint();//'this' is the JPanel

}

Could anyone help me find the problem encountered here ? It is the first time I post on a this forum.

Thanks !

NB : Whenever the update() method is called, I declare it in a thread like this :

SwingUtilities.invokeLater(new Runnable()

{

publicvoid run()

{

update();

}

};)

The update() method includes more elements than I have posted here, some concern other GUI components and some non GUI elements.

[2914 byte] By [gzellea] at [2007-11-27 1:27:55]
# 1

There's no need to remove/revalidate/repaint the textArea/scroll pane.

Just set the new text and the caret position to 0.

Here's an example:

import java.awt.BorderLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

public class TextAreaScrollPaneTest {

public static void main(String[] args) {

try {

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);

final JTextArea textArea = new JTextArea();

textArea.setText("No scroll bar");

JPanel panel = new JPanel(new BorderLayout());

panel.add(new JScrollPane(textArea), BorderLayout.CENTER);

JButton button = new JButton("Change Text");

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

textArea.setText("Scroll bar appear when text change");

textArea.setCaretPosition(0);

}

});

panel.add(button, BorderLayout.SOUTH);

frame.add(panel);

frame.setSize(150, 150);

frame.setVisible(true);

} catch (Exception e) {e.printStackTrace();}

}

}

If you want further help post a Short, Self Contained, Compilable and Executable, Example Program ([url http://homepage1.nifty.com/algafield/sscce.html]SSCCE[/url]) that demonstrates the problem.

Rodney_McKaya at 2007-7-12 0:25:07 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thank you for the previous code, however, what you showed to me does not resolve the problem of resizing the JScrollPane to the Height of the JTextArea at each action.

It is true a lot of the method I called (repaint/revalidate etc...) are not necessary ! I removed them to what you said.

What I really want is not to have to scroll vertically but still see my full text in the Area (horizontally scrollable) ?

Am I still not understanding the problem right ?

Thanks

gzellea at 2007-7-12 0:25:07 > top of Java-index,Desktop,Core GUI APIs...
# 3

Not sure I understand what you are asking, but JTextArea has a setRows(..) method. So when you reset the text you should be able to reset the rows. You would then probably need to revalidate the panel so it can lay out the frame again. Or maybe you event need to use pack() on the frame to get the proper size of the text area.

camickra at 2007-7-12 0:25:07 > top of Java-index,Desktop,Core GUI APIs...