For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!
Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.
/* * Test_Find.java */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.text.*; public class Test_Find extends JFrame { public Test_Find() { initComponents(); setExtendedState(JFrame.MAXIMIZED_BOTH); textarea.setText(initialText); textarea.setCaretPosition(0); setVisible(true); textarea.requestFocusInWindow(); } private void initComponents() { scrollTextarea = new JScrollPane(); textarea = new JTextArea(); toolbar = new JToolBar(); findB = new JButton(); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setTitle("Test Find"); scrollTextarea.setViewportView(textarea); getContentPane().add(scrollTextarea, BorderLayout.CENTER); toolbar.setFloatable(false); findB.setText("Find"); findB.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { findBActionPerformed(evt); } }); toolbar.add(findB); getContentPane().add(toolbar, BorderLayout.NORTH); } private void findBActionPerformed(ActionEvent evt) { String input = JOptionPane.showInputDialog( "Find what:", pattern); if(input==null){ input=""; } pattern=input; doFind(); } private int doFind(){ textarea.requestFocusInWindow(); currentCaretPosition = textarea.getCaretPosition(); text = textarea.getText(); if(!pattern.equals("")){ int pos=0; pos = text.toLowerCase().indexOf(pattern.toLowerCase(), currentCaretPosition); if(pos>-1){ scrollTextarea.getHorizontalScrollBar().setValue(0); textarea.setCaretPosition(pos+pattern.length()); highlight(textarea, pattern); return 1;//found } }else{ removeHighlights(textarea); return 0;//no pattern } return -1;//not found } // Creates highlights around all occurrences of pattern in textComp public void highlight(JTextComponent textComp, String pattern) { // First remove all old highlights removeHighlights(textComp); try { Highlighter hilite = textComp.getHighlighter(); Document doc = textComp.getDocument(); String text = doc.getText(0, doc.getLength()); int pos = 0; // Search for pattern while ((pos = text.toLowerCase().indexOf(pattern.toLowerCase(), pos)) >= 0) { // Add highlight around pattern using private painter hilite.addHighlight(pos, pos+pattern.length(), textPainter); pos += pattern.length(); } } catch (BadLocationException e) { } } // Removes only our private highlights public void removeHighlights(JTextComponent textComp) { Highlighter hilite = textComp.getHighlighter(); Highlighter.Highlight[] hilites = hilite.getHighlights(); for (int i=0; i<hilites.length; i++) { if (hilites.getPainter() instanceof TextPainter) { hilite.removeHighlight(hilites[i]); } } } // An instance of the private subclass of the default highlight painter private Highlighter.HighlightPainter textPainter = new TextPainter(Color.yellow); // A private subclass of the default highlight painter class TextPainter extends DefaultHighlighter.DefaultHighlightPainter { public TextPainter(Color color) { super(color); } } public static void main(String args[]) { new Test_Find(); } private String input, pattern, text; private int currentCaretPosition; private JButton findB; private JScrollPane scrollTextarea; private JTextArea textarea; private JToolBar toolbar; private String initialText = "Swing JTable and JScrollPane Problem\n" + " tlloreti 4 Jul 5, 2005 4:15 PM\n" + "by Tom.Sanders \u00bb \n" + "Swing CTRL-A (select all) ignored in applet\n" + " ilmbeachgirl 4 Jul 5, 2005 4:10 PM\n" + "by ilmbeachgirl \u00bb \n" + "Swing drawing problem\n" + " gnosnahz 0 Jul 5, 2005 4:09 PM\n" + "by gnosnahz \u00bb \n" + "Java Event Handling events effecting panels in multiple tabs\n" + " sean_hagen 1 Jul 5, 2005 4:06 PM\n" + "by es5f2000 \u00bb \n" + "Swing Searching text in files & highlighting that text\n" + " sketty 1 Jul 5, 2005 3:57 PM\n" + "by camickr \u00bb \n" + "Swing JTextField displaying prtially its contents\n" + " SreeJay 1 Jul 5, 2005 3:55 PM\n" + "by camickr \u00bb \n" + "Swing how to get BufferedImage of a JPanel\n" + " praveen.Arora 1 Jul 5, 2005 3:53 PM\n" + "by camickr \u00bb \n" + "Swing Creating a form from a xml schema..\n" + " zoltern 0 Jul 5, 2005 3:51 PM\n" + "by zoltern \u00bb \n" + "Swing Da Suckers: Java and Windows XP\n" + " selamiski@sahinski.com 1 Jul 5, 2005 3:42 PM\n" + "by selamiski@sahinski.com \u00bb \n" + "Java 2D Rescaling using JAI\n" + " G-Rehuku 0 Jul 5, 2005 3:30 PM\n" + "by G-Rehuku \u00bb \n" + "Swing how to get Page Preview of multiple page JPanel\n" + " Sonu.Arora 0 Jul 5, 2005 2:20 PM\n" + "by Sonu.Arora \u00bb \n" + "Swing IDE for Swing Development\n" + " JavaEnthusiastt 8 Jul 5, 2005 2:17 PM\n" + "by uhrand \u00bb \n" + "Java 2D Can't connect to X11 window server\n" + " kusigubi 0 Jul 5, 2005 2:06 PM\n" + "by kusigubi \u00bb \n"; }
if (hilites.getPainter() instanceof TextPainter) {