New to listeners and actionperformed
For simplicity, I have a JFrame which contains a JList and a button.
For the JList I wrote my own class listselectionlistener which gives me the selected items from the JList.
for the button I added a ActionListener to do what i want it to do.
My problem is that I need the selected items from the JList to use in the actionperformed of the button. I can't seem to get this right.
As I'm very new to Java, any example code would be greatly appreciated. Thanks!
Let's see what you've got -- your code -- and work from there. Please use code tags when you post.
OK, but my code is a 810 lines, so i'll post this and let me know if you need more.
here is my listselectionlistener
//<editor-fold defaultstate="collapsed" desc=" updateFrontList SELECTION LISTER" >
class FrontListSelectionListener implements ListSelectionListener {
public Object[] frontListTempObj = new Object[myData.size()];
public void valueChanged(ListSelectionEvent evt) {
if (!evt.getValueIsAdjusting()) {
JList list = (JList)evt.getSource();
frontListTempObj = list.getSelectedValues();
for (int i = 0; i < frontListTempObj.length; i++) {
Object singleSelect = frontListTempObj[i];
ImageIcon previewIcon = new ImageIcon(frontListTempObj[i].toString(), null);
previewLabel.setIcon(previewIcon);
//previewLabel.validate();
}
}
}
}//</editor-fold>
here is the button listen thingy
class MyRemoveButtonActionListener implements ActionListener {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ArrayList<String> removeArray = new ArrayList<String>();
//adds the selected file for deletion to an removeArray after the user confirmed
for (int i = 0; i < frontListTempObj.length; i++) {
///--it continues for a while, my error comes where i want to use frontlistempobj in the above.
I'm just learning too, and I have to admit that this question is making me read up and try to learn a bunch of stuff, so take what I'm telling you with a grain of salt, but I was wondering if you should fool with the frontListTempObj at all, or would it be just better asking the JList for the list of selected values at the time the button is pressed. You could ask the JList directly if both button and JList are in the same class, or use a getter if they are in different classes. Or do I not understand your question and am over-simplifying things?
So what is the error?Is it something like frontListTempObj might not have been initialized?
dwga at 2007-7-12 21:55:49 >

That sounds like what i need. The error is it can't find the variable (frontListTempObj), I assume because it's in a different class. Both the listlistener and actionlistener is in the same class, so if you can show me how i can reference from the actionlistener class to the listlistener variable, that would be great.
You could let your FrontListSelectionListener class implement the ListSelectionListener as well as the ActionListener. That way both the actionPerformed and valueChanged methods could operate on the frontListTempObj.
class FrontListSelectionListener implements ListSelectionListener, Action Listener {
public Object[] frontListTempObj = new Object[myData.size()];
public void valueChanged(ListSelectionEvent evt) {
if (!evt.getValueIsAdjusting()) {
JList list = (JList)evt.getSource();
frontListTempObj = list.getSelectedValues();
for (int i = 0; i < frontListTempObj.length; i++) {
Object singleSelect = frontListTempObj[i];
ImageIcon previewIcon = new ImageIcon(frontListTempObj[i].toString(), null);
previewLabel.setIcon(previewIcon);
//previewLabel.validate();
}
}
}
public void actionPerformed(java.awt.event.ActionEvent evt) {
ArrayList<String> removeArray = new ArrayList<String>();
//adds the selected file for deletion to an removeArray after the user confirmed
for (int i = 0; i < frontListTempObj.length; i++) {
///--it continues for a while, my error comes where i want to use frontlistempobj in the above.
}//</editor-fold>
Again, sorry if this is over-simplifying things, but in the class that has the JList (lets assume that it's called myJList (not the ListSelectionListener, the JList), have this method:
public Object[] getmyJListSelections()
{
return myJList.getSelectedValues();
}
Then have your button's actionlistener call it:
private void doButton1Action(ActionEvent arg0)
{
// ask the list to give us the selected values and display them
// in a StringBuilder object
Object[] selectedValues = getmyJListSelections(); // will have to use object name here too
StringBuilder mySB = new StringBuilder();
if (selectedValues != null && selectedValues.length > 0)
{
mySB.append("Selected values are: " + "\n");
for (int i = 0; i < selectedValues.length; i++)
{
mySB.append(selectedValues[i] + "\n");
}
}
else
{
mySB.append("Nothing was selected from the list");
}
// show what we have collected
JOptionPane.showMessageDialog(null, mySB.toString());
}
Edit: nevermind, just do what hunter suggests. I bow to his greater knowledge and experience.
Message was edited by:
petes1234
> Edit: nevermind, just do what hunter suggests. I bow> to his greater knowledge and experience.Nothing about my razor wit or charming good looks? ;)
> > Edit: nevermind, just do what hunter suggests. I
> bow
> > to his greater knowledge and experience.
>
> Nothing about my razor wit or charming good looks? ;)
I love to program and I love to post here, but I'm a hobbiest and know my limits. So I hate to burst your bubble, but my calling you a better coder is a backhanded compliment at best. ;)
nevermind. I talked to my wife, and she said to say that it is all about your razor wit and charming good looks.
Thanks hunter! was thinking along those lines, but I got my syntax wrong :(
but now, i still need your help.. I've added the listselectionlistener method to my button's actionlistener's class. now the button has the variable and executes fine, but it doesn't seem to get the selected items on the JList.
i tried to add the valuechanged method to the actionpeformed method, but that wasn't accepted. now i think i should add the listselectionlistener to the button e.g. button.addListSelectionListener(new MyListSelectionListener?()); but that is also not accepted....
i think i'm still at square one with different code, lol
Any help please?
@petes, she's a smart woman, hang on to her :)@Tjorrie, post your new code, it sounds like you've got the syntax wrong.
my button
removeButton.addActionListener(new RemoveButtonActionListener());
and...
class RemoveButtonActionListener implements ListSelectionListener, ActionListener {
public Object[] frontListTempObj = new Object[myData.size()];
public void valueChanged(ListSelectionEvent evt) {
if (!evt.getValueIsAdjusting()) {
JList list = (JList)evt.getSource();
frontListTempObj = list.getSelectedValues();
for (int i = 0; i < frontListTempObj.length; i++) {
Object singleSelect = frontListTempObj[i];
ImageIcon previewIcon = new ImageIcon(frontListTempObj[i].toString(), null);
previewLabel.setIcon(previewIcon);
//previewLabel.validate();
}
}
}
public void actionPerformed(java.awt.event.ActionEvent evt) {
ArrayList<String> removeArray = new ArrayList<String>();
//adds the selected file for deletion to an removeArray after the user confirmed
for (int i = 0; i < frontListTempObj.length; i++) {
int option = JOptionPane.showConfirmDialog(null, "Are you sure you want to remove " + listSelection.get(i));
switch (option) {
case 0: removeArray.add(listSelection.get(i)); break;
case 1: textArea.append("\nUser selected No for " + listSelection.get(i) + "\n"); break;
case 2: textArea.append("\nUser selected cancel for " + listSelection.get(i) + "\n"); break;
default: textArea.append("\nBad input\n");
}
}////etc etc
You add the actionlistener to the button, but do you ever add the listselectionlistener to the jlist? If you don't then the jlist's events won't cause anything to happen.
Yes, I've changed that to :frontList.addListSelectionListener(new RemoveButtonActionListener());is this right or wrong? i don't know :( It's executes fine, but it looks like the frontListTempObj is empty, it just skips over the for loop :(
Have you put any System.out.println(...) statements in your RemoveButtonActionListener method to see what it is in fact doing? If not, consider trying that.
Yes I have. just now i added some more and it says the frontlisttempobj is empty. why doesn't the
nevermind! just thought of something and got it working! because the frontlistTempObj is created (is that the right word? or should it be initialized?) in the RemoveButtonActionListener, it loses it's value (i assume it goes out of the memory) and therefore when only the actionperformed is called, it is zero! so i just moved it out to the class's super (if that's the right way to put it), i.e. moved it up one scope, so now it keeps it's value, it changes correctly and the changed selected items stays in the object. so now my program works! yay! thanks for hunter and for pete's sake....hehe