• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static context for AbstractAction question  RSS feed

 
David Pantale
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'm sure there's a simple explanation for this but I can't figure it out. I extended AbstractAction and implemented actionPerformed. I want to reference a method in another, clearly non-static, class. However I'm getting a non-static method cannot be referenced from a static context message in the actionPerformed method. I checked AbstractAction and the ActionListener interface and nothing there was static. Can someone please explain why I'm getting this message?

Thanks Dave
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler is not lying, but it's impossible to see where you're going wrong without some code. Trim it down to the bare minimum to demonstrate the problem, please.

Also:
I want to reference a method in another, clearly non-static, class.


It's not whether the class is static that matters. It's whether you're in a static method or initializer. And in any case, only non-top-level classes can be static.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Pantale wrote:I'm sure there's a simple explanation for this but I can't figure it out.

It's a bit tough for us too without seeing your code (and also the exact message you're getting). Plainly a piece of code in a static context is trying to access a method that isn't, but why and where is presumably what you want to know.

Winston
 
David Pantale
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the swift responses. The code is below (note: I'm using NetBeans):

//Class that I wish to get information from.
public final class PortfoliosTopComponent extends TopComponent implements ExplorerManager.Provider, LookupListener {

private static transient ExplorerManager em = new ExplorerManager();
private Lookup.Result<PortfolioNode> result = null;
private static PortfolioNode portfolionode = null;
private String portfolioname;
.
.
.
public String getPortfolioName() { //I want to retrieve this value
return portfolioname;
}
}


//Action class that is attempting to get the info
public class AddStockAction extends AbstractAction {

public AddStockAction() {
super("Add Stock");
}

@Override
public void actionPerformed(ActionEvent e) {

String result = JOptionPane.showInputDialog("Enter name:");
if (result == null) {
return;
}
xmlManager xm = new xmlManager();
if (xm.addStock(PortfoliosTopComponent.getPortfolioName(), result)) //Right here is where I get the message:
PortfoliosTopComponent.refreshNode(); //non-static method getPortfolioName cannot be referenced from a static context

}

}

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Pantale wrote:

Well PortfoliosTopComponent is a class name, so PortfoliosTopComponent.getPortfolioName() is how you would call a static method.
What you need is an instance (ie, probably a variable) name there instead.

Winston
 
David Pantale
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, very good point. Unfortunately I'm not quite sure how I'm going to be able to do this. Any suggestions?
 
David Pantale
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I figured out a way to do it - Netbeans only I'm afraid. To get a reference to a node in another class (TopComponent) use:

Node[] n = TopComponent.getRegistry().getCurrentNodes();

and then, for example, its:

String x = n[0].getDisplayName();

if you want the nodes display name.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks wrong just from looking at it.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!