Creating an instance variable in one class that connects to another instance variable
Hey,
What I'm trying to do is creating an instance variable in one class that connects to another instance variable.
I'm gonna create an instance variable of the class Model in the class View. That instance variable of Model is gonna connect with instance variable of another class called TegnePanel which is also in class View.
Here is my how I tried to do it but I get errors on cp.add(model, BorderLayout.CENTER); and tegnepanel.addModel(new model());
I can't figure out what I did wrong, did I forget something?
JPanel jpInner = new JPanel();
Code:
public class View extends JFrame {
{
JMenu file = new JMenu("File");
JMenu format = new JMenu("Format");
JMenu setup = new JMenu("Setup");
JMenuBar menuBar = new JMenuBar();
menuBar.add(file);
menuBar.add(format);
menuBar.add(setup);
setJMenuBar(menuBar);
setSize(300, 300);
setLocation(200, 200);
setVisible(true);
}
GridLayout gl = new GridLayout(0,2);
JPanel jpInner = new JPanel();
String TegnePanel;
String Model;
public View(){
Container cp = this.getContentPane();
TegnePanel tegnepanel = new TegnePanel();
Model model = new Model ();
cp.add(tegnepanel, BorderLayout.CENTER);
cp.add(model, BorderLayout.CENTER);
tegnepanel.addModel(new model());
}
*/
this.setTitle("Grafikk");
this.setLocation(100, 100);
this.setSize(500,450);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
View v = new View();
}
}
Re: Creating an instance variable in one class that connects to another instance vari
I'm not sure that you've given us enough information to allow us to be able to help you. For instance, when mentioning errors, you should show actual error messages. And when describing and showing code, assume that we have no idea what the rest of your program looks like. Also on a minor note, your class naming is a little confusing since many of us equate "model" with non-GUI logic portion of the program, one that wouldn't get added as a component to the view. Also, what exactly do you mean by having one variable "connect to" another variable. Please provide more information and code, and please provide much more detail in your description.