I have a Auto Scroll Problem with JScrollPane. For example, in the following code, if we change the value of the Spinner, all TextAreas's value will be Changed. Then JScrollPane auto scroll to the latest changed JComponent.
Can I prevent this kind of Auto Scroll?
Some one marked that this question is "duplicate" but not at all. This question Java / Swing : JTextArea in a JScrollPane, how to prevent auto-scroll? is a JTextArea in a JScrollPane, so we can use DefaultCaret to solve this problem, because JTextArea has DefaultCaret. But in my question, it is a JPanel in a JScrollPane, a JPanel has not a DefaultCaret. So we cannot use the same way to solve this problem.
import java.awt.BorderLayout
import javafx.scene.layout.Pane;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class ScrollControl {
static JFrame main_frame = new JFrame();
static JScrollPane scroll_pane = new JScrollPane();
static JPanel pane = new JPanel();
static JSpinner spinner = new JSpinner();
static JTextArea text1 = new JTextArea();
static JTextArea text2 = new JTextArea();
static JTextArea text3 = new JTextArea();
static JTextArea text4 = new JTextArea();
static JTextArea text5 = new JTextArea();
static JTextArea text6 = new JTextArea();
static JTextArea text7 = new JTextArea();
public static void main(String args[]){
main_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
spinner.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent arg0) {
text1.setText(spinner.getValue().toString());
text2.setText(spinner.getValue().toString());
text3.setText(spinner.getValue().toString());
text4.setText(spinner.getValue().toString());
text5.setText(spinner.getValue().toString());
text6.setText(spinner.getValue().toString());
text7.setText(spinner.getValue().toString());
}
});
pane.add(spinner);
pane.add(text1);
pane.add(text2);
pane.add(text3);
pane.add(text4);
pane.add(text5);
pane.add(text6);
pane.add(text7);
scroll_pane = new JScrollPane(pane);
main_frame.setSize(300, 100);
main_frame.getContentPane().add(scroll_pane, BorderLayout.CENTER);
main_frame.setVisible(true);
}
}