Home » Newcomers » Newcomers » Buttons lose function when detail of action event changed(Action event handler decoupled from action listener)
Buttons lose function when detail of action event changed [message #1787100] |
Fri, 18 May 2018 18:14 |
Patrick Moran Messages: 141 Registered: March 2018 |
Senior Member |
|
|
I wanted to change a large number of JTextFields that are filled in response to JButtons. When I changed a few details, all of the buttons lost their functions. Is there any way to prevent this from happening:
The original was:
JButton btnSave = new JButton("Save");
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
try
{
//String query = "select * from EmployeeInfo";
String query = "insert into EmployeeInfo (EID, Name, Surname, Password, Age) values(?, ?, ?, ?, ?)";
PreparedStatement pst = connection.prepareStatement(query);
pst.setString(1, textFieldEID.getText());
pst.setString(2, textFieldName.getText());
pst.setString(3, textFieldSurname.getText());
pst.setString(4, textFieldPassword.getText());
pst.setString(5, textFieldAge.getText());
pst.execute();
// ResultSet rs = pst.executeQuery();
JOptionPane.showMessageDialog(null, "Data saved");
pst.close();
// rs.close();
}
catch (Exception e1)
{
JOptionPane.showMessageDialog(null, "Data save failed");
}
refreshTable();
}
});
btnSave.setBounds(64, 80, 117, 29);
contentPane.add(btnSave);
Change the above to the following to the following and all function disappears. The textbox is still visible when the program runs, but the button does nothing.
JButton btnSave = new JButton("Save");
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
try
{
//String query = "select * from TM";
String query = "insert into TM (EID, BKG1, QUES1, ANSW1, RESP1 , ImageID) values(?, ?, ?, ?, ?)";
PreparedStatement pst = connection.prepareStatement(query);
pst.setString(1, textFieldEID.getText());
pst.setString(2, textFieldBKG1.getText());
pst.setString(3, textFieldSurname.getText());
pst.setString(4, textFieldPassword.getText());
pst.setString(5, textFieldAge.getText());
pst.setString(6, textFieldImageID.getText());
pst.execute();
// ResultSet rs = pst.executeQuery();
JOptionPane.showMessageDialog(null, "Data saved");
pst.close();
// rs.close();
}
catch (Exception e1)
{
JOptionPane.showMessageDialog(null, "Data save failed");
}
refreshTable();
}
});
btnSave.setBounds(64, 80, 117, 29);
contentPane.add(btnSave);
With lots of screen elements to replace and recode, I've wasted days of work. How can I prevent this unwanted change from happening?
|
|
| | |
Goto Forum:
Current Time: Sat Jan 25 10:27:41 GMT 2025
Powered by FUDForum. Page generated in 0.07803 seconds
|