aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Help in adding ActionListener. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Help in adding ActionListener." Watch "Help in adding ActionListener." New topic
Author

Help in adding ActionListener.

Andrew Geroge Alexander
Ranch Hand

Joined: Feb 14, 2012
Posts: 31
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

Joined: Sep 08, 2007
Posts: 3054
    
  33

You can make use of Anonymous inner classes. Something like


You can read more about such approach here.


Mohamed Sanaulla | My Blog
Andrew Geroge Alexander
Ranch Hand

Joined: Feb 14, 2012
Posts: 31
Thanks for the fast reply, I will read through and come back if i could not understand anything.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4339
    
    2

the other approach is to use one actionListener and a switch or if statement


SCJP
Visit my download page
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3054
    
  33

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

Joined: Feb 14, 2012
Posts: 31
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.


[Thumbnail for file.jpg]

Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3054
    
  33

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

Joined: Feb 14, 2012
Posts: 31
No. I have added the ActionListener to the JCheckBox (checkLbl).
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3054
    
  33

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

Joined: Feb 14, 2012
Posts: 31
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

Joined: Oct 16, 2003
Posts: 1075
    
  10

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.


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help in adding ActionListener.
 
Similar Threads
Help Me Click the Default Button
GUI interaction
last week of class last assignment. Any help is appreciated.
Problem with custom painting and multiple JPanel
Problems Resizing TextArea