1

Say I have this class:

public class ExitView extends JPanel {

    private static final long serialVersionUID = 1L;

    public ExitView() {
            JLabel title = new JLabel("Exit?");
    title.setFont(new Font("Ariel", Font.PLAIN, 44));
            this.add(title);
    }
}

And this also:

public class EndView extends ExitView {

    public ExitView() {
            this.remove(title);
    }
}

This code is stripped way down, but in my code this is not removing the JLabel in EndView. I am able to just title.setText("") but that's not really getting rid of it. Can anybody explain why it wouldn't remove the label? Thanks.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
user3226170
  • 175
  • 1
  • 1
  • 14

5 Answers5

1

you are creating a label in the base class constructor and adding this directly to the JPanel. But in the later check you don't use the reference to that created JLabel but some other. b/c the variable is out of scope it might be difficult to reference it again. The fix is as Paco mentioned. bring the variable out of the constructor scope and set it globably.

public class ExitView extends JPanel {

private static final long serialVersionUID = 1L;

    public ExitView() {
        JLabel title = new JLabel("Exit?"); // <- here you create a JLabel 
                                            // in that scope where is the               
                                            // reference for later use??
        title.setFont(new Font("Ariel", Font.PLAIN, 44));
        this.add(title);
    }
}

public class EndView extends ExitView {

     public ExitView() {
        this.remove(title); <- // the reference you create here doesn't 
                               // equals the JLabel created earlier
     }
}
Blaatz0r
  • 1,205
  • 1
  • 12
  • 24
0

Try making the jlabel a field of the class:

public class ExitView extends JPanel {

    private static final long serialVersionUID = 1L;
    private JLabel title;

    public ExitView() {
            title = new JLabel("Exit?");
    title.setFont(new Font("Ariel", Font.PLAIN, 44));
            this.add(title);
    }
}

Edited
Look at this minimal example, it works:

public class ExitView extends JPanel{
    protected JLabel label;

    public ExitView() {
        label = new JLabel("your label");
        this.add(label);
    }

    public static void main(String[] args) {
        JDialog dialog = new JDialog();
        EndView endView= new EndView();

        dialog.add(endView);
        dialog.pack();
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setVisible(true);
    }
}

class EndView extends ExitView {
    public EndView() {
        this.remove(label);
    }
}

Your error must be on other place. How do you use EndView?

Paco Abato
  • 3,920
  • 4
  • 31
  • 54
  • Changed it to this (as it makes total sense) and made the visibility protected, but still getting the same result... Not removing it from the subclass. – user3226170 Feb 06 '15 at 00:08
0

When you use the extends (some class) in Java thenthe class that extends

this can see only the variables(Objects) that are out of constructor and other methods and are (public or protected) but not (private).

public class ExitView extends JPanel {

private static final long serialVersionUID = 1L;
public JLabel title=new JLabel("Exit?"); //Here

public ExitView() {
   title.setFont(new Font("Ariel", Font.PLAIN, 44));
        this.add(title);
 }
}

The other class:

@SuppressWarnings("serial")

public class EndView extends ExitView {

public EndView() {
        this.remove(title); //now it can see the JLabel cause
                            //it is out of constructor and other
                           //methods and is (public or protected)
 }
}

Also check this link implements Vs Extends why and why not..

Community
  • 1
  • 1
crAlexander
  • 376
  • 1
  • 2
  • 12
0

You're also using the ExitView constructor in EndView.

0

Can anybody explain why it wouldn't remove the label? Thanks.

Because:

public class ExitView extends JPanel {

    private static final long serialVersionUID = 1L;

    public ExitView() {
        JLabel title = new JLabel("Exit?"); // local JLabel instance
    title.setFont(new Font("Ariel", Font.PLAIN, 44));
        this.add(title);
    }
}

Then:

@SuppressWarnings("serial")

public class EndView extends ExitView {

    public EndView() {
        this.remove(title); //now it can see the JLabel cause
                            //it is out of constructor and other
                            //methods and is (public or protected)
                            //'title' is not the same 'title' JLabel
                            // set in the superclass.
    }
}

There is a way to actually remove the component but it is messy. What I would suggest you do instead of having a JLabel containing another JLabel, change title to be a String. You can simply change the text property of the label by setting it to empty String, or by setting the visibility to false.

Pang
  • 9,564
  • 146
  • 81
  • 122
hfontanez
  • 5,774
  • 2
  • 25
  • 37