Tuesday, May 25, 2010

Highlight text in JEditorpane of JOptionPane

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Document;
import javax.swing.text.Highlighter;
import javax.swing.text.JTextComponent;

/**
*
* @author Monirul Molla
*/
public class HighlightTextInJEditorpaneOfJOptionPane extends JFrame implements ActionListener{
Highlighter.HighlightPainter myHighlightPainter;
JFrame fr;
JPanel pnl;
JButton btn;
HighlightTextInJEditorpaneOfJOptionPane()
{
myHighlightPainter = new MyHighlightPainter(Color.YELLOW);
fr = new JFrame("Highlight Text");
fr.setLayout(null);
fr.setBounds(50,50,300,200);
fr.setMinimumSize(new Dimension(300,200));
pnl = new JPanel();
pnl.setBounds(0,0,300,200);
pnl.setLayout(null);
btn = new JButton("Click");
btn.setBounds(100,50,80, 25);
btn.addActionListener(this);
pnl.add(btn);
fr.add(pnl);
fr.setVisible(true);

}
public void highlight(JTextComponent textComp, String pattern) {
// First remove all old highlights
//removeHighlights(textComp);
pattern = pattern.toLowerCase();
try {
Highlighter hilite = textComp.getHighlighter();
Document doc = textComp.getDocument();
String text = doc.getText(0, doc.getLength());
text = text.toLowerCase();
int pos = 0;

// Search for pattern
while ((pos = text.indexOf(pattern, pos)) >= 0) {
// Create highlighter using private painter and apply around pattern
hilite.addHighlight(pos, pos+pattern.length(), myHighlightPainter);
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 if (hilites[i].getPainter() instanceof MyHighlightPainter) {
hilite.removeHighlight(hilites[i]);
}
}
}

class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter
{
public MyHighlightPainter(Color color) {
super(color);
}
}

public void actionPerformed(ActionEvent evt)
{
if(evt.getSource()==btn)
{
String stage ="";
String remarks = "";
Object[] message = new Object[5];
message[0] = "Please enter correct stage for this remarks.";
JLabel remarks_lbl = new JLabel("Remarks :");
JTextField remarks_txt = new JTextField();
JLabel stage_lbl = new JLabel("Stage :");
JTextField stage_txt = new JTextField();
String[] options = {"OK","CANCEL"};
JEditorPane ta = new JEditorPane();
ta.setSize(new Dimension(2000,1000));
//ta.setLineWrap(true);
// ta.setWrapStyleWord(true);
//ta.setRows(10);
// ta.setColumns(50);
ta.setText(" Insert some text after the 5th character\nMicrosoft Citizenship Partner of Microsoft Citizenship Partner of the Year Winner for 2009\nInsert some text after the 5th character\nInsert some text after the 5th character\nInsert some text after the 5th character\nEnumerating the Lines in a JTextArea Component");
highlight(ta,"the");
int vqc1 = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
int hqc1 = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
JScrollPane jsp = new JScrollPane(ta,vqc1,hqc1);
jsp.setPreferredSize(new Dimension(350, 300));

remarks_txt.setText("remarks");
stage_txt.setText("stage");
message[1] = remarks_lbl;
message[2] = remarks_txt;
message[3] = stage_lbl;
message[4] = jsp;

//For setting the curser position
/*
try
{
Document doc = ta.getDocument ();
Matcher matcher = Pattern.compile ("Molla").matcher (doc.getText (0, doc.getLength ()));
matcher.find ();
ta.setCaretPosition (matcher.start ());
ta.moveCaretPosition (matcher.end ());
}
catch(Exception e)
{

}
*/
Icon ic = new ImageIcon("D:/Desktop icon/CMFieldContainerIcon.GIF");//D:\Desktop icon\New Folder (3)\New Folder (3)\images10

int result = JOptionPane.showOptionDialog(
this, // the parent that the dialog blocks
message, // the dialog message array
"Confirmation", // the title of the dialog window
JOptionPane.DEFAULT_OPTION, // option type
JOptionPane.INFORMATION_MESSAGE, // message type
ic, // optional icon, use null to use the default icon
options, // options string array, will be made into buttons
options[0] // option that should be made into a default button
);
switch(result)
{
case 0: // yes
stage = stage_txt.getText();
break;
}
}

}

public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);
new HighlightTextInJEditorpaneOfJOptionPane();
}
});
}

}

No comments:

Post a Comment