I have a problem with java Window Builder application.
I need to make this application to save the name (textField) into an array variable from another class.
So, when you press the New Name button should pass the Name from the textField, and let you introduce another one and pass this new one in the array next position and so on.
After that, the names from array must be shown into an textArea
The problem is that when I press the button, instead of pass the textField text into array, I get a lot of swing err. If I change the "n" variable from String[] into String, it don't give me any error, but it only save one name.
DATA Class
public class data {
String[] n;
public void saveData(String na, int counter){
n[counter]=na;
}
public void showData(int counter){
for (int i = 0; i < counter; i++) {
System.out.println(n[counter]);
}
}
}
Window Class
import java.awt.EventQueue;
public class dataWindow {
// Data OBJECT
data d = new data();
private JFrame frame;
private JTextField textName;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
dataWindow window = new dataWindow();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public dataWindow() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lblName = new JLabel("Name");
textName = new JTextField();
textName.setColumns(10);
JButton btnNewName = new JButton("New Name");
btnNewName.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String n;
int c=0;
// HERE I GET THE TEXT AND PASS IT TO THE ARRAY IN DATA CLASS
// here I think that I get the error when I press the "New Name" button
n=textName.getText();
d.saveData(n, c);
textName.setText("");
c++;
}
});
GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(44)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(btnNewName)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(lblName)
.addGap(18)
.addComponent(textName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addContainerGap(259, Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(72)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(lblName)
.addComponent(textName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED, 114, Short.MAX_VALUE)
.addComponent(btnNewName)
.addGap(33))
);
frame.getContentPane().setLayout(groupLayout);
}
}
Can someone please help me? Thanks in advance.