Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / July 2005

Tip: Looking for answers? Try searching our database.

Block Selection

Thread view: 
ashuvashi - 13 Jun 2005 09:26 GMT
Hi friends,
I am very new to Java.. I've created a notepad kind of editor.. I want to
add a functionality of block selection using mouse.. Is any body who can
help me...?
It's really urgent.. please help me.. I am really thankfull to him...
Thanks,
Ashesh Vashi
leemingjun - 13 Jun 2005 10:57 GMT
The text support block selection by default,isn't is?
leemingjun - 13 Jun 2005 11:01 GMT
The text component support block selection by default,isn't is?
ashuvashi - 28 Jul 2005 05:59 GMT
Thanks for reply...
i got something for it.. that i would like to share with you all...
it could be useful for someone...
and one more thing text components support block selection by default that
is true but when you want to make editor kind of thing you need to have
something that helps you to rectangle selection which is not selection..

This is the code

you can use it but may be need to refine it a bit...
   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++;
     }
   }
}

Thanks,
With Regards,
Ashesh Vashi
Knute Johnson - 28 Jul 2005 22:05 GMT
> Thanks for reply...
> i got something for it.. that i would like to share with you all...
[quoted text clipped - 141 lines]
> With Regards,
> Ashesh Vashi

Clear the old data with Graphics.fillRect().

knute...

email s/nospam/knute/
Knute Johnson - 29 Jul 2005 01:03 GMT
Please disregard previous post.  It was supposed to go to another
message.  The mysteries of the internet.

Signature

Knute Johnson
email s/nospam/knute/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.