• Post Reply Bookmark Topic Watch Topic
  • New Topic

how do you ..  RSS feed

 
Thay Xianrong
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do you delete something from a JList .. ?
can u add ActionListener to an JList?
example(the actual coding is at home.. i'm at school now)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class myCoding extends JFrame implements ActionListener
{
JList abc;
public static void main(String[] args)
{
abc = new JList();
JList.addActionListener(this);
}
public void ActionPerformed(actionEvent e)
{
abc.remove(e.getSource()) // can this work ??
}
}
is there a .remove() method for JLists ??
and.. i've been doing this swing thing for quite a while .. what does the "extends JFrame" actually mean .. can anyone xplain it to me .. i dun see any JFrame class anywhere.. is it imported ?
TIA .. !
 
Korol Bloom
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Thay,
and welcome to the ranch! So you have school today? Over here in Germany we have Easter holyday -- no school, no job, shops are closed.
Well, the apropriate listener for a JList is a ListSelectionListener. If you add this to the jlist, you will receive an event, whenever an item is selected or deselected. Is that the actoin you want to trigger? I don't know how to remove an item from the list, maybe you cannot.
Which kind of jframe you are talking about. Is it this one? javax.swing.JFrame
It exists!
, korol
 
Julie de Wet
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
private final ArrayList arrayList = new ArrayList();
...
JList list = new JList();
arrayList.add(...) // add elements - if you have a Collection use addAll

list.setModel(new AbstractListModel()
{
public int getSize()
{
return arrayList.size();
}

public Object getElementAt(int i)
{
return arrayList.get(i);
}
});
list.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent e)
{
if(!list.isSelectionEmpty())
{
arrayList.remove(...); // maybe remove(list.getSelectedIndex());
list.clearSelection();
list.repaint();
}
}
});
As to JFrame it is part of the javax.swing package which you import...
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!