Try calling:
IPList.updateUI();
You might also want to do this:
SwingUtilities.invokeLater
public void run() {
//Put your code which modifies the
//GUI right here...
}
}
Main Topics
Browse All TopicsI have thefollowing code that changes the items of a JList one by one. I would like the JList to show the changes as soon as they occur but my code can't make this happen. All I get is all of the changes appearing at once, instead of each item being changed one by one ...
for (int i = 0; i < numberOfClients; i++) {
address = (String)IPList.getModel().
try {
uc = new UploadClient(address, 1500, "c:\\clientDir");
uc.synchronize(); //this method blocks
listValues.setElementAt(ne
}
catch (Exception ex) {
listValues.setElementAt(ne
ex.printStackTrace();
}
IPList.setListData(listVal
IPList.invalidate();
IPList.validate();
IPList.repaint();
this.repaint();
this.invalidate();
this.validate();
}
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Here's the main GUI class
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Frame1 extends JFrame {
DefaultListModel listModel = new DefaultListModel();
TextField ip4 = new TextField();
TextField ip3 = new TextField();
TextField ip2 = new TextField();
TextField ip1 = new TextField();
JButton addButton = new JButton();
JButton deleteButton = new JButton();
JList IPList = new JList();
JLabel jLabel4 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel5 = new JLabel();
JPanel jPanel1 = new JPanel();
JButton uploadButton = new JButton();
JScrollPane jScrollPane1 = new JScrollPane();
public Frame1() {
try {
jbInit();
this.setBounds(400,400,500
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.getContentPane().setL
IPList.setModel(listModel)
addButton.addActionListene
public void actionPerformed(ActionEven
addButton_actionPerformed(
}
});
addButton.setText("Add IP");
deleteButton.setText("Dele
deleteButton.addActionList
public void actionPerformed(ActionEven
deleteButton_actionPerform
}
});
deleteButton.setToolTipTex
jLabel4.setText(".");
jLabel3.setText(".");
jLabel2.setText(".");
this.addWindowListener(new
public void windowClosing(WindowEvent e) {
this_windowClosing(e);
}
});
this.setResizable(false);
jLabel5.setText("IP Address to Add:");
jPanel1.setBorder(BorderFa
uploadButton.setText("Star
uploadButton.addActionList
public void actionPerformed(ActionEven
uploadButton_actionPerform
}
});
IPList.setDoubleBuffered(t
this.getContentPane().add(
this.getContentPane().add(
jPanel1.add(ip1);
jPanel1.add(jLabel3);
jPanel1.add(ip2);
jPanel1.add(jLabel2);
jPanel1.add(ip4);
jPanel1.add(jLabel4);
jPanel1.add(ip3);
this.getContentPane().add(
this.getContentPane().add(
this.getContentPane().add(
this.getContentPane().add(
jScrollPane1.getViewport()
}
void addButton_actionPerformed(
if ((ip1.getText().equals("")
(ip3.getText().equals(""))
return;
}
String address = ip1.getText() + "." + ip2.getText() + "." +
ip3.getText() + "." + ip4.getText();
listModel.insertElementAt(
}
public static void main(String[] args) {
Frame1 f = new Frame1();
f.setVisible(true);
}
void this_windowClosing(WindowE
System.exit(0);
}
void deleteButton_actionPerform
if (IPList.isSelectionEmpty()
int selectedIndex = IPList.getSelectedIndex();
listModel.removeElementAt(
}
void uploadButton_actionPerform
UploadClient uc;
int numberOfClients = IPList.getModel().getSize(
String address;
DefaultListModel model = (DefaultListModel)IPList.g
for (int i = 0; i < numberOfClients; i++) {
address = (String)listModel.getEleme
try {
uc = new UploadClient(address, 1500, "c:\\clientDir");
uc.synchronize();
model.set(i,new String(address + " uploaded"));
}
catch (Exception ex) {
model.set(i,new String(address + " error"));
ex.printStackTrace();
}
}
}
}
Here's the other class that is needed
import java.io.*;
import java.net.*;
import java.util.*;
public class UploadClient {
final boolean debug = true;
final int BUFFLEN = 64 * 1024;
final String USERNAME = "devi";
final String PASSWORD = "devi";
int port;
Socket soc;
String ipAdd;
String syncDir;
BufferedReader in;
BufferedOutputStream bos;
public UploadClient(String ipAdd, int port, String syncDir) {
this.ipAdd = ipAdd;
this.port = port;
this.syncDir = syncDir;
}
public void synchronize() throws IOException {
initiateSession();
}
public boolean initiateSession() throws IOException {
soc = new Socket(ipAdd, port);
in = new BufferedReader(new InputStreamReader(soc.getI
OutputStream os = soc.getOutputStream();
bos = new BufferedOutputStream(os);
String serverReply = null;
serverReply = in.readLine();
serverReply = in.readLine();
writeLine(USERNAME);
serverReply = in.readLine();
writeLine(PASSWORD);
serverReply = in.readLine();
boolean isAuthorized = serverReply.equals("LOGGED
if (!isAuthorized) {
soc.close();
}
return isAuthorized;
}
public void writeLine(String line) throws IOException {
byte ln[] = line.getBytes();
bos.write(ln, 0, ln.length);
bos.write(13);
bos.flush();
}
}
How to use:
- compile and run Frame1.java
- add some ip addresses using the textfields (correctness in ot needed)
- click on add to add an IP to the JList
- after adding a few IPs click on Start
- this will generate a lot of error which will print to the console
- after all ip addresses have been done, each IP address will be appended the String "error"
As you will see all List items get updated/appended at the same time, not one by one ...
None of the proposed answers or comments solved my problem. I posted the code as requested but no new answers have come forth.
I finally figured out that the reason the List wasn't being updated was that my for loop was inside the event thread. Any update to the List would only be handled after I finished processing the current event.
The workaround was to put the list updating code into a timer and calling that time in my event processing code.
Asking for question be deleted
Business Accounts
Answer for Membership
by: heyhey_Posted on 2000-10-11 at 10:51:41ID: 4680323
use DefaultListModel and , index)
model.setElementAt(newData