0

i have a problem in changing the icon of a JToggleButton at runtime without clicking this latter or changing its state i've tried to do it with static method but it doesn't change i just wanna know is there a way to do so in java???

this the server

public class PartsServer {

public static void main(String[] args) {
    try {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.socket().bind(new InetSocketAddress(6000));
        serverSocketChannel.setOption(SO_RCVBUF, 100);
        boolean running = true;
        while (running) {
            SocketChannel socketChannel
                    = serverSocketChannel.accept();
            new Thread(new ClientHandler(socketChannel)).start();
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }        
}

}

this class handles the clients and it includes the method that change the JToggleButton

package memory;

import java.nio.channels.SocketChannel;

public class ClientHandler implements Runnable{

private final SocketChannel socketChannel;

public ClientHandler(SocketChannel socketChannel) {
    this.socketChannel = socketChannel;
}

public void run() {
    String partName;
    while (true) {
        System.out.println("ani hna");
        partName = HelperMethods.receiveMessage(socketChannel);
        if (partName.equalsIgnoreCase("quit")) {
            break;
        } else {
            MainApp.changeToggleButtonIcon();
        }
    }
}}

and in final here's the static method i call to change the icon that's situated in MainApp

public static void changeToggleButtonIcon(){
toggleButton.setIcon(new ImageIcon("C:\\Users\\PC-
         HOME\\Desktop\\design\\Speech Bubble-50.png"));

}

Karim
  • 637
  • 1
  • 5
  • 13
  • Yes, there is a way. You just call setIcon() on the button, with the new icon as argument. We can't tell why your code is wrong without seeing it. – JB Nizet May 13 '17 at 15:12
  • i did the same thing as You say but i still can't figure out why it doesn't change `toggleButton.setIcon(new ImageIcon("C:\\Users\\PC-HOME\\Desktop\\design\\Speech Bubble-50.png"));` – Karim May 13 '17 at 15:16
  • 1
    We can't tell why your code is wrong without seeing it. – JB Nizet May 13 '17 at 15:18
  • i'll clarify it to You i'm using socket channels when the server receives a message its processes it and at that time it change the icon of the JToggleButton hold on i'll post the code – Karim May 13 '17 at 15:33
  • 1
    Edit your question. Don't post code in comments. – JB Nizet May 13 '17 at 15:33
  • i edited it i hope it'll help You and thank You in advance – Karim May 13 '17 at 15:49
  • 1
    Your server never creates nor display any Swing frame. So how could it change the icon of a checkbox? The frame is most probably created by the client, which connects to your server, and runs in another JVM, possibly on another machine 20,000 kilometers away. – JB Nizet May 13 '17 at 15:52
  • thank You @JBNizet i've found the problem thank Tou for Your time – Karim May 13 '17 at 16:56

0 Answers0