简体   繁体   English

有没有一种方法可以让多个jTextArea在Java-Netbeans中共享滚动窗格?

[英]Is there a way that multiple jTextArea can share scrollpane in Java- Netbeans?

Is there a way that a scrollpane from another jTextArea can control another jTextArea to scroll upwards and downwards? 有没有一种方法可以使另一个jTextArea的滚动窗格控制另一个jTextArea向上和向下滚动?

在此处输入图片说明

They may not be the same in width but they will have the same lines of rows. 它们的宽度可能不相同,但是将具有相同的行数。 If one jTextArea has more lines than the other then empty lines will be added to the second jTextArea to match them. 如果一个jTextArea的行多于另一行,则将空行添加到第二个jTextArea以匹配它们。

Assuming jScrollPane1 and jScrollPane2 are the 2 scrollpanes of your 2 jTextareas respectievley, with below code you can implement your requirements. 假设jScrollPane1jScrollPane2是2个jTextareas的2个滚动窗格,使用以下代码可以实现您的要求。

jScrollPane1 = new JScrollPane();
jTextArea1 = new JTextArea();
jScrollPane2 = new JScrollPane();
jTextArea2 = new JTextArea();

jScrollPane1.setViewportView(jTextArea1);
jScrollPane2.setViewportView(jTextArea2);
.
.
.
.

JScrollBar verticalScrollBar1 = jScrollPane1.getVerticalScrollBar();

verticalScrollBar1.addAdjustmentListener(new AdjustmentListener() {

     @Override
     public void adjustmentValueChanged(AdjustmentEvent e) {
            JScrollBar vertical = jScrollPane2.getVerticalScrollBar();
            vertical.setValue(e.getValue());
     }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM