new Java programmer here so I might have a basic question here. I am making an array of JTextFields. I want to use setText outside of the class in a public method but it does not work. However, I am able to use setText in the class (not in a method). I am not sure why. Here is some code as a SSCCE to show what I am experiencing. import java.awt.BorderLayout; import javax.swing.*;
public class testSetText extends JFrame
{
private JPanel panel;
private JTextField[] arrayField;
private JTextField singleField;
public testSetText()
{
// Specify an action for close button
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Make a panel
panel = new JPanel();
// Make array of JTextField components
JTextField[] arrayField = new JTextField[2];
for (int i = 0; i < 2; i++)
{
arrayField[i] = new JTextField(10);
arrayField[i].setEditable(false);
arrayField[i].setText("<>!");
panel.add(arrayField[i]);
}
// Make just one JTextField component
singleField = new JTextField(10);
singleField.setText("Works here");
panel.add(singleField);
// Add to panel to frame
add(panel);
// Pack the contents of the window and display it
pack();
setVisible(true);
// This will work!
arrayField[0].setText("Array index in class");
// This won't? Why?
setInMethodWithArray();
// Is this a problem with JTextField itself? Let me try a single element
setInMethodWithSingleElement();
// Hmmm so an element in an array of JTextFields can be addressed with setText in a class but not
// in a method in same class with same statement. But a single JTextField can be used in a method
// by that same class. Why do arrays behave so differently?
// EDIT: So I misunderstood, it does not work with a non array as well either!!
}
public void setInMethodWithArray()
{
arrayField[1].setText("This text will never show up");
}
public void setInMethodWithSingleElement()
{
//singleField.setText("Works here as non-array"); <--- before edit
singleField.setText("nevermind, it does not work here either");
}
public static void main(String[] args)
{
new testSetText();
}
}
you should declare arrayField[]
in class area so array Field is accessible from setInMethodWithArray()
method.
JTextField[] arrayField = new JTextField[2]; //class area
in constructor initial it
arrayField[i]= new JTextField(10);
arrayField[i].setEditable(false);
arrayField[0].setText("Array index in class");
because arrayField in the constructor scope ..and you are accessing arrayField Within constructor .so it works..
This won't? Why?
setInMethodWithArray();
because method setInMethodWithArray()
can't access arrayField[]
because it's not in method scope or in class scope.that's because you have declare it in the constructor so after constructor code block execute arrayField
doesn't exist .it's reference lost because it's local variable
..
public void setInMethodWithArray()
{
arrayField[1].setText("This text will never show up");
}
now set can access arratField[] so now your code will work
import java.awt.BorderLayout;
import javax.swing.*;
public class testSetText extends JFrame
{
private JPanel panel;
private JTextField singleField;
// Make array of JTextField components
private JTextField[] arrayField = new JTextField[2];
public testSetText()
{
// Specify an action for close button
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Make a panel
panel = new JPanel();
for (int i = 0; i < 2; i++)
{
arrayField[i] = new JTextField(10);
arrayField[i].setEditable(false);
arrayField[i].setText("<>!");
panel.add(arrayField[i]);
}
// Make just one JTextField component
singleField = new JTextField(10);
singleField.setText("Works here");
panel.add(singleField);
// Add to panel to frame
add(panel);
// Pack the contents of the window and display it
pack();
setVisible(true);
// This will work!
arrayField[0].setText("Array index in class");
// This won't? Why?
setInMethodWithArray();
// Is this a problem with JTextField itself? Let me try a single element
setInMethodWithArray();
// Hmmm so an element in an array of JTextFields can be addressed with setText in a class but not
// in a method in same class with same statement. But a single JTextField can be used in a method
// by that same class. Why do arrays behave so differently?
}
public void setInMethodWithArray()
{
arrayField[1].setText("This text will never show up");
}
public void setInMethodWithSingleElement()
{
singleField.setText("Works here as non-array");
}
public static void main(String[] args)
{
new testSetText();
}
}
output>>