Reputation: 813
I have a problem with my application. The label in my GUI is supposed to update and show a message that is received from the server.
Sending the message from server to client is working fine. Also receiving the message is working fine. The problem is showing the message in the label.
For updating the label I'm using SwingUtilities.invokeLater(new Runnable()). The GUI does not show att all, not if I add a button or anything else. BUT, if I remove the line rec.getMessage(); in main the GUI shows.
What am I missing?
Class Sender:
public class Sender {
private String address = "225.200.200.200";
private int port = 10002;
String message = "";
public Sender(String msg) {
message = msg;
send();
}
public void send() {
MulticastSocket socket = null;
InetAddress group = null;
try {
group = InetAddress.getByName(address);
socket = new MulticastSocket();
socket.joinGroup(group);
socket.setTimeToLive(1);
}
catch (IOException e) {
System.err.println("Error" + e.getMessage());
System.exit(0);
}
while(true) {
try {
DatagramPacket packet = new DatagramPacket(message.getBytes(), message.getBytes().length, group, port);
socket.send(packet);
}
catch (IOException e) {
System.err.println("Error" + e.getMessage());
}
}
public static void main(String[] args) {
new Sender("hello");
}
}
In class Receiver:
public class Receiver extends JFrame {
private String address = "225.200.200.200";
private int port = 10002;
private JLabel label;
public Receiver() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(320, 150);
setLocationRelativeTo(null);
label = new JLabel("Message");
expressionLabel.setHorizontalAlignment(JLabel.CENTER);
add(expressionLabel, BorderLayout.CENTER);
setVisible(true);
}
public void getMessage() {
MulticastSocket socket = null;
InetAddress group = null;
try {
group = InetAddress.getByName(address);
socket = new MulticastSocket(port);
socket.setSoTimeout(100000);
socket.joinGroup(group);
}
catch (IOException e) {
System.err.println("Error" + e.getMessage());
System.exit(0);
}
byte[] data = new byte[1024];
while (true) {
DatagramPacket packet = new DatagramPacket(data, data.length);
socket.receive(packet);
String message = new String(data, 0, packet.getLength());
displayMessage(message);
}
try {
socket.leaveGroup(group);
socket.close();
}
catch (IOException iofel) {}
}
private void displayMessage(String message) {
System.out.println(message); //THIS MESSAGE SHOWS
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
label.setText(message); //LABEL WON'T UPDATE
System.out.println(message); //THIS MESSAGE WON'T SHOW
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Receiver rec = new Receiver();
rec.getMessage();
}
});
}
}
Upvotes: 0
Views: 95
Reputation: 347332
GetMeasage is blocking the event dispatching thread, prevent it from processing new events from the event queue, including paint requests
Don't perform any action in the event dispatching thread which is blocking or long running. Instead, consider using a SwingWorkwr, especially if you need to be able to update the UI in response to some change that the background thread has caused
See Currency in Swing for more details
Upvotes: 1