残念ながら、 の選択モデルはありませんJTextArea
。そうでなければ、これは非常に簡単だったでしょう...
代わりに、各テキスト領域のに を追加するChangeListener
必要がありました。Caret
これにより、キャレットの位置がいつ変更されたかをリアルタイムで確認できました。
次の問題は、現在のテキスト領域のみが実際に選択ハイライトを表示することに気付いたときに発生しました....(nbこれは簡単に修正できます。次の更新を確認してください)
次に、フォーカスされていないテキスト領域に蛍光ペンを適用する必要がありました...
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.io.File;
import java.io.FileReader;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
public class CaretTest {
public static void main(String[] args) {
new CaretTest();
}
public CaretTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridLayout(1, 2));
JTextArea left = new JTextArea(10, 20);
JTextArea right = new JTextArea(10, 20);
left.setEditable(false);
right.setEditable(false);
left.getCaret().addChangeListener(new ChangeHandler(left, right));
right.getCaret().addChangeListener(new ChangeHandler(right, left));
left.addFocusListener(new FocusHandler(left, right));
right.addFocusListener(new FocusHandler(right, left));
JScrollPane leftSP = new JScrollPane(left);
JScrollPane rightSP = new JScrollPane(right);
leftSP.getHorizontalScrollBar().setModel(rightSP.getHorizontalScrollBar().getModel());
leftSP.getVerticalScrollBar().setModel(rightSP.getVerticalScrollBar().getModel());
add(leftSP);
add(rightSP);
FileReader reader = null;
try {
reader = new FileReader(new File("Ni.txt"));
left.read(reader, null);
reader.close();
reader = new FileReader(new File("Ni.txt"));
right.read(reader, null);
} catch (Exception exp) {
exp.printStackTrace();
} finally {
try {
reader.close();
} catch (Exception e) {
}
}
}
protected void updateHighlighting(JTextArea source, JTextArea target) {
DefaultHighlighter.DefaultHighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(target.getSelectionColor());
int start = source.getSelectionStart();
int end = source.getSelectionEnd();
try {
target.getHighlighter().addHighlight(start, end, painter);
} catch (BadLocationException ex) {
ex.printStackTrace();
}
}
public class ChangeHandler implements ChangeListener {
private final JTextArea source;
private final JTextArea target;
public ChangeHandler(JTextArea source, JTextArea target) {
this.source = source;
this.target = target;
}
@Override
public void stateChanged(ChangeEvent e) {
if (e.getSource() == source.getCaret()) {
target.getHighlighter().removeAllHighlights();
updateHighlighting(source, target);
}
}
}
public class FocusHandler extends FocusAdapter {
private final JTextArea source;
private final JTextArea target;
public FocusHandler(JTextArea source, JTextArea target) {
this.source = source;
this.target = target;
}
@Override
public void focusGained(FocusEvent e) {
source.getHighlighter().removeAllHighlights();
target.getHighlighter().removeAllHighlights();
updateHighlighting(source, target);
}
}
}
}
ps- 独自のテキストを提供する必要があります ;)
「非ハイライター」の例で更新
JTextComponent#getCaret()#setSelectionVisible(true)
フォーカスされていないテキスト コンポーネントに選択されたテキストを表示するために使用できることを指摘してくれた StanislavL に感謝します。
フォーカスの変更によりこれfalse
が再び発生したことがわかったので、常に変更ハンドラー内で更新しましたtrue
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.io.File;
import java.io.FileReader;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
public class CaretTest {
public static void main(String[] args) {
new CaretTest();
}
public CaretTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridLayout(1, 2));
JTextArea left = new JTextArea(10, 20);
JTextArea right = new JTextArea(10, 20);
left.setEditable(false);
right.setEditable(false);
left.getCaret().setSelectionVisible(true);
right.getCaret().setSelectionVisible(true);
left.getCaret().addChangeListener(new ChangeHandler(left, right));
right.getCaret().addChangeListener(new ChangeHandler(right, left));
JScrollPane leftSP = new JScrollPane(left);
JScrollPane rightSP = new JScrollPane(right);
leftSP.getHorizontalScrollBar().setModel(rightSP.getHorizontalScrollBar().getModel());
leftSP.getVerticalScrollBar().setModel(rightSP.getVerticalScrollBar().getModel());
add(leftSP);
add(rightSP);
FileReader reader = null;
try {
reader = new FileReader(new File("Ni.txt"));
left.read(reader, null);
reader.close();
reader = new FileReader(new File("Ni.txt"));
right.read(reader, null);
} catch (Exception exp) {
exp.printStackTrace();
} finally {
try {
reader.close();
} catch (Exception e) {
}
}
}
}
public static class ChangeHandler implements ChangeListener {
private static boolean ignoreUpdates = false;
private final JTextArea source;
private final JTextArea target;
public ChangeHandler(JTextArea source, JTextArea target) {
this.source = source;
this.target = target;
}
@Override
public void stateChanged(ChangeEvent e) {
if (e.getSource() == source.getCaret()) {
if (!ignoreUpdates) {
ignoreUpdates = true;
try {
target.getCaret().setSelectionVisible(true);
source.getCaret().setSelectionVisible(true);
target.setSelectionStart(source.getSelectionStart());
target.setSelectionEnd(source.getSelectionEnd());
} finally {
ignoreUpdates = false;
}
}
}
}
}
}