0

I am writing the code for a primitive address book for one of my classes, and it is required that we use a class for the address book itself, and then another class for putting in new entries. I understand this, but when I call the class method for entering new contacts, I want to change the GUI. I am using JFrame, and so for my GUI base i am using the contentPane. In my first class I created my contentPane with a basic GUI:

public class Address extends JFrame implements ActionListener
{
    Container contentPane;

    public Address()
    {
        super();
        contentPane = getContentPane();
        contentPane.setLayout(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("Address Book");
        setSize(775,775);
        setLocation(0,0);
        setBackground(Color.BLUE);
    }
}

And now in my second class, the input one, I want to clear the contentPane with contentPane.removeAll().

class Entries
{
    public void newContact()
    {
        contentPane.removeAll();
    }
}

Unfortunately, nothing in my class Entries is recognizing contentPane and other variables i am attempting to modify. Is there a special way to name the contentPane and other variables in class Address so that they are usable in Entries, or do i need to recreate the variable?

Harper
  • 140
  • 2
  • 4
  • 10

3 Answers3

1

When you create an object of Entries you need to give it a reference to the Address object so you can use its variables. I'm not sure how your program layout is set up, but if Entries is created within Address you'd call Entries entry = new Entry(this); or if it is from a parent to both classes you'd need to set up variables...

Address address = new Address();
Entries entry = new Entry(address);

Code for how you'd access it in Entries:

class Entries
{
    private Address reference;

    public Entries(Address reference) {
        this.reference = reference;
    }

    public void newContact()
    {
        reference.contentPane.removeAll();
    }
}
Grambot
  • 4,370
  • 5
  • 28
  • 43
  • Thank you, this didn't short cut the question or work its way around. It worked immediately and is usable in any situation relating to my question. – Harper Jan 26 '13 at 02:25
  • Unfortunately, when i tried this on a larger scale using arrays and JLabels, it broke down. any suggestions? – Harper Jan 29 '13 at 02:15
0

You need to pass the reference of contentPane to Entries either as a constructor arg or add a contentPane property(getter,setter) to Entries.

Manuel Quinones
  • 4,196
  • 1
  • 19
  • 19
0

You need a reference to access stuff, either you duplicate the variable and give the contentPane in the Entries constructor :

private Container contentPane;

public Entries(Container contentPane) {
    this.contentPane = contentPane;
}

Or you create a getter on your contentPane in the Address class and pass the contentPane.

Both solutions are quite bad in my opinion however, maybe you should consider extending a JPanel for your entries class, and add the Entries to your main contentPane, then you will be able to do whatever UI stuff you want in the Entries class.

Michael Celey
  • 12,645
  • 6
  • 57
  • 62
Javadrien
  • 201
  • 1
  • 6
  • I am not familiar with how to pass things. even if it is not necessarily the best way, it may be good knowledge. Could you explain here/provide me with a link that would explain how to pass the contentPane? – Harper Jan 29 '13 at 02:16