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.
SECOND 



To set the selection background color, use setSelectionColor (illustrated below but not used).
I don't really understand what you're saying with it always appears at the bottom of the window. I want to have it at the top but I am guessing (and I may be wrong here) that your textarea is in a scrollpane and that by highlighting the text it scrolls to the end of your selection, so I suggest to set the caret position after highlighting the text.
Here is a sample of what I understood. Let me know if this is not what you're looking for: