• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help in adding ActionListener.

 
Andrew Geroge Alexander
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am trying to display checkbox(s), and text field(s) and a button based on the number of files in a Directory. For example I want 3 Checkboxes(with the lable of the file name) and 3 text boxes with a Button if there are 3 files in the directory.
I have achieved to do this by using the following code,



What I fail to know is adding a diferent actionlistener to every differerent component. I have added the action listener to the components, but they call the same action performed method. (I do know that ActionListener fires the ActionPerformed method, but I want every component to do something else)
Eventually I want to take the file name and the number of records from each file; and then to display in the text area.
Any help on adding a different action listener?
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can make use of Anonymous inner classes. Something like


You can read more about such approach here.
 
Andrew Geroge Alexander
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the fast reply, I will read through and come back if i could not understand anything.
 
Randall Twede
Ranch Hand
Posts: 4469
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the other approach is to use one actionListener and a switch or if statement
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote:the other approach is to use one actionListener and a switch or if statement

The only concern here would be that in case a new condition comes up then one would have to edit the if statement, kind of fragile.
 
Andrew Geroge Alexander
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm... I tried implementing the Anonymous inner class, i have some bit of a problem. Say if there are two files in the jList, so the loop runs for two times thus placing two checkboxes with their file names, if the user clicks one checkbox i am taking it in a string array.



The problem now i have is, If the user decides to deselect the file when he clicks it again, it still gets recorded.

I have obtained the output as in the attachment. So if i click the file it is stored in the msg[] array if it is deselected it still is again stored in the array.

any idea on how to avoid this.
file.jpg
[Thumbnail for file.jpg]
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem now i have is, If the user decides to deselect the file when he clicks it again, it still gets recorded.

You mean Clicking on the JList?

If yes then,for which event of JList have you added the listener?

 
Andrew Geroge Alexander
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. I have added the ActionListener to the JCheckBox (checkLbl).
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you debug to see if the event listener method is being invoked when you uncheck the CheckBox? I am not sure which event handles the check/uncheck operations (ActionListener is one, but there should be few specific to check box component)

The problem might be that the JList component is not refreshed to reflect the new array or the changes in the array.

How are you managing the deleting of String/File name from the array?

Also please show us the code, not just screeshots.
 
Andrew Geroge Alexander
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I am not sure how to do that, I mean;
Did you debug to see if the event listener method is being invoked when you uncheck the CheckBox?

The JList is placed in another class, that gets the info based on the following code.

How are you managing the deleting of String/File name from the array?

I have not gone so far yet.. I am just trying to avoid the same file getting into the array in the first place,
I think a simple sort and then the following code will do the trick.
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ruban Raj Eugin Francis wrote:
The problem now i have is, If the user decides to deselect the file when he clicks it again, it still gets recorded.

use checkLbl.isSelected() to find if the checkbox has been selected and use it in a if condition.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic