I have a project in java, and I have a problem.
Description of goal:
In Form A (MainForm) I have some buttons in enable(false) mode.
I have a function LockOrUnlockButtons() that checks some conditions and if this function return true then the buttons make enable(true) state .
I want to call this function for "event of the closing of Form B" (AddCstmrForm).
I tried to solve this goal with receiving this function as parameter:
public Void AddCstmrForm(Runnable myFunc) {
.....
....
}
The problem:
But in Form A (MainForm), when I send the function LockOrUnlockButtons() to the Constructor of Form B (AddCstmrForm) i got Error:
Constructor AddCstmrForm in class AddCstmrForm cannot be applied to given types.
required: no arguments.
founf: Void
What did I do wrong?
Harel
THE CODE:
in the Form A ,(MainForm):
private void buttonAddNewCstmrCrdActionPerformed(java.awt.event.ActionEvent evt) {
AddCstmrForm addCstmr = null;
try
{
addCstmr = new AddCstmrForm(LockOrUnlockButtons());
}
catch (Exception ex)
{
Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
}
addCstmr.show();
}
private Void LockOrUnlockButtons() throws Exception
{
if(sngltn.GetAllCustemers().size()==0)
buttonUpdateAddActivityCstmrCrd.setEnabled(false);
else
buttonUpdateAddActivityCstmrCrd.setEnabled(true);
if(sngltn.GetAllCustemers().size()==0)
buttonDeleteCstmrCrd.setEnabled(false);
else
buttonDeleteCstmrCrd.setEnabled(true);
if(sngltn.GetAllCustemers().size()==0)
buttonQueriesViewData.setEnabled(false);
else
buttonQueriesViewData.setEnabled(true);
return null;
}
*in the Form B(AddCstmrForm): *
public class AddCstmrForm extends javax.swing.JFrame {
...............
.............
.........
private Runnable MainFormLockOrUnlockButton;
........
public Void AddCstmrForm(Runnable myFunc) throws Exception {
initComponents();
..............
..........
......
this.MainFormLockOrUnlockButton = myFunc;
return null;
}
..............
..........
......
}

This isn't a constructor:
public Void AddCstmrForm(Runnable myFunc) throws Exception {
initComponents();
...
}
It's a method called AddCstmrForm with a Void return type.
I think you meant:
public AddCstmrForm(Runnable myFunc) throws Exception {
initComponents();
...
}
You also need to create a Runnable to call your LockOrUnlockButtons method. For example:
AddCstmrForm addCstmr = new AddCstmrForm(new Runnable() {
@Override public void run() {
LockOrUnlockButtons();
}
});
Unless you're using Java 8, in which case you could write:
AddCstmrForm addCstmr = new AddCstmrForm(this::LockOrUnlockButtons);
Additionally, your LockOrUnlockButtons can be simplified significantly:
private void LockOrUnlockButtons() throws Exception {
boolean anyCustomers = !sngltn.GetAllCustemers().isEmpty();
buttonUpdateAddActivityCstmrCrd.setEnabled(anyCustomers);
buttonDeleteCstmrCrd.setEnabled(anyCustomers);
buttonQueriesViewData.setEnabled(anyCustomers);
}
(I'd also strongly advise you to follow Java naming conventions, use a return type of void rather than Void unless you really need to, and avoid throws Exception. You should revisit your exception handling approach in general, by the looks of things.)
See more on this question at Stackoverflow