Hi. I'm new with Java's GUI Components and I'm having a hard time with my program
design right now. My program deals with manipulating database elements (i.e. fields
and records) and I'm stuck with asking the user to add a new record into the
database. My program originally works in the command line interface but when I
redesigned it to GUI, I got stuck since I'm not that familiar with the implementation.
The program initially does not know how many fields there is in the database. It
first asks the user to enter a textfile before calculating it. So when the file
is read and when the user clicks on the add button, a new frame will appear, asking
for new field values per field. I already had set up the frame but my problem is
how can I get the user-inputted values from the textFields and place them into
my database table? I always get null field values.. My problem lies somewhere
in this part of the code:
int n = dbHeaders.size();
values = new String[dbHeaders.size()];
addFrame=new JFrame("Add new record");
addFrame.setLayout(new BorderLayout());
addFrame.setSize(400,100);
addFrame.setVisible(true);
xPanel=new JPanel();
xPanel.setLayout(new GridLayout(n,1,4,4));
for(int i=0; i<dbHeaders.size();i++){
labels=new JLabel("Please enter "+dbHeaders.get(i)+": ");
fields=new JTextField(15);
xPanel.add(labels);
xPanel.add(fields);
values[i]=fields.getText();//something's wrong here...
addFrame.add(xPanel,BorderLayout.NORTH);
}
yPanel=new JPanel();
yPanel.setLayout(new FlowLayout());
aOk=new JButton("OK");
yPanel.add(aOk);
aOk.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
for(int j=0; j<values.length;j++){
String string=values[j];
System.out.println("fields: "+string);
//when i check in this part, string is always null...
}
record = new DBRecord(dbHeaders, values);
dbTable.add(record);
JOptionPane.showMessageDialog(null, "Record added.");
}
});
..And does anybody knows how to implement the "reset" button?
thanks in advance.
sakura