Results 1 to 19 of 19
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default what is better to do?

    I have a reference of the JTextPane (textpane) at most classes of my Text writer application.
    Now, when ever I need to get hold of the caret I just attach the caret listener to that ref like this:

    Java Code:
     textpane.addCaretListener(new CaretListener() {
                public void caretUpdate(CaretEvent e) {
                    int dot = e.getDot();
                    int mark = e.getMark();
                }
          });
    Or should I have that listener only once and pass the dot variable around as a result?

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,021
    Rep Power
    6

    Default Re: what is better to do?

    "and pass the dot variable around as a result? "

    Sorry that makes my brain go in a stackoverflow. What does that mean? Post a code example of what you would do if you were to go down this path.

    Note that if I were to make assumptions I would advise you to create unique listeners in stead of reusing the same one everywhere, but I can't say that for sure until I know what your other idea exactly is.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: what is better to do?

    First it is about the class that sets the text canvas a JTextPane:

    Java Code:
    class TextCanvas extends JPanel{
        private JTextPane textpane;
        private String text;
        private Caret caret;
    
        public TextCanvas() {
            StyledDocument doc = new DefaultStyledDocument();
            StyledEditorKit styleEditKit = new StyledEditorKit();
            textpane = new JTextPane();
            textpane.setDocument(doc);
            textpane.setEditorKit(styleEditKit);
            
            JScrollPane paneScrollPane = new JScrollPane(textpane);
            paneScrollPane.setVerticalScrollBarPolicy(
                            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
            paneScrollPane.setHorizontalScrollBarPolicy(
            				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            paneScrollPane.setPreferredSize(new Dimension(780, 830));
    
            this.add(paneScrollPane);
     . . . . . etc.
    Then there are several classes that build the menus like file/ edit and the font menus.
    The menu actions etc. change e.g. the font family size etc of the textcanvas above.
    Here find an example of a stored size action of the size font menu item which is a menu radio button:

    Java Code:
    actionmap.put("font-size-" + fsize,
                        new StyledEditorKit.FontSizeAction("font-size-" + 
                                                fsize, Integer.parseInt(fsize)));
               
                Action action = actionmap.get("font-size-" + fsize);
                action.putValue(Action.NAME, fsize);
    
                rbSize = new JRadioButtonMenuItem(action);
    But now I realize that I need to change the selected font items according to what the caret of the
    text-pane points to (what is under the caret, right). So I need the caret offset which is the dot
    (another complication is that there might be a text-selection than: dot != mark). So I get the caret
    like this (and remember we are at the font menu class not at the above TextCanvas class):

    Java Code:
     textpane.addCaretListener(new CaretListener() {
                public void caretUpdate(CaretEvent e) {
                     int dot = e.getDot();
                     int mark = e.getMark();
    	         fontindex = dot;
                }
          });

    Once I have the offset, the fontindex, I have to find out what font selections are on that caret-text
    (size/ family/ style and color), which I get like this (remember I have the reference of the JTexPane which is "textpane"):


    Java Code:
    StyledDocument doc = textpane.getStyledDocument();
                    AttributeSet a = doc.getCharacterElement(fontindex).getAttributes();
                    Enumeration enumx = a.getAttributeNames();
                    System.out.println();
                    while (enumx.hasMoreElements()) {
                        Object o = enumx.nextElement();
                        String ename = o.toString();
                        String value = a.getAttribute(o).toString();
                        
                        if (ename.equals("family")) {
                            System.out.println("family = " + value);
                           
                        }
    
                        if (ename.equals("size")) {
                            System.out.println("size = " + value);
                             
                        }
    
                        if (ename.equals("foreground")) {
                            System.out.println("color = " + value);
                            
                        }
    
                        if (ename.equals("bold")) {
                            System.out.println("bold = " + value);
                            
                        }
    
                        if (ename.equals("italic")) {
                            System.out.println("italic = " + value);
                             
                        }
    
                        if (ename.equals("underline")) {
                            System.out.println("underline = " + value);
                             
                        }
                    }
    Last edited by willemjav; 03-07-2014 at 10:02 AM.

  4. #4
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: what is better to do?

    Note the variable "fontindex" and with each if statement I will ajust the menus/ buttons and comboxes according the
    the font under the caret!

    But there is yet another issue: it does not make since to change all selection when the caret jumps around, right?
    So I will be using yet another listener the MenuListener and its menuSelected method. Because I only will
    Set all menus etc. when the user tens to enters one!

    And that's where I am at right now.....

  5. #5
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: what is better to do?

    (you're not mad now gimbal2 are you?)
    All this coding is very important to me because I need to know the font attributes of
    family, size and style at each character to calculate the size of each character
    in order to construct a page view of the text.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,021
    Rep Power
    6

    Default Re: what is better to do?

    I'm mad, but I'm not angry ;) What makes you say that anyway?

    In any case you are not answering my question; I did not ask you to post a wall of code you already have, I asked for an example of what you were thinking to do.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: what is better to do?

    Oh, man I might stick to playing the piano....
    I thought that, that wall-of-code is what explains my intention.....

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,021
    Rep Power
    6

    Default Re: what is better to do?

    Then it must be my own failure, too much information for me to process at this point in time (sometimes it may not seem like it, but I also have to do some work...).
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: what is better to do?

    ok, I understand, anyway things are progressing (not sure in what direction though)....
    Last edited by willemjav; 03-07-2014 at 11:09 AM.

  10. #10
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: what is better to do?

    But now I realize that I need to change the selected font items according to what the caret of the
    text-pane points to (what is under the caret, right)
    The editor kit Actions do this by default for you. If you have selected text then the attribute will be applied to that text. If no text is selected then the attribute is applied at the current Caret position and then when you start typing it will inherit that attribute. Look at the examples from the Swing tutorial on: Text Component Features (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    when ever I need to get hold of the caret I just attach the caret listener to that ref like this
    Makes no sense to me. If you think you need to get the Caret position in 5 places then you will have added 5 listeners to the Caret. The only time I can think of that you might need a Caret position is if you have a status bar that displays the row/column position of the Caret. If you want to know the Caret position when you click a button (because you are creating a page view), then you just use the getCaretPosition() method of the text pane.

    I still don't understand your PageView concept. I'm not sure what you are trying to do. But you could also just create a second JTextPane that shares the Document with the original text pane. Then you can have a second view of that Document.
    Last edited by camickr; 03-07-2014 at 04:51 PM.

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,610
    Rep Power
    5

    Default Re: what is better to do?

    Quote Originally Posted by gimbal2 View Post
    What makes you say that anyway?
    Because you're such a meany. Every time I see one of your posts I tremble. :)

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,021
    Rep Power
    6

    Default Re: what is better to do?

    Muhuahahahahaaaaaa!

    Nooooo, everyone should have a friend like me. On that note I wish you all an excellent weekend friends!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: what is better to do?

    Camickr with page view i tend to say
    1) drawing a rectangle with the size of a single page
    2) drawing the text into it using a g-mode by using the actual font size and style

    I would calculate each character width: if (sumofcharwidth > pagewidth) new line
    Than get the max charheight of each line and
    If (sumoflineheight > pageheight) new page

    With each (caret) offset I can get the style attributes of each character in order to get
    The font family size and style of that character to measure width and height.....
    I know that this is probably not the way to do it, but this is how I understand it for now!
    Last edited by willemjav; 03-08-2014 at 01:32 PM.

  14. #14
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: what is better to do?

    There is no need to use a CaretListener to get the attributes of every character. All you need is an offset. So I still don't underatand what you are doing.

    What is the difference between a "page mdoe" and the original text pane containing the text?

    Displaying and formatting styled text is extremely complicated. For you to attempt to do in on your own will be extremely difficult. For example your approach won't take into account tab characters. I still don't see any reason why you can't just use a second text pane. All you need to do is set the size of the text pane to represent a page of data. Then you scroll the data to a particular offset represents a single page of data and the text pane will do all the rendering for you.

    Swing components are designed on a Model/View approach. You should take time to understand what this means. If all you want is a "page view" then there should be no need for you to reinvent the wheel. Just use another text pane to create a new view.

  15. #15
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: what is better to do?

    I need the listener to adjust the menus of style, size, color and font family according what I have under the cursor. That is no big deal see please the last code block of post 3!

    Please imagine the window presentation of the application "OpenOffice" (which I believe is written Java): that is what I like to do and what about printing the text? I want to achieve a "what you see is what you get" condition.

    It is indeed complicated and I am looking into the classes linemetric and font layout......

  16. #16
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: what is better to do?

    I need the listener to adjust the menus of style, size, color and font family according what I have under the cursor.
    Yes that makes sense. However, in your original question you stated:

    ...Now, when ever I need to get hold of the caret I just attach the caret listener to that ref like this:
    Which sounded like you added multiple CaretListeners to the text pane, which is not necessary.

    what about printing the text?
    I'm don't know anything about printing, but I would think that is the size of the page is the same size as the text pane then it will print what you see. You should not need to write code to attempt to mimic what the text pane does. I think that is the wrong approach. As I say it is reinventing the when. I would think you should concentrate on specifrying the proper page size, getting the proper offset to print from and so on, but I can't offer any direct advice.

  17. #17
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: what is better to do?

    I do not think so because the screen res is not the one of the printer?

  18. #18
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: what is better to do?

    Here is the method that sets the font under the caret. The index is the character offset which
    might come from the caret. It further needs the doc as a StyledDocument, and it returns a font object:

    Java Code:
    public Font setFontonIndex(StyledDocument doc, int index) {
                Font font = null;
                AttributeSet a = doc.getCharacterElement(index).getAttributes();
                Enumeration enumx = a.getAttributeNames();
                Object o;
                String name;
                String value;
                String family = "";
                int size = 0;
                int style = Font.PLAIN;
                System.out.print("index: " + index + " ");
    
                while (enumx.hasMoreElements()) {
                    o = enumx.nextElement();
                    name = o.toString();
                    value = a.getAttribute(o).toString();
    
    		  if (name.equals("family")){
    		  	family = (String) value;
                            System.out.print("family: <" + family + "> ");
    		  }
    
                      if (name.equals("size")){
    		  	size = Integer.parseInt(value);
                            System.out.print("size: <" + size + "> ");
    		  }
    
    
    		  if (name.equals("bold")){
                            Boolean b = Boolean.parseBoolean(value);
                            style = Font.BOLD + style;
                            System.out.print("bold: <" + b + ">/ ");
    		  }
    
    		  if (name.equals("italic")){
                            Boolean b = Boolean.parseBoolean(value);
                            style = Font.ITALIC + style;
                            System.out.print("italic: <" + b + ">/ ");
    		  }
                      if (name.equals("underline")){
                            Boolean b = Boolean.parseBoolean(value);
                            System.out.print("underline: <" + b + ">/ ");
    		  }
    
                      if (name.equals("foreground")) {
                            System.out.print("color: <" + getColorString(value)
                                                                        + "> ");
    		  }
    
    		  font = new Font(family, style, size);
                }     System.out.println();
                return font;
            }
    Last edited by willemjav; 03-10-2014 at 08:35 PM.

  19. #19
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: what is better to do?

    Next I enter the color object-to-string into the next method
    using a key (color name) and value (color object) map:


    Java Code:
     public String getColorString(String colorstr) {
            final Pattern pattern = Pattern.compile("[=,\\]]");
            final String[] result = pattern.split(colorstr);
             Color c = new Color(Integer.parseInt(result[1]),
                     Integer.parseInt(result[3]),Integer.parseInt(result[5]));
                Set entries = colorMap.entrySet();
                Iterator iter = entries.iterator();
                String key;
                Color value;
                Map.Entry entry;
                while (iter.hasNext()) {
                    entry = (Map.Entry)iter.next();
    
                    value = (Color) entry.getValue();
                    if (value.equals(c)) {
                        colorstr = (String) entry.getKey();
                        break;
                    }
                }
            return colorstr;
        }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •