FileDocCategorySizeDatePackage
MultiHighlight.javaAPI DocExample1506Thu Oct 24 20:14:26 BST 2002None

MultiHighlight

public class MultiHighlight extends Object implements ActionListener

Fields Summary
private JTextComponent
comp
private String
charsToHighlight
Constructors Summary
public MultiHighlight(JTextComponent c, String chars)

    comp = c;
    charsToHighlight = chars;
  
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent e)

    // highlight all characters that appear in charsToHighlight
    Highlighter h = comp.getHighlighter();
    h.removeAllHighlights();
    String text = comp.getText().toUpperCase();

    for (int j=0; j < text.length(); j+=1) {
      char ch = text.charAt(j);
      if (charsToHighlight.indexOf(ch) >= 0) try {
        h.addHighlight(j, j+1, DefaultHighlighter.DefaultPainter);
      } catch (BadLocationException ble) { }
    }
  
public static voidmain(java.lang.String[] args)

    JFrame frame = new JFrame("MultiHighlight");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTextArea area = new JTextArea(5, 20);
    area.setText("This is the story\nof the hare who\nlost his spectacles.");
    frame.getContentPane().add(new JScrollPane(area), BorderLayout.CENTER);

    JButton b = new JButton("Highlight All Vowels");
    b.addActionListener(new MultiHighlight(area, "aeiouAEIOU"));
    frame.getContentPane().add(b, BorderLayout.SOUTH);
    frame.pack();
    frame.setVisible(true);