Home Ask Login Register

Developers Planet

Your answer is one click away!

Carlos February 2016

Share variable between classes - Pane Constructors

I'm a newbie in Java and I would like to ask you a quite simple question which concerns classes:

I have a class that needs the name of a Pane that will be further introduced afterwards:

private final class CustomDocumentFilter extends DocumentFilter {
    private final StyledDocument styledDocument = pane.getStyledDocument();   
    // etc etc

private void cree_ihm() {
    Container pane = getContentPane();
    ((AbstractDocument) pane.getDocument()).setDocumentFilter(new CustomDocumentFilter());
    // etc etc

which for sure doesn't work because pane is defined afterwards. I think the problem is that I don't really understand how to deal with classes in a functions' environment.

I would greatly appreciate your help.


cricket_007 February 2016

  1. You can't use a variable in a field like that, so you'll need to use a constructor to do the field assignment.

  2. The java.awt.Container class that is returned by getContentPane() does not have a getStyledDocument() or getDocument() method. You need a JTextPane.

Also not sure why you had a private class.

public final class CustomDocumentFilter extends DocumentFilter 
    private StyledDocument styledDocument;
    private JTextPane pane;

    public CustomDocumentFilter(JTextPane pane) {
        this.pane = pane; // Not really necessary
        this.styledDocument = pane.getStyledDocument();

    // Override some DocumentFilter methods here like insert and remove

I assume this method is in another class somewhere?

private void cree_ihm() {
    JTextPane pane = new JTextPane(); // <-- Get a JTextPane from somewhere
    ((AbstractDocument) pane.getDocument()).setDocumentFilter(new CustomDocumentFilter(pane));


Post Status

Asked in February 2016
Viewed 3,120 times
Voted 8
Answered 1 times


Leave an answer

Quote of the day: live life