I know that when creating buttons, like next and previous, that the code can be somewhat long to get those buttons to function.
My professor gave us this example to create the next button:
private void jbtnNext_Click() {
JOptionPane.showMessageDialog(null, "Next" ,"Button Pressed",
JOptionPane.INFORMATION_MESSAGE);
try {
if (rset.next()) {
fillTextFields(false);
}else{
//Display result in a dialog box
JOptionPane.showMessageDialog(null, "Not found");
}
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
Though, I do not really understand how that short and simple if statement is what makes the next button function. I see that the fillTextFields(false) uses a boolean value and that you need to initialize that boolean value in the beginning of the code I believe. I had put private fillTextFields boolean = false; but this does not seem to be right…
I’m just hoping someone could explain it better. Thanks 🙂
Well,
fillTextFields(true);is a function call and when you pass in a true/false flag it does some things (you have to see the code inside the function in order to find out exactly what it does).The field declaration
private fillTextFields boolean = false;is invalid, you’re supposed to provide the type before the name, e.g.:private boolean fillTextFields = false;. Aside from the invalid syntax that flag really doesn’t do anything, especially if you’re not using it anywhere.I don’t understand what else you expect to see in the
jbtnNext_Click()method… when you declare your button and it gets clicked on the UI, then this method gets invoked. It doesn’t make the button work, the button works even when you have nothing in thejbtnNext_Click()method. For example:Getting a button to function depends on what you view as a functioning button. What is supposed to happen when you click next/previous?
Update:
Was the
fillTextFieldsmethod given to you somewhere? If it was, then you don’t need to declare anything, much less a variable. If it’s already provided, then you just call the method, that’s all. If it’s not provided then you need to declare it:Otherwise what you see in that function is all you need to do in order to go to the next record in the database.