Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

EventListener and itemListener  RSS feed

 
Pedro Neves
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, i'm experiencing a problem. when i an action listener to some objects they do not respont and i dont know why.

Here's some of my code to provide a better explanation about my problem.


public class InterfaceGrafica extends JFrame implements ActionListener,ItemListener
{

...

JButton jb;
JMenu About;
JMenuItem Help;

...

public InterfaceGrafica()
{

for(int i=0 ; i < nods.length ; i++)
{

JPanel meio_cima = new JPanel(); //JPanel que contem o botão view (permitirá escolher quais os sensores a ver)


//botão view (permitirá escolher quais os sensores a ver)
jb = new JButton("View");
jb.setPreferredSize(new Dimension(100,20));
jb.addActionListener(this);
meio_cima.add(jb);


//Panel qu albergará o panel meio e o panel meio_cima
JPanel principal = new JPanel();
//JLabel Lauxx = new JLabel(nods[i]); ================> apenas para controlo


}

//Menu ABOUT
About = new JMenu("About");
About.addActionListener(this);

//Menu HELP
Help = new JMenu("Help");
Help.addActionListener(this);

}
public void actionPerformed(ActionEvent e) {

Object source = e.getSource();


if(source == jb)
System.out.print("View"); ---------------------> do not respond

if(source == About)
System.out.print("About"); ---------------------> do not respond
if(source == Help)
System.out.print("Help"); ---------------------> do not respond
}

}


Besides this, i have one more problem. When i have a panel with checkboxes (a JCheckBox []) how do i know what item is checked or unchecked to trat on ItemeventListener??

Hope you can help me

Thanks in advance
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not an awt expert, but I would think that your comparisons are all coming up false.
It would be much better to maybe compare an attribute of source (name maybe?)
to determine where the event originated.



When posting code, you should use the [ code ] tags to improve readability.


WP
 
Pedro Neves
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William P O'Sullivan wrote:Not an awt expert, but I would think that your comparisons are all coming up false.
It would be much better to maybe compare an attribute of source (name maybe?)
to determine where the event originated.



When posting code, you should use the [ code ] tags to improve readability.


WP



Sorry.

Well i'm doing the same thing to another buttons and it works. Besides, how can i get the name of the object it generated the event? i think it might help to my second question
 
Vince Stout
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For starters, you are putting the event listener on "Help", which is declared as a JMenuItem, not JMenu, I'm not sure how your window is layed out, but that could be a problem. But as far as getting the source...



...should work fine for catching the event. Maybe the line...
Object source = e.getSource();
...is causing a problem. Kinda hard to tell without seeing all of your code.

-VS
 
Pedro Neves
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vince Stout wrote:For starters, you are putting the event listener on "Help", which is declared as a JMenuItem, not JMenu, I'm not sure how your window is layed out, but that could be a problem. But as far as getting the source...



...should work fine for catching the event. Maybe the line...
Object source = e.getSource();
...is causing a problem. Kinda hard to tell without seeing all of your code.

-VS



well.. Unfortunatly that also didn't solves the problem. I tried somethig like this: e.toString().equals(jb.getText()) but it also didn't solves the problem. the layout of my window and panels is null. :-/ . But it works for the other buttons and objects i have.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the way you have it coded
jb = new JButton("View");
inside a for loop means this line

if(source == jb)

will only be true for the last button added on the last panel
 
Pedro Neves
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:the way you have it coded
jb = new JButton("View");
inside a for loop means this line

if(source == jb)

will only be true for the last button added on the last panel




You are tottally right. I think i could solve it with an array, but then again, how can i know the "view" button that had generated the event?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
loop the array, and if jbArray[x] == source
doSomething();
break;
 
Pedro Neves
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:loop the array, and if jbArray[x] == source
doSomething();
break;


Thanks Man its working ;). What about help Menu and about menu? how can i solve that? does anybody knows?

Thanks in advance
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the 4th reply had this

For starters, you are putting the event listener on "Help", which is declared as a JMenuItem, not JMenu


did you do anything about that?
 
Pedro Neves
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:the 4th reply had this

For starters, you are putting the event listener on "Help", which is declared as a JMenuItem, not JMenu


did you do anything about that?



I made it ;) thanks a lot to everyone
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!