Forums Register Login

Help in adding ActionListener.

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
You can make use of Anonymous inner classes. Something like


You can read more about such approach here.
+Pie Number of slices to send: Send
Thanks for the fast reply, I will read through and come back if i could not understand anything.
+Pie Number of slices to send: Send
the other approach is to use one actionListener and a switch or if statement
1
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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]
+Pie Number of slices to send: Send
 

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?

+Pie Number of slices to send: Send
No. I have added the ActionListener to the JCheckBox (checkLbl).
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
This tiny ad is wafer thin:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1871 times.
Similar Threads
Problem with custom painting and multiple JPanel
Problems Resizing TextArea
GUI interaction
Help Me Click the Default Button
last week of class last assignment. Any help is appreciated.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:02:45.