1
Hello, I am creating a Java project using Eclipse with windowbuilder. Inside this project I have Jframe with the objects, jbuttons, jtextfields, jformattedtextfields, etc... I have all these objects in my project. It turns out that when I try to create a function at the beginning of the code to give actions to these objects, these objects do not appear in the functions. It’s like they don’t exist. I can’t create functions, methods, actions for these objects because they don’t appear in the functions. Does anyone know why this happens... ?
Here are the instantiated objects:
JTextPane PEQuestion = new JTextPane();
PEQuestion.setText("1) Ao fazer compras no supermercado:");
PEQuestion.setBounds(10, 280, 599, 48);
Question.add(PEQuestion);
JTextPane peTextA = new JTextPane();
peTextA.setText("A) Compro tudo que tenho vontade, sem prestar aten\u00E7\u00E3o no pre\u00E7o, na marca ou na embalagem;");
peTextA.setBounds(34, 339, 540, 48);
Question.add(peTextA);
JTextPane peTextB = new JTextPane();
peTextB.setText("B) Uso apenas o pre\u00E7o como crit\u00E9rio de escolha;");
peTextB.setBounds(34, 395, 540, 48);
Question.add(peTextB);
JTextPane peTextC = new JTextPane();
peTextC.setText("C) Presto aten\u00E7\u00E3o se os produtos de uma determinada marca s\u00E3o ligados a alguma empresa que n\u00E3o respeita o meio ambiente ou quest\u00F5es sociais;");
peTextC.setBounds(34, 449, 540, 48);
Question.add(peTextC);
JTextPane peTextD = new JTextPane();
peTextD.setText("D) Procuro considerar pre\u00E7o e qualidade, al\u00E9m de escolher produtos que venham em embalagens recicl\u00E1veis e que respeitem crit\u00E9rios ambientais e sociais.");
peTextD.setBounds(34, 504, 540, 48);
Question.add(peTextD);
And here the functions that I try to create with the objects (at the beginning of the class), and that do not identify the objects:
public void mudarTexto()
{
PEQuestion.setText("2) Entre os alimentos que normalmente você consome, que quantidade é pré-preparada, embalada ou importada?");
peTextA.setText("A) Quase todos;");
peTextB.setText("B) Metade;");
peTextC.setText("C) Um quarto;");
peTextD.setText("D) Muito poucos. A maior parte dos alimentos que consumo não é pré-preparada nem embalada, tem origem orgânica e é produzida na região onde vivo.");
PEImg.setIcon(new ImageIcon(mainView.class.getResource("/peImages/02.png")));
}
The error that appears is:
Pequestion cannot be resolved
peTextA cannot be resolved
peTextB cannot be resolved
peTextC cannot be resolved
peTextD cannot be resolved
Thank you.
They exist only in the scope in which they are instantiated, correct?
– Marcelo Shiniti Uchimura
Why not declare controls in a wider scope, such as the class?
class MinhaJanela extends JPanel { private JTextPane PEQuestion; /* mais abaixo */ public void metodoQueEuQueroTanto() { PEQuestion = new JTextPane(); } /* mais abaixo */ public void metodoQueEuQueroMais() { PEQuestion.setText("minha questão"); } }
– Marcelo Shiniti Uchimura
The objects are within the constructor method and the functions before the constructor method.
– Gabriel Augusto
I did not understand. How so declare in a wider scope?
– Gabriel Augusto
The scope of an if block is the if block itself. The scope of a method is the method itself, including the various ifs blocks that perhaps exist within it. The scope of the class is all methods within the class, and all ifs blocks of all methods.
– Marcelo Shiniti Uchimura