Re: rectangular selections in text componets.
- From: "ashuvashi" <asheshvashi@xxxxxxxxx>
- Date: Thu, 28 Jul 2005 00:55:40 -0400
hi,
This is quite possible.. i was also looking for this.. i got some code for
it.. Go through following code for it...some of the code i've taken from my
code so refine it..
private class NewEditorPane
extends javax.swing.JEditorPane {
public NewEditorPane() {
super();
setCaret(new Caret_());
}
public void copy() {
javax.swing.text.Highlighter.Highlight[] selections =
jTemplateEditorPane.getHighlighter().getHighlights();
String text = "";
int cnt = selections.length;
if (cnt != 0) {
for (int i = 0; i < cnt; i++) {
int start = selections[i].getStartOffset();
int end = selections[i].getEndOffset();
try {
String selectedText = jTemplateEditorPane.getDocument().
getText(start,
end - start);
text += selectedText + '\n';
}
catch (javax.swing.text.BadLocationException bex) {
}
}
java.awt.datatransfer.StringSelection ss = new java.awt.
datatransfer.
StringSelection(text);
java.awt.Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
ss, ss);
}
else {
super.copy();
}
}
}
private class Caret_
extends javax.swing.text.DefaultCaret {
public Caret_() {
setBlinkRate(500);
this.addChangeListener(new StateChangeAdapter());
}
java.awt.Point lastPoint = new java.awt.Point(0, 0);
public void mouseMoved(java.awt.event.MouseEvent e) {
super.mouseMoved(e);
lastPoint = new java.awt.Point(e.getX(), e.getY());
}
public void mouseClicked(java.awt.event.MouseEvent e) {
super.mouseClicked(e);
getComponent().getHighlighter().removeAllHighlights();
}
public void mouseReleased(java.awt.event.MouseEvent me) {
javax.swing.text.Highlighter.Highlight[] selections =
jTemplateEditorPane.getHighlighter().getHighlights();
if (selections.length > 0) {
generateDataGathereEvent();
}
}
protected void moveCaret(java.awt.event.MouseEvent e) {
java.awt.Point pt = new java.awt.Point(e.getX(), e.getY());
javax.swing.text.Position.Bias[] biasRet = new javax.swing.text.
Position.Bias[1];
int pos = getComponent().getUI().viewToModel(getComponent(), pt,
biasRet);
if (biasRet[0] == null) {
biasRet[0] = javax.swing.text.Position.Bias.Forward;
}
if (pos >= 0) {
setDot(pos);
java.awt.Point start = new java.awt.Point(Math.min(lastPoint.
x, pt.x),
Math.min(lastPoint.y,
pt.y));
java.awt.Point end = new java.awt.Point(Math.max(lastPoint.x,
pt.x),
Math.max(lastPoint.y,
pt.y));
customHighlight(start, end);
}
}
protected void customHighlight(java.awt.Point start,
java.awt.Point end) {
getComponent().getHighlighter().removeAllHighlights();
int y = start.y;
int firstX = start.x;
int lastX = end.x;
int pos1 = getComponent().getUI().viewToModel(getComponent(),
new java.awt.Point(firstX, y));
int pos2 = getComponent().getUI().viewToModel(getComponent(),
new java.awt.Point(lastX, y));
try {
getComponent().getHighlighter().addHighlight(pos1, pos2,
( (javax.swing.text.DefaultHighlighter) getComponent().
getHighlighter()).
DefaultPainter);
}
catch (Exception ex) {
ex.printStackTrace();
}
y++;
while (y < end.y) {
int pos1new = getComponent().getUI().viewToModel(getComponent(),
new java.awt.Point(firstX, y));
int pos2new = getComponent().getUI().viewToModel(getComponent(),
new java.awt.Point(lastX, y));
if (pos1 != pos1new) {
pos1 = pos1new;
pos2 = pos2new;
try {
getComponent().getHighlighter().addHighlight(pos1,
pos2,
( (javax.swing.text.DefaultHighlighter)
getComponent().getHighlighter()).
DefaultPainter);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
y++;
}
}
}
With Regards,
Ashesh Vashi
.
- Prev by Date: Re: Fading window problem
- Next by Date: Re: Block Selection
- Previous by thread: 3 state listbox?
- Next by thread: Re: Block Selection
- Index(es):
Relevant Pages
|