title: How to change highlighting color in Java Swing TextArea? And also, change the beginning of text corresponding to the highlighting location
body: <p>Problem 1: BY using defaulthighlighter, I can make the focused lines change to blue. <strong>Now I want to change it to other colors. Do anyone know how to change this parameter?</strong> --- solved</p>
  
  <p>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 <strong>always appears at the bottom of the window. I want to have it at the top.</strong> Could anyone tell me how to deal with that?</p>
  
  <p>I use one demo to show my problem:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>The result is :
  <img src="https://i.stack.imgur.com/p95cV.png" alt="enter image description here"></p>
  
  <p>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.</p>