Reputation: 1
so I have three different classes... the first is the desktop jframe with a menu bar. the second is a jdialog and the last is a menuitem. My hope is to pass the results of the Jdialog to the desktop jframe, from there I want to use that information to create a new menuitem in the desktop Jframe.
Here is what I have:
Desktop Jframe (called "DesktopFrame"):
thingAddMenu.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent event)
{
newThing = new NewThing(DesktopFrame.this);
newSem.setVisible(true);
thingEditMenu.add(NewThing.getItem());
thingMenu.add(thingEditMenu);
bar.add(thingMenu);
}//end method actionPerformed
}// end anonymous inner class
);//end addActionListener
JDialog class (called "NewThing"):
btnCreate.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
item = new thingMenuItem(timeframe,num);
dispose();
}
});
getContentPane().add(GenInfo, BorderLayout.NORTH);
pack();
setResizable(false);
setLocationRelativeTo(parent);
}
public int getNum()
{
return num;
}
public JMenuItem getItem()
{
return item;
}
public String getTime()
{
return timeframe;
}
JMenuItem class (called "thingMenuItem"):
public class thingMenuItem extends JMenuItem
{
public thingMenuItem(String name, int num)
{
super(name);
addActionListener(
new ActionListener() // anonymous inner class
{
public void actionPerformed( ActionEvent event )
{
//do some task
} // end method actionPerformed
} // end anonymous inner class
); // end call to addActionListener
}
}
This is just a piece of the entire code. The idea is that you click "thingAddMenu" to create a "newThing" instance from the JDialog class "NewThing". Then from here, you click a button called "Create" in this JDialog (that appears on the Desktop Jframe). When you click the button, it creates a new MenuItem that will be added to the "thingEditMenu" of the overall Desktop Jframe. Right now my error is in the line of the Desktop Jframe "thingEditMenu.add(NewThing.getItem());". It says I cannot make a static reference to a non static method "getItem". The problem is that I hope to not make this static as the MenuItem's information will change with the user's input in that JDialog. So How can I make it static without actually making it static? Also does anyone have a better way to code this so that I can consolidate?
Upvotes: 0
Views: 109
Reputation: 36
A method listed as static cannot access fields of that class unless they are marked as static. The reason is that the static method is associated with the class - you do not need an instance of the class to run that code. When the static method is run, the fields of the class that are not static cannot be accessed since they are associated with an instance, not the class itself like the static method.
Upvotes: 0
Reputation: 57154
Change
newThing = new NewThing(DesktopFrame.this);
newSem.setVisible(true);
thingEditMenu.add(NewThing.getItem());
to
newThing = new NewThing(DesktopFrame.this);
newSem.setVisible(true);
thingEditMenu.add(newthing.getItem()); //variable name in lower case written
If not you are trying to call getItem
on the clasas, not the object, but since the method is not static that of course won´t work, you have to call it of the object you just before created.
Upvotes: 1