hi Mikhail,
You first create an empty panel and make it visible by calling show().
You do not start a separate thread, but you merely put fill() into
the swing event queue, that is, adding the textAreas is peformed after
current swing stuff has finished.
The difference in the scrollPane behaviour lies in the way how
a new textArea is added to the panel. calling add(Component) adds
the component to the end of panels component list. This lets the
scrollpane scroll down every time you add a new textArea.
Adding a component on top using add(Component, 0), lets
the scrollPane scroll up.
see your modified source below.
BTW: call show() or better setVisible(true) if you have done all layout stuff.
Gernot
---------
snipp--------------------------------------------------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main
{
public static JFrame m_mainFrame;
public static JPanel m_panel = new JPanel();
public static JScrollPane m_scrollPane;
public static Dimension m_size = new Dimension(200, 300);
static int inc = 0;
public static void fill(boolean b)
{
JTextArea text;
int max = b ? 100 : 1;
if(b)
m_panel.removeAll();
for(int i = 0; i < max; i ++) {
text = new JTextArea("Text " + (inc++));
text.setLineWrap(true);
text.setWrapStyleWord(true);
text.setEditable(false);
m_panel.add(text, 0);
}
}
public static void main(String args[])
{
m_mainFrame = new JFrame();
m_mainFrame.setSize(m_size);
m_mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
m_panel.setLayout(new BoxLayout(m_panel, BoxLayout.Y_AXIS));
m_scrollPane = new JScrollPane(m_panel);
m_mainFrame.getContentPane().add(m_scrollPane, BorderLayout.CENTER);
JButton b = new JButton("Add");
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
fill(false); // add a single component
m_mainFrame.validate();
m_mainFrame.repaint();
}
});
m_mainFrame.getContentPane().add(b, BorderLayout.SOUTH);
fill(true); //add all at once
m_mainFrame.validate();
m_mainFrame.repaint();
// if easy call fill() then it don't scroll to end
// but if call hapens from another thread then it scroll to end
/*
SwingUtilities.invokeLater(new Runnable(){
public void run() {
fill(true);
m_mainFrame.validate();
m_mainFrame.repaint();
}
});
*/
m_mainFrame.setVisible(true);
}
}
-------------------snapp----------------------------------------------------------------------
*********** REPLY SEPARATOR ***********
On 11.02.02 at 12:00 [EMAIL PROTECTED] wrote:
>Send Advanced-swing mailing list submissions to
> [EMAIL PROTECTED]
>
>To subscribe or unsubscribe via the World Wide Web, visit
> http://eos.dk/mailman/listinfo/advanced-swing
>or, via email, send a message with subject or body 'help' to
> [EMAIL PROTECTED]
>
>You can reach the person managing the list at
> [EMAIL PROTECTED]
>
>When replying, please edit your Subject line so it is more specific
>than "Re: Contents of Advanced-swing digest..."
>
>
>Today's Topics:
>
> 1. Scrolling from another thread (Onopin Mikhail)
>
>--__--__--
>
>Message: 1
>From: "Onopin Mikhail" <[EMAIL PROTECTED]>
>To: <[EMAIL PROTECTED]>
>Subject: Scrolling from another thread
>Date: Sun, 10 Feb 2002 23:36:54 +0300
>
>Hello!
>
>I can't understand why the m_scrollPane (see code below) is auto scroll to
>end while adding JTextArea.
>
>//---- start
>import javax.swing.*;
>import java.awt.*;
>
>public class Main
>{
> public static JFrame m_mainFrame;
> public static JPanel m_panel = new JPanel();
> public static JScrollPane m_scrollPane;
>
> public static Dimension m_size = new Dimension(200, 300);
>
> public static void fill()
> {
> JTextArea text;
>
> m_panel.removeAll();
>
> for(int i = 0; i < 100; i ++) {
> text = new JTextArea("Text");
> text.setLineWrap(true);
> text.setWrapStyleWord(true);
> text.setEditable(false);
>
> m_panel.add(text);
> }
>
> m_mainFrame.validate();
> m_mainFrame.repaint();
> }
>
>
> public static void main(String args[])
> {
> m_mainFrame = new JFrame();
> m_mainFrame.setSize(m_size);
> m_mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> m_mainFrame.show();
>
> m_panel.setLayout(new BoxLayout(m_panel, BoxLayout.Y_AXIS));
> m_scrollPane = new JScrollPane(m_panel);
>
> m_mainFrame.getContentPane().add(m_scrollPane, BorderLayout.CENTER);
>
> // if easy call fill() then it don't scroll to end
> // but if call hapens from another thread then it scroll to end
> SwingUtilities.invokeLater(new Runnable(){
> public void run() {
> fill();
> }
> });
> }
>}
>
>//---- end
>
>What wrong?
>
>Onopin Mikhail.([EMAIL PROTECTED])
>
>
>
>--__--__--
>
>_______________________________________________
>Advanced-swing mailing list
>[EMAIL PROTECTED]
>http://eos.dk/mailman/listinfo/advanced-swing
>
>
>End of Advanced-swing Digest
Gernot Veith
Pixel GmbH
Software Entwicklung
+49-89-89868-338
www.pixel.de
[EMAIL PROTECTED]
_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing