JScrollPane's Scrollverhalten koppeln

Hallo, ich hab zwei JScrollPane (jsp1 und jsp2) nebeneinander und möchte jetzt, wenn in A/B nach unten gescrollt wird, soll auch in dem anderen gescrollt werden,

Beispiel:
[spoiler]```/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package javaapplication;

import java.awt.;
import java.awt.event.
;
import java.io.;
import java.util.regex.
;
import javax.swing.*;

/**

  • @author CB
    */
    public class RegexAufVerz {

    private static Pattern p = null;

    /**

    • @param args the command line arguments
      /
      public static void main(String[] args) {
      // TODO code application logic here
      final JTextField verz = new JTextField(“F:\”);
      final JTextField rege = new JTextField("^.
      ([sS]\d{1,2}[eE]\d{1,2}).*$");
      JButton such = new JButton(“Suche …”);
      final JCheckBox unte = new JCheckBox(“Unter-Verz.?”);
      final JTextArea jta1 = new JTextArea();
      final JTextArea jta2 = new JTextArea();
      JPanel jp1 = new JPanel(new GridLayout(2, 3));
      JPanel jp2 = new JPanel(new GridLayout(1, 2));
      jp1.add(verz);
      jp1.add(rege);
      jp1.add(such);
      jp1.add(unte);
      jp1.add(new JLabel());
      jp1.add(new JLabel());
      jp2.add(new JScrollPane(jta1));
      jp2.add(new JScrollPane(jta2));
      JFrame jf = new JFrame(“41RegExVerz”);
      jf.setLayout(new BorderLayout());
      jf.add(jp1, BorderLayout.NORTH);
      jf.add(jp2, BorderLayout.CENTER);

      such.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
      SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
      suche(verz.getText(), rege.getText(), unte.isSelected(), jta1, jta2);
      }
      });
      }
      });

      jf.pack();
      jf.setSize(800, 600);
      jf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
      jf.setVisible(true);
      }

    private static void appendLn(final String[] strings1, final String[] strings2, final JTextArea jta1, final JTextArea jta2, final boolean ln) {
    if (strings1 != null) {
    for (String string : strings1) {
    jta1.append(string);
    jta1.append(" “);
    }
    }
    if (strings2 != null) {
    for (String string : strings2) {
    jta2.append(string);
    jta2.append(” “);
    }
    }
    if (ln) {
    jta1.append(”
    “);
    jta2.append(”
    ");
    }
    }

    private static String[] strArr(final String… strings) {
    String[] res = new String[strings.length];
    System.arraycopy(strings, 0, res, 0, strings.length);
    return res;
    }

    private static boolean suche(final String verz, final String rege, final boolean unte, final JTextArea jta1, final JTextArea jta2) {
    try {
    p = Pattern.compile(rege);
    File file = new File(verz);
    if (!file.isDirectory() || !file.canRead()) {
    appendLn(strArr(file.toString(), “!file.isDirectory() || !file.canRead()”), null, jta1, jta2, true);
    return false;
    }
    File[] fl = file.listFiles();
    if (fl == null || fl.length == 0) {
    appendLn(strArr(file.toString(), “fl == null || fl.length == 0”), null, jta1, jta2, true);
    return false;
    }
    for (File f : fl) {
    suche1(f, unte, jta1, jta2);
    }
    } catch (Exception e) {
    appendLn(strArr(verz, rege, e.toString(), e.getMessage()), null, jta1, jta2, true);
    p = null;
    return false;
    }
    p = null;
    return true;
    }

    private static boolean suche1(final File file, final boolean unte, final JTextArea jta1, final JTextArea jta2) {
    try {
    if (file == null) {
    appendLn(strArr(“file == null”), null, jta1, jta2, true);
    return false;
    }
    if (!file.canRead()) {
    appendLn(strArr(file.toString(), “!file.canRead()”), null, jta1, jta2, true);
    return false;
    }
    Matcher m = p.matcher(file.getName());
    if (m.find()) {
    appendLn(strArr(file.toString()), null, jta1, jta2, false);
    for (int i = 0; i < m.groupCount() + 1; i++) {
    appendLn(null, strArr(m.group(i)), jta1, jta2, false);
    }
    appendLn(null, null, jta1, jta2, true);
    }
    if (unte) {
    if (file.isDirectory()) {
    File[] fl = file.listFiles();
    if (fl == null) {
    appendLn(strArr(file.toString(), “fl == null”), null, jta1, jta2, true);
    return false;
    }
    if (fl.length == 0) {
    appendLn(strArr(file.toString(), “fl.length == 0”), null, jta1, jta2, true);
    return false;
    }
    for (File f : fl) {
    suche1(f, unte, jta1, jta2);
    }
    }
    }
    } catch (Exception e) {
    appendLn(strArr(file.toString(), e.toString(), e.getMessage()), null, jta1, jta2, true);
    return false;
    }
    return true;
    }
    }


wie bewerkstellige ich das am besten?

Danke &Grüße

Besorge dir den ViewPort der JScrollPanes und füge denen einen ChangeListener hinzu. Über den wirst du über Änderungen informiert, die du dann dem jeweils anderen JScrollPane setzen kannst.

Danke für deine schnelle Antwort, @EikeB .