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()
{
public void run()
{
update();
}
};)
The update() method includes more elements than I have posted here, some concern other GUI components and some non GUI elements.