title: changing the color of a setText() method using a JTextPane
body: <p>I have tried alot of different things yet i can't seem to figure out how to do it. I have read about 40 pages on how to set the color of ALL the text in the pane, but not one specific bit. I have some source code that is an SSCCEE(whatever it is called) but it does not have what i have tried, it just shows what i want to have the color changed.</p>
  
  <p>In the <code>tf.addActioListener()</code> there is a bit that has </p>
  
  <pre><code>else {
  tf.setText("&gt; ");
  tp.setText(tp.getText() + "&gt; ERROR: Invalid Command" + newline);
  try {
  FileWriter fw = new FileWriter(file);
  fw.write(tp.getText());
  fw.close();
  } catch (IOException ioe) {}
  }
  </code></pre>
  
  <p>I want the color of "> ERROR: Invalid Command" to be changed to red.</p>
  
  <p>P.S. I forgot to mention this is for the Text-Based game i am making. Thanks in advance, Braydon</p>
  
  <p>CODE:</p>
  
  <pre><code>package net.GUI.frame;
  
  import java.awt.event.*;
  import java.awt.*;
  import java.io.*;
  import javax.swing.*;
  
  public class Frame {
      public static void main(String[] args) {
          JFrame frame = new JFrame();
          final JTextPane tp = new JTextPane();
          final JTextField tf = new JTextField();
          JScrollPane sp = new JScrollPane();
          JPanel panel = new JPanel();
          final File file = new File("log.txt");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);
          frame.setSize(350,410);
          tp.setEditable(false);
          tp.setForeground(Color.blue);
          tf.setText("&gt; ");
          sp.setViewportView(tp);
          GroupLayout jPanel2Layout = new javax.swing.GroupLayout(panel);
          panel.setLayout(jPanel2Layout);
          jPanel2Layout.setHorizontalGroup(
                  jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                 .addComponent(tf)
                  .addComponent(sp, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
              );
          jPanel2Layout.setVerticalGroup(
                  jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                      .addComponent(sp, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE)
                      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                      .addComponent(tf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
              );
          GroupLayout layout = new javax.swing.GroupLayout(frame.getContentPane());
          frame.getContentPane().setLayout(layout);
          layout.setHorizontalGroup(
                  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addGroup(layout.createSequentialGroup()
                      .addContainerGap()
                      .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                      .addContainerGap())
              );
          layout.setVerticalGroup(
                  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addGroup(layout.createSequentialGroup()
                      .addContainerGap()
                      .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                      .addContainerGap())
          );
          frame.addWindowListener( new WindowAdapter() {
              public void windowOpened( WindowEvent e ){
                  tf.requestFocus();
              }
          });
          /**File file10 = new File("Rooms/Room1/roomDescription.txt");
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          PrintStream ps = new PrintStream(baos);
          PrintStream old = System.out;
          System.setOut(ps);
  
          try {
              FileReader fr = new FileReader(file10);
              int ch = fr.read();
  
              while(ch != -1) {
                  System.out.print((char) ch);
                  ch = fr.read();
              }
          } catch (Exception ex) {
  
          }
          System.out.flush();
          System.setOut(old);
          tp.setText(baos.toString());
          try {
              FileWriter fw = new FileWriter(file);
              fw.write(tp.getText());
              fw.close();
          } catch (IOException ioe) {
  
          }*/
  
          tp.setText("You find yourself in a blank room with a cot and a small stool. The walls are made of cold " +
                  "grey concrete and have been sanded smooth. There is a solid iron door set into one of the walls." +
                  " There are no windows." +
                  "\nWhat do you want to do?" +
                  "\n" +
                  "\n");
  
          tf.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  String newline = "\n";
                  if(tf.getText().equals("&gt; inspect cot")) {  
                      String string = tf.getText();
                      tp.setText(tp.getText()+string + newline);
                      tf.setText("&gt; ");
                      File file1 = new File("Rooms/Room1/inspectCot.txt");
                      ByteArrayOutputStream baos = new ByteArrayOutputStream();
                      PrintStream ps = new PrintStream(baos);
                      PrintStream old = System.out;
                      System.setOut(ps);
  
                      try {
                          FileReader fr = new FileReader(file1);
                          int ch = fr.read();
  
                          while(ch != -1) {
                              System.out.print((char) ch);
                              ch = fr.read();
                          }
                      } catch (Exception ex) {
  
                      }
  
                      System.out.flush();
                      System.setOut(old);
                      tp.setText(tp.getText() + (baos.toString()));
                      try {
                          FileWriter fw = new FileWriter(file);
                          fw.write(tp.getText());
                          fw.close();
                      } catch (IOException ioe) {
  
                      }
                  } else {
                      tf.setText("&gt; ");
                      tp.setText(tp.getText() + "&gt; ERROR: Invalid Command" + newline);
                      try {
                          FileWriter fw = new FileWriter(file);
                          fw.write(tp.getText());
                          fw.close();
                      } catch (IOException ioe) {
  
                      }
                  }
              }
          });
      }
  }
  </code></pre>
  
  <p>I have already made the game using JTextArea but would like to make it a bit more aesthetically pleasing, if you know what i mean?</p>
  
  <p>here is a link to the original game: <a href="http://www.mediafire.com/?vp8baa2u03aafob" rel="nofollow">http://www.mediafire.com/?vp8baa2u03aafob</a></p>