how to access the jbutton that is in the other class?

Asked

Viewed 754 times

-4

I got the jbutton in the main class, so I put him in the public room so I could access from another class. but you’re not doing the right thing

public class piscarbt{

 tela t = new tela();
public void piscar(){

    t.jButton1.setBackground(Color.white);

}


}

esse a foto do código fonte do botão, ele está no netbeans

I made this code and the color of jbutton does not change, remembering that in the main class it ispublic javax.swing.JButton jButton1;

I don’t know what to do

  • It’s not very easy to do, but it’s not impossible to learn. See http://stackoverflow.com/questions/7115672/change-jbutton-gradient-color-but-only-for-one-button-not-all?lq=1

2 answers

0

You can leave the button as static:

Private Static JButton btnBotão;

And create a statistical method to call this button:

public static JButton getButton(){ return btnBotao; }

all this in the main class.

In the class you call this button you can call by the statistical method:

Principal.getButton().addActionListener() .... anyway, implements as you wish

0

Get a button in the main class:

public JButton getBotao() {
return seuBotao;
}

In the other instance class the main:

ClassePrincipal controle = new ClassePrincipal():

And your code will stay that way:

JButton botao = controle.getBotao();
t.botao.setBackground(Color.white);

Try it and give feedback

  • That alone will not solve, depending on what LAF is being applied. For whoever colors the button is the Uimanager, and that method alone does not change the bakcground.

  • what I must do diego

  • @Diogocipriano I posted a link with an example, maybe if you adapt, meet.

  • I managed to change his color but there’s one but I have him change it twice using thread and he only changes the last one every time

Browser other questions tagged

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