How to access jTextField statico created with swing?

Asked

Viewed 437 times

6

So guys I got one jTextField that works normally but when I put it as static the setText no longer works, and I need it static because I pass the same to a function, I did it without the swing and it worked I don’t know what’s going on now.

Below is the code:

//Codigo gerado automaticamento pelo swing

public static javax.swing.JTextField blueBGolem;

blueBGolem.setEditable(false);
blueBGolem.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
blueBGolem.setForeground(new java.awt.Color(255, 255, 255));
blueBGolem.setHorizontalAlignment(javax.swing.JTextField.CENTER);
blueBGolem.setText("5:00");
blueBGolem.setBorder(null);
blueBGolem.setOpaque(false);
Painel.add(blueBGolem);
blueBGolem.setBounds(0, 0, 70, 17);


private void blueBGolemStartActionPerformed(java.awt.event.ActionEvent evt) {                                                
    //timerBlueBGolem.start();
    blueBGolem.setText("ooi");
} 

EDIT

I think it’s something in my code but I can’t find what..

follows the complete code

http://pastebin.com/Rat9QA9j

  • What is the error message? I don’t think using Static is the best solution to the problem. Specify better what you are trying to do.

  • @Jodsonleandro There is no error message simply by clicking on jbutton nothing happens

  • @Jodsonleandro I need to pass my jtextfield to a function where the same will be changed and to do this he has to be static

  • @alleen94 It does not need to be static. If you pass the object by parameter in the method, you will get the instance. I don’t remember in which structure the swing was made, but you can validate if your Jtextfield instance is always the same.

  • @Jodsonleandro the netbeans won’t let me pass if the object is not static

2 answers

3

The problem is that your variables are not static, just put as static that solves your problem.

If you are using Netbeans and the "drag components" option configures as below:

NetBeans

If manually making the components add Static to the variables:

public javax.swing.JTextField blueBGolem; // anterior
public static javax.swing.JTextField blueBGolem; // novo

2

You don’t need to make your Jtextfield static. What you mention about passing a Jtextfield as an argument to a method can be done without Jtextfield being static unless the target method is a static method, such as public Static void main(String[] args).

If you are using Jtextfield within the main method, try declaring Jtextfield only within the main method instead of declaring it as the class instance variable.

Browser other questions tagged

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