Modification of variables passed by parameters

Asked

Viewed 107 times

0

It is possible to modify a button passed by parameter?

I have a main class and a secondary school...

In the main class I prompt one of the secondary by passing a string and a button.

Secundaria s = new Secundaria (String s, Botao B);

In high school I have to paint this B button. I can’t do this in the main class, I would have to modify the main button in high school. Has as?

I tried to declare an auxiliary button in secondary school which gets the B button, and paint this auxiliary, but it didn’t work.

In my main class I have this:

PingThread reitoria = new PingThread ("200.132.148.6",d.jButton3);
   reitoria.start();  

And in my class I test if a server is online, I want to paint the button, I use this:

public class PingThread  extends java.lang.Thread  {

public  JButton aux;   
String ip;
public  JFramePrincipal d = new JFramePrincipal();

 public PingThread( String ip, JButton B) { // ip do servidor e botão correspondente 
    this.ip = ip; 
    aux = B;
 }

 @Override
 public void run() {

        Ping p = new Ping();
        Reitoria R = new Reitoria();

        p.runSystemCommand("ping " +ip);
    if (p.retorno == true) { // Servidor Online
        d.setVisible(true);
        aux.setBackground(Color.red);
        System.out.println(p.retorno); // true if online
    }
    else {
        System.out.println(p.retorno); // false else
    }
 } //fim run()

 } //fim classe

In case I wanted to paint the B button I get in the Pingthread class constructor.

  • Show what you’re doing. It’s like, it depends on how you do it. But it may be that the way you do it doesn’t meet your need. You must [Edit] the question and put the code you are using.

  • And are you giving any problems with what you’re doing? You just want to know if it’s possible to modify the members of B? If it is, yes, it is possible, just try to modify to see.

  • the boot is not painting. Yes, I just wanted to paint the parameter that I pass. But it can’t be accessed directly because I have to paint 12 individual buttons. Pass the button I want, and depending on the answer I paint it I tried to use aux.setBackground(Color.red); but this paints the auxiliary. would have to paint the builder’s

  • But it’s supposed to be the same. When you do aux = B you’re not cloning the object, you’re using another variable to access the same object. Maybe the problem lies elsewhere, after all in the demonstrated code does not do what you claim to be doing.

1 answer

1

You may not be observing the expected result simply by trying to access windows from a secondary thread, and this is illegal.

You shouldn’t try to change the appearance of a button from another thread. You need to synchronize this other thread first, so the button painting will actually occur on the main thread. Try using the method Synchronized, thus:

 @Override
 public void run() {

        Ping p = new Ping();
        Reitoria R = new Reitoria();

        p.runSystemCommand("ping " +ip);
    if (p.retorno == true) { // Servidor Online
        synchronized(this) {
            d.setVisible(true);
            aux.setBackground(Color.red);
            System.out.println(p.retorno); // true if online
        }
    }
    else {
        System.out.println(p.retorno); // false else
    }
 }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.