You might try something like this ... wrote this some time ago:
/**
arg is what you are looking for
*/
private void parseText(String arg) throws IllegalArgumentException {
if (arg == null || arg.length() == 0 ) {
throw new IllegalArgumentException("Bad argument in parseText: "+arg);
}
intpointer = 0,
argLen= arg.length();
String data= getDisplayContent(); // method gets the content of your TextArea
if ( ipopdisp.getCaretPosition() < pointer ) { // ipopdisp is the TextArea Object
pointer = ipopdisp.getCaretPosition();
}
if ( data != null && data.length() > 0 && !data.equals(displayContent) ) {
displayContent = data; // defined at the class level
parseBeg = 0; // ditto
parseEnd = 0; // ditto
}
if ( isCaseSensitive ) { // at the class level
parseBeg = displayContent.indexOf(arg, parseEnd);
}
else {
parseBeg = displayContent.toUpperCase().indexOf(arg.toUpperCase(), parseEnd);
}
parseEnd = parseBeg+argLen;
if ( parseBeg == -1 ) {
parseEnd = 0;
if ( isCaseSensitive ) {
parseBeg = displayContent.indexOf(arg, parseEnd);
}
else {
parseBeg = displayContent.toUpperCase().indexOf(arg.toUpperCase(), parseEnd);
}
parseEnd = parseBeg+argLen;
if ( parseBeg == -1 ) {
parseBeg = 0;
parseEnd = 0;
}
}
char[] displayArray = displayContent.toCharArray();
for ( int a = 0; a < parseBeg; a++ ) {
String s = ""+displayArray[a];
if ( !s.equals(EOLS[2]) ) { // That's '\r'
pointer++;
}
}
ipopdisp.setCaretPosition(pointer);
ipopdisp.requestFocus();
}
caveat emptor
how can i add that to this
does it need more imports?
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import java.awt.TextArea;
import java.util.*;
public class Editor extends JApplet implements ActionListener {
JPanel panel;
JFileChooser fc;
//hight width
TextArea field = new TextArea("test", 30, 50);
JComboBox combo;
private JPanel pnlNorth = new JPanel(); // North quadrant
private JPanel pnlSouth = new JPanel(); // South quadrant
private JPanel pnlEast = new JPanel(); // East quadrant
private JPanel pnlWest = new JPanel(); // West quadrant
private JPanel pnlCenter = new JPanel(); // Center quadrant
private JButton saveButton = new JButton("Save");
private JButton openButton = new JButton("Load");
private JButton btnEast = new JButton("Head");
private JButton btnWest = new JButton("Body");
private JButton btnCenter = new JButton("somthing");
private JMenuBar mb = new JMenuBar(); // Menubar
private JMenu mnuFile = new JMenu("File"); // File Entry on Menu bar
private JMenuItem mnuItemQuit = new JMenuItem("Quit"); // Quit sub item
private JMenu mnuHelp = new JMenu("Help"); // Help Menu entry
private JMenuItem mnuItemAbout = new JMenuItem("About"); // About Entry
public void actionPerformed(ActionEvent e) {
// Handle open button action.
if (e.getSource() == btnEast) {
String findStr = "<HEAD>";
String txt = field.getText();
int index = txt.indexOf("<HEAD>");
System.out.println(index);
}
else if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(Editor.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
field.setText("");
File file = fc.getSelectedFile();
try {
BufferedReader in = new BufferedReader(new FileReader(file));
String data;
while ((data = in.readLine()) != null) {
field.append(data + '\n');
}
} catch (IOException _ex) {
System.out.println("Error " + _ex);
_ex.printStackTrace();
}
// This is where a real application would open the file.
// log.append("Opening: " + file.getName() + "." + newline);
}// else {
// log.append("Open command cancelled by user." + newline);
// }
// log.setCaretPosition(log.getDocument().getLength());
// Handle save button action.
}/*
* else if (e.getSource() == saveButton) { int returnVal = fc.showSaveDialog(Editor.this); if (returnVal ==
* JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); //This is where a real application would
* save the file. // log.append("Saving: " + file.getName() + "." + newline); } else { //log.append("Save
* command cancelled by user." + newline); } //log.setCaretPosition(log.getDocument().getLength()); }
*/
}
@Override
public void start() {
// TODO Auto-generated method stub
super.start();
}
@Override
public void stop() {
// TODO Auto-generated method stub
super.stop();
}
@Override
public void update(Graphics g) {
// TODO Auto-generated method stub
super.update(g);
}
public void init() {
fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
panel = new JPanel();
this.setJMenuBar(mb);
mnuFile.add(mnuItemQuit);
mnuHelp.add(mnuItemAbout);
mb.add(mnuFile);
mb.add(mnuHelp);
pnlNorth.add(saveButton);
pnlSouth.add(openButton);
pnlEast.add(btnEast);
pnlWest.add(btnWest);
pnlCenter.add(btnCenter);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(pnlNorth, BorderLayout.NORTH);
this.getContentPane().add(pnlSouth, BorderLayout.SOUTH);
this.getContentPane().add(pnlEast, BorderLayout.EAST);
this.getContentPane().add(pnlWest, BorderLayout.WEST);
openButton.addActionListener(this);// load
saveButton.addActionListener(this);// save
btnEast.addActionListener(this);//head
panel.add(field);
this.add(panel);
this.setSize(300, 300);
this.setVisible(true);
}
}
Take a look at this code
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class Editor extends JApplet implements ActionListener {
JPanel panel;
JFileChooser fc;
// hight width
TextArea field = new TextArea("test", 30, 50);
JComboBox combo;
private JPanel pnlNorth = new JPanel(); // North quadrant
private JPanel pnlSouth = new JPanel(); // South quadrant
private JPanel pnlEast = new JPanel(); // East quadrant
private JPanel pnlWest = new JPanel(); // West quadrant
private JPanel pnlCenter = new JPanel(); // Center quadrant
private JButton saveButton = new JButton("Save");
private JButton openButton = new JButton("Load");
private JButton btnEast = new JButton("Head");
private JButton btnWest = new JButton("Body");
private JButton btnCenter = new JButton("somthing");
private JMenuBar mb = new JMenuBar(); // Menubar
private JMenu mnuFile = new JMenu("File"); // File Entry on Menu bar
private JMenuItem mnuItemQuit = new JMenuItem("Quit"); // Quit sub item
private JMenu mnuHelp = new JMenu("Help"); // Help Menu entry
private JMenuItem mnuItemAbout = new JMenuItem("About"); // About Entry
public void actionPerformed(ActionEvent e) {
// Handle open button action.
if (e.getSource() == btnEast) {
String findStr = "<HEAD>";
String txt = field.getText().toUpperCase();
int index = txt.indexOf(findStr);
if (index >= 0) {
field.select(index, index + findStr.length());// ////////CODE ADDED HERE
field.requestFocus();// //////////CODE ADDED HERE
}
System.out.println(index);
}
if (e.getSource() == btnWest) {
String findStr = "<BODY>";
String txt = field.getText().toUpperCase();
int index = txt.indexOf(findStr);
if (index >= 0) {
field.select(index, index + findStr.length());// ////////CODE ADDED HERE
field.requestFocus();// //////////CODE ADDED HERE
}
System.out.println(index);
} else if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(Editor.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
field.setText("");
File file = fc.getSelectedFile();
try {
BufferedReader in = new BufferedReader(new FileReader(file));
String data;
while ((data = in.readLine()) != null) {
field.append(data + '\n');
}
} catch (IOException _ex) {
System.out.println("Error " + _ex);
_ex.printStackTrace();
}
// This is where a real application would open the file.
// log.append("Opening: " + file.getName() + "." + newline);
}// else {
// log.append("Open command cancelled by user." + newline);
// }
// log.setCaretPosition(log.getDocument().getLength());
// Handle save button action.
}/*
* else if (e.getSource() == saveButton) { int returnVal = fc.showSaveDialog(Editor.this); if (returnVal ==
* JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); //This is where a real application would
* save the file. // log.append("Saving: " + file.getName() + "." + newline); } else { //log.append("Save
* command cancelled by user." + newline); } //log.setCaretPosition(log.getDocument().getLength()); }
*/
}
@Override
public void start() {
// TODO Auto-generated method stub
super.start();
}
@Override
public void stop() {
// TODO Auto-generated method stub
super.stop();
}
@Override
public void update(Graphics g) {
// TODO Auto-generated method stub
super.update(g);
}
public void init() {
fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
panel = new JPanel();
this.setJMenuBar(mb);
mnuFile.add(mnuItemQuit);
mnuHelp.add(mnuItemAbout);
mb.add(mnuFile);
mb.add(mnuHelp);
pnlNorth.add(saveButton);
pnlSouth.add(openButton);
pnlEast.add(btnEast);
pnlWest.add(btnWest);
pnlCenter.add(btnCenter);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(pnlNorth, BorderLayout.NORTH);
this.getContentPane().add(pnlSouth, BorderLayout.SOUTH);
this.getContentPane().add(pnlEast, BorderLayout.EAST);
this.getContentPane().add(pnlWest, BorderLayout.WEST);
openButton.addActionListener(this);// load
saveButton.addActionListener(this);// save
btnEast.addActionListener(this);// head
btnWest.addActionListener(this);//body ////////////////CODE ADDED HERE
panel.add(field);
this.add(panel);
this.setSize(300, 300);
this.setVisible(true);
}
}