mas não funicona direito,
que estou fazendo errado?
package swingtests;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
public class CompilerEditor {
private DefaultStyledDocument doc = null;
private StyleContext sc = null;
private Style defautStyle = null;
private Style mainStyle = null;
private JTextPane texto;
public CompilerEditor(JTextPane texto) {
sc = new StyleContext();
doc = new DefaultStyledDocument(sc);
texto.setStyledDocument(doc);
defautStyle = sc.getStyle(StyleContext.DEFAULT_STYLE);
mainStyle = sc.addStyle("MainStyle", defautStyle);
StyleConstants.setBold(mainStyle, true);
StyleConstants.setForeground(mainStyle, Color.BLUE);
this.texto = texto;
}
public void executa(ArrayList<String> palavras) {
processaTexto(analisaTexto(palavras));
}
private void processaTexto(ArrayList<Point> points) {
System.out.println(points);
doc.setLogicalStyle(0, mainStyle);
for (Point p : points) {
doc.setCharacterAttributes(p.x, p.y, mainStyle, false);
}
}
private ArrayList<Point> analisaTexto(ArrayList<String> palavras) {
ArrayList<Point> onde = new ArrayList<Point>();
int pinicial = -1;
int pfinal = -1;
for (String s : palavras) {
String substr = texto.getText();
do {
pinicial = substr.indexOf(s);
System.out.println(substr);
if (pinicial != -1) {
pfinal = pinicial + s.length();
substr = substr.substring(pfinal);
onde.add(new Point(pinicial, s.length()));
pfinal = -1;
}
} while (pinicial != -1);
}
return onde;
}
}
class RunCompiler {
public static void main(String[] args) {
final JTextPane text = new JTextPane();
final ArrayList<String> words = new ArrayList<String>();
final CompilerEditor compiler = new CompilerEditor(text);
words.add("case");
words.add("if");
words.add("while");
try {
text
.setPage("http://www.cs.unc.edu/~huan/comp14/TelephoneDigitProgram.java");
} catch (Exception e) {
e.printStackTrace();
}
JFrame f = new JFrame();
JScrollPane scroll = new JScrollPane(text);
f.setLayout(new BorderLayout());
f.setSize(400, 400);
JButton button = new JButton("Processa");
f.add(scroll, "Center");
f.add(button, "South");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
compiler.executa(words);
System.out.println("dentro do evento do button");
}
});
}
}