I think I found something weird.. Or it could just be something that I don't understand. I get this error when I tried to add a ActionListener to my popupRequest variable as shown in the code snippet.
error: variable popupRequest might not have been initialized
popupRequest.addActionListener(new ActionListener() {
1 error
So meaning to say my popupRequest isn't initialised and that is probably why the error is thrown. But the thing is I did initialise that variable.
Code Snippet
JPopupMenu popup = new JPopupMenu();
JMenuItem popupTitle,popupHostJoin,popupRequest;
if (SwingUtilities.isRightMouseButton(evt)) {
JL_CurrentUsers.setSelectedIndex(JL_CurrentUsers.locationToIndex(evt.getPoint()));
popup.add(popupTitle = new JMenuItem("Private Message"));
popup.addSeparator();
if (nickname.equals(JL_CurrentUsers.getSelectedValue()))
popup.add(popupHostJoin = new JMenuItem("Host..."));
else {
popup.add(popupRequest = new JMenuItem("Request..."));//I initialise it here
popup.add(popupHostJoin = new JMenuItem("Join..."));
}
popupHostJoin.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new PvtMessageGUI(fHost,fPort,nickname).setVisible(true);
}
});
//this is the line that gives me that error
popupRequest.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
client.sendMessage(new ChatMessage(ChatMessage.REQUEST,nickname+"->"+JL_CurrentUsers.getSelectedValue()));
}
});
popup.show(JL_CurrentUsers,evt.getX(),evt.getY());
}
I can work around it by initialising my popupRequest when i first declare the variable.
JMenuItem popupTitle,popupHostJoin,popupRequest=new JMenuItem("Request"...);
What I wanna know is.. What is the difference between initialising my popupRequest when I declared it at the beginning, and initialising it inside my if-else statement? And I don't see why the program didn't detect the initialised popupRequest but it did for the initialised popupHostJoin variable when I added the ActionListener to it.
P.S. I hope my post isn't too confusing.
Aucun commentaire:
Enregistrer un commentaire