How to add elements to a JList without preventing refresh
- From: "Anonymous user" <Number_42@xxxxxxxxxxxx>
- Date: 30 Dec 2005 04:11:11 -0800
Hello,
i have a simple problem. Let's say that in some app, i have a loop wich
add a lot of elements to a JList. I CANNOT change the loop, but i can
change how each element is added.
What i would like is that i can scroll the JList with the mouse,
select/unselect, etc... WHILE the elements are added:
1/ Is it possible? (for knwoledge)
2/ Is it possible in a simple manner? (for developping it)
3/ How can that be done?
Here is what i've come to without any success:
=========================================== 8<
import java.awt.BorderLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class Refresh extends JFrame {
private JPanel jContentPane = null;
private JList jList = null;
private JButton jButton = null;
private JScrollPane jScrollPane = null;
public Refresh() {
super();
initialize();
}
private void initialize() {
this.setSize(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(getJContentPane());
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJButton(), BorderLayout.NORTH);
jContentPane.add(getJScrollPane(), BorderLayout.CENTER);
}
return jContentPane;
}
private JList getJList() {
if (jList == null) {
jList = new JList(new DefaultListModel());
}
return jList;
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setText("Add");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
// this CANNOT be changed
for (int i = 0; i < 1000000; i++)
add2(i);
}
});
}
return jButton;
}
/**
* Direct access.
*
* The JList "freezes" while new element are addded.
*/
private void add1(int i) {
((DefaultListModel)getJList().getModel()).addElement(new Integer(i));
}
/**
* invokeLater.
*
* The JList freezes too, and a java.lang.OutOfMemoryError is throwed;
*/
private void add2(int i) {
SwingUtilities.invokeLater(new RunAdd(i));
}
private final class RunAdd implements Runnable {
private int i;
public RunAdd(int i) {
this.i = i;
}
public void run() {
((DefaultListModel)getJList().getModel()).addElement(new
Integer(i));
}
}
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setViewportView(getJList());
}
return jScrollPane;
}
public static void main(String[] args) {
new Refresh().setVisible(true);
}
}
=========================================== 8<
TIA for any help :)
.
- Follow-Ups:
- Re: How to add elements to a JList without preventing refresh
- From: Thomas Hawtin
- Re: How to add elements to a JList without preventing refresh
- Prev by Date: Re: Variablen in Methode nicht zulaessig
- Next by Date: EzHomeTech will develop a US$99 box for java application.
- Previous by thread: polygon shape of jcomponent
- Next by thread: Re: How to add elements to a JList without preventing refresh
- Index(es):