Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Trying to make a class that extends ParagraphView work in WrappedPlainView

Posted on 2004-08-25
12
Medium Priority
?
363 Views
Last Modified: 2013-11-23
Hi,

Firstly the source code for this question is:

/**
 * @author Stanislav Lapitsky
 * @version 1.0
 */

import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;

class LNTextPane extends JFrame {
    public LNTextPane() {
        JEditorPane edit = new JEditorPane();
        edit.setEditorKit(new NumberedEditorKit());

        JScrollPane scroll = new JScrollPane(edit);
        getContentPane().add(scroll);
        setSize(300, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String a[]) {
        new LNTextPane();
    }
}

class NumberedEditorKit extends StyledEditorKit {
    public ViewFactory getViewFactory() {
        return new NumberedViewFactory();
    }
}

class NumberedViewFactory implements ViewFactory {
    public View create(Element elem) {
        String kind = elem.getName();
        if (kind != null)
            if (kind.equals(AbstractDocument.ContentElementName)) {
                return new LabelView(elem);
            }
            else if (kind.equals(AbstractDocument.
                             ParagraphElementName)) {
//              return new ParagraphView(elem);
                return new NumberedParagraphView(elem);
            }
            else if (kind.equals(AbstractDocument.
                     SectionElementName)) {
                return new BoxView(elem, View.Y_AXIS);
            }
            else if (kind.equals(StyleConstants.
                     ComponentElementName)) {
                return new ComponentView(elem);
            }
            else if (kind.equals(StyleConstants.IconElementName)) {
                return new IconView(elem);
            }
        // default to text display
        return new LabelView(elem);
    }
}

class NumberedParagraphView extends ParagraphView {
    public static short NUMBERS_WIDTH=25;

    public NumberedParagraphView(Element e) {
        super(e);
        short top = 0;
        short left = 0;
        short bottom = 0;
        short right = 0;
        this.setInsets(top, left, bottom, right);
    }

    protected void setInsets(short top, short left, short bottom,
                             short right) {super.setInsets
                             (top,(short)(left+NUMBERS_WIDTH),
                             bottom,right);
    }

    public void paintChild(Graphics g, Rectangle r, int n) {
        super.paintChild(g, r, n);
        int previousLineCount = getPreviousLineCount();
        int numberX = r.x - getLeftInset();
        int numberY = r.y + r.height - 5;
        g.drawString(Integer.toString(previousLineCount + n + 1),
                                      numberX, numberY);
    }

    public int getPreviousLineCount() {
        int lineCount = 0;
        View parent = this.getParent();
        int count = parent.getViewCount();
        for (int i = 0; i < count; i++) {
            if (parent.getView(i) == this) {
                break;
            }
            else {
                lineCount += parent.getView(i).getViewCount();
            }
        }
        return lineCount;
    }
}

The thing that I would like to do is to make

class NumberedParagraphView "extends ParagraphView" {

"extends WrappedPlainView" instead.

However, if I tried to do that, it would keep scrolling over to the right. I would like it to work the same way as ParagraphView cause I require some functions of the WrappedPlainView class. You can try compiling the source code above and you would understand what i meant.

Can someone please tell me how to make it work using WrappedPlainView and not having it to keep scrolling to the right and why WrappedPlainView causes that problem?
0
Comment
Question by:_MW_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 11889966
Do you want to scroll to the right at all? If not you could just set the property of the scroll bar to never show up:

scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
0
 
LVL 12

Expert Comment

by:Giant2
ID: 11890341
I see this:
with extends ParagraphView:
1 sdasds
2 ewoifnwoin4r2fine
3 asdfeiwncwe...
4
...
and a scrollbar appear to the right only when the number of row are bigger than the window size (25).
continuing typing on the same line, when the right margin is reached, a new line is created (and numerated)

with extends WrappedPlainView:
1 fasdfòsdafsdjfajsdòlfkjsdkf
2 asdfjkadjfòjj
3
...
and a scrollbar appear to the right only when the number of row are bigger than the window size (25).
on the bottom appear a scollbar ever (even if the row typed are less than the windows size).
continuing typing on the same line, when the right margin is reached, the typing continue on the same line (not into a new line)

What you want to change?

0
 

Author Comment

by:_MW_
ID: 11891867
I am sorry...try this code instead...the problem only happens when you open a file with it

/**
 * @author Stanislav Lapitsky
 * @version 1.0
 */

import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
import java.io.*;

class LNTextPane extends JFrame {
    public LNTextPane() {
        JEditorPane edit = new JEditorPane();
        edit.setEditorKit(new NumberedEditorKit());
        try
        {
                      File file = new File("LNTextPane.java");
                      edit.read(new FileReader(file), file);
            }
            catch(IOException e)
            {
            }
            
       

        JScrollPane scroll = new JScrollPane(edit);
        getContentPane().add(scroll);
        setSize(300, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String a[]) {
        new LNTextPane();
    }
}

class NumberedEditorKit extends StyledEditorKit {
    public ViewFactory getViewFactory() {
        return new NumberedViewFactory();
    }
}

class NumberedViewFactory implements ViewFactory {
    public View create(Element elem) {
        String kind = elem.getName();
        if (kind != null)
            if (kind.equals(AbstractDocument.ContentElementName)) {
                return new LabelView(elem);
            }
            else if (kind.equals(AbstractDocument.
                             ParagraphElementName)) {
//              return new ParagraphView(elem);
                return new NumberedParagraphView(elem);
            }
            else if (kind.equals(AbstractDocument.
                     SectionElementName)) {
                return new BoxView(elem, View.Y_AXIS);
            }
            else if (kind.equals(StyleConstants.
                     ComponentElementName)) {
                return new ComponentView(elem);
            }
            else if (kind.equals(StyleConstants.IconElementName)) {
                return new IconView(elem);
            }
        // default to text display
        return new LabelView(elem);
    }
}

class NumberedParagraphView extends WrappedPlainView {
    public static short NUMBERS_WIDTH=25;

    public NumberedParagraphView(Element e) {
        super(e);
        short top = 0;
        short left = 0;
        short bottom = 0;
        short right = 0;
        this.setInsets(top, left, bottom, right);
    }

    protected void setInsets(short top, short left, short bottom,
                             short right) {super.setInsets
                             (top,(short)(left+NUMBERS_WIDTH),
                             bottom,right);
    }

    public void paintChild(Graphics g, Rectangle r, int n) {
        super.paintChild(g, r, n);
        int previousLineCount = getPreviousLineCount();
        int numberX = r.x - getLeftInset();
        int numberY = r.y + r.height - 5;
        g.drawString(Integer.toString(previousLineCount + n + 1),
                                      numberX, numberY);
    }

    public int getPreviousLineCount() {
        int lineCount = 0;
        View parent = this.getParent();
        int count = parent.getViewCount();
        for (int i = 0; i < count; i++) {
            if (parent.getView(i) == this) {
                break;
            }
            else {
                lineCount += parent.getView(i).getViewCount();
            }
        }
        return lineCount;
    }
}

It keeps scrolling to the right non stop...
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 12

Expert Comment

by:Giant2
ID: 11892995
I see a resizing in the code.
Now investigating about it.
0
 

Author Comment

by:_MW_
ID: 11893012
ok..thanks dude
0
 

Author Comment

by:_MW_
ID: 11893934
still no idea?
0
 
LVL 12

Accepted Solution

by:
Giant2 earned 1000 total points
ID: 11894054
I try this in class NumberedParagraphView:
      public void setSize(float f1, float f2) {
            if (getWidth() < f1) {
                  System.out.println("increase");
            }
            System.out.println("from " + getWidth() + " to " + f1);
            super.setSize(f1, f2);
      }
and see that the size of each line increase every time.

So I try to do this in class NumberedParagraphView:
      public void setSize(float f1, float f2) {
            if (getWidth() < f1) {
                  System.out.println("increase");
            }
            System.out.println("from " + getWidth() + " to " + f1);
            super.setSize(1000, f2);
      }
It seems to go.
0
 

Author Comment

by:_MW_
ID: 11894088
So you just add that method and it worked?

Where does this method gets called?
0
 
LVL 12

Expert Comment

by:Giant2
ID: 11894167
>So you just add that method and it worked?
yes, the second version.

>Where does this method gets called?
I think this method is called when a component is added. So when something is read a setSize is called (for each char of line).

Hope this help you.
Bye, Giant.
0
 

Author Comment

by:_MW_
ID: 11894183
I just started learning Swing a couple of weeks ago and so I kinda suck at it =(

Do you have any recommendations which site is good for learning Swing?

I am learning Swing from Sun's Swing tutorials but I think their tutorials are very basic.
0
 

Author Comment

by:_MW_
ID: 11894197
Yup...thanks a lot for your help again =)

Ur good!

0
 
LVL 12

Expert Comment

by:Giant2
ID: 11899799
The Sun Tutorial for Swing is a good basic tutorial.
I suggest you to learn at a realy good level this tutorial.
Another at basic level is here:
http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/

For particular things about Swing I personally like to search over the net because in this manner I see all the approaches available.

Thanks. Hapy to help you.
:)

Bye, Giant.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question