问题
I have 2 methods, the first one where i display selected value, index from my JList(list). What i want to do is to send selectedValue - s - to CreateMap method. I tried this code, but s variable is null. Why?
public void actionPerformed(ActionEvent e)
{
int index = 0;
if(e.getActionCommand().equals("Check")){ //if button is pressed
index = list.getSelectedIndex();
System.out.println("Index selected" + index);
String s = (String) list.getSelectedValue();
System.out.println("Value Selected " +s);
createMap();
}
}
private Map<String, Integer>createMap()
{
Map<String, Integer> graphicsMap = new HashMap<>();
for(LaneInformation l:graphics.laneInfos )
{
if (l.getEllipse().contains(graphics.startX, graphics.startY)) {
graphicsMap.put(this.s, graphics.startX);
}
}
return graphicsMap;
}
回答1:
I assume you have 2 String variables called s
, one global and one local in actionPerformed
.
Change the line
String s = (String) list.getSelectedValue();
to
this.s = (String) list.getSelectedValue();
And it should work. Be sure you have a global variable called String s
.
来源:https://stackoverflow.com/questions/28961811/get-string-from-another-method