Problem 1: BY using defaulthighlighter, I can make the focused lines change to blue. Now I want to change it to other colors. Do anyone know how to change this parameter? --- solved
Problem 2: pos is the beginning index of my substring which I want to highlight. I use setCaretPosition(pos); to update the showing content. But it always appears at the bottom of the window. I want to have it at the top. Could anyone tell me how to deal with that?
I use one demo to show my problem:
import java.awt.Color;
import java.net.MalformedURLException;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
public class Test {
public static void main(final String[] args) throws MalformedURLException {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
init();
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
private static void init() throws BadLocationException {
JFrame frame = new JFrame();
final JTextArea textArea = new JTextArea();
JScrollPane pane = new JScrollPane(textArea);
textArea.setText("Something. Something else.\nA second line\na third line"
+ "Blabla\nBlabla\nBlabla\nBlabla\nBlabla\nBlabla\nBlabla\n"
+ "Blabla\nBlabla\nBlabla\nBlabla\nBlabla\nBlabla\nSomething. Samething else.\nA second line\na third line"
+ "Blabla\nBlabla\nBlabla\nBlabla\nBlabla\nBlabla\nBlabla\n"
+ "Blabla\nBlabla\nBlabla\nBlabla\nBlabla\nBlabla\nSomething. Sbmething else.\nA second line\na third line"
+ "Blabla\nBlabla\nBlabla\nBlabla\nBlabla\nBlabla\nBlabla\n"
+ "Blabla\nBlabla\nBlabla\nBlabla\nBlabla\nBlabla\nSomething. Scmething else.\nA second line\na third line"
+ "Blabla\nBlabla\nBlabla\nBlabla\nBlabla\nBlabla\nBlabla\n"
+ "Blabla\nBlabla\nBlabla\nBlabla\nBlabla\nBlabla\nSomething. Sdmething else.\nA second line\na third line"
+ "Blabla\nBlabla\nBlabla\nBlabla\nBlabla\nBlabla\nBlabla\n"
+ "Blabla\nBlabla\nBlabla\nBlabla\nBlabla\nBlabla\nSomething. Semething else.\nA second line\na third line"
+ "Blabla\nBlabla\nBlabla\nBlabla\nBlabla\nBlabla\nBlabla\n"
+ "Blabla\nBlabla\nBlabla\nBlabla\nBlabla\nBlabla\nSomething. Sfmething else.\nA second line\na third line"
+ "Blabla\nBlabla\nBlabla\nBlabla\nBlabla\nBlabla\nBlabla\n"
+ "Blabla\nBlabla\nBlabla\nBlabla\nBlabla\nBlabla\nSomething. Sgmething else.\nA second line\na third line"
+ "Blabla\nBlabla\nBlabla\nBlabla\nBlabla\nBlabla\nBlabla\n"
+ "Blabla\nBlabla\nBlabla\nBlabla\nBlabla\nBlabla\n");
textArea.setSelectionColor(Color.RED);
frame.add(pane);
frame.setSize(300, 120);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
String turnToString2 = "Sdmething else.\nA second line\na third line"
+ "Blabla\nBlabla\nBlabla\nBlabla\nBlabla";
int pos2 = textArea.getText().indexOf(turnToString2);
textArea.getHighlighter().addHighlight(pos2,
pos2 + turnToString2.length(),
new DefaultHighlighter.DefaultHighlightPainter(Color.yellow));
textArea.setCaretPosition(pos2);
The result is :
I want it to be at the right top of the screen but in this code, it is shown at the bottom of the scrollpane. Can anyone know how to change this? THanks.
To solve 2) Use modelToView to get point of the first selected row. Then use scrollRectToVisible using the Point (NOTE: height of the rectangle must be your viewport height).