I have a JList on a JScrollPane, would like to update the view
after removing/adding some elements. I tried (JList).revalidate(),
(JList).repaint(), (JScrollPane).revalidate() and (JScrollPAne).repaint(),
just cannot get it to work. Please advise.
Thanks much and best regards,
Dehong
This is much more efficient than updating/repainting the whole JList, which
it seems you are trying to do.
Hope this helps,
Bora Eryilmaz
:> Hi Everybody,
:>
Try using a ListModel with the JList. In my experience, that always
results in the displayed list immediately reflecting your changes.
= Steve =
--
Steve W. Jackson
Montgomery, Alabama
Hi,
this should help:
/**
* @author Nuno Ferreira
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Tester extends JFrame
{
private JList list = null;
DefaultListModel lm = null;
public Tester()
{
super("Tester");
initialize();
}
private void initialize()
{
setSize(300, 600);
int i = 0;
lm = new DefaultListModel();
lm.addElement(("value " + i++));
lm.addElement(("value " + i++));
lm.addElement(("value " + i++));
lm.addElement(("value " + i++));
lm.addElement(("value " + i++));
lm.addElement(("value " + i++));
lm.addElement(("value " + i++));
lm.addElement(("value " + i++));
list = new JList(lm);
getContentPane().setLayout(new BorderLayout(5, 5));
getContentPane().add(new JScrollPane(list), BorderLayout.CENTER);
JButton button = new JButton("Delete");
button.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int index = list.getSelectedIndex();
System.out.println("index: " + index);
if (index > -1)
lm.removeElementAt(index);
}
}
);
getContentPane().add(button, BorderLayout.SOUTH);
}
public static void main(String[] args)
{
Tester window = new Tester();
window.setVisible(true);
}
}