My situation is:
I'm writing software for custom test procedures. If the class with the test is loaded, all the necessary values are given to the test class through it's constructor. The test class dynamically loads a GUI that's been build in NetBeans' WYSIWYG editor. The GUI itself does not contain any more intelligence than getters and setters for the labels and action handlers for when a button is pressed or a menu is loaded. The actual testing happens from the test class, so I can use one standard UI for different tests. There should be feedback from the UI to the test class, and that's where I'm stuck. Since the UI is a child from a JFrame, the UI's super is javax.swing.JFrame - not the test class which dynamically loads it.
I've been searching on how I can let the UI trigger a method in the test class, but I couldn't find an answer. So probably I'm searching for the wrong way.
Please push me the right direction: How can I get it done that if I push a button on the UI, a method from the loader class gets triggered?
Please keep in mind that i can not call the method directly from a class import, since the UI will be called by various classes, depending on the type of test i want to run, so the UI is actually not directly aware of the class that calls it.
[EDIT] - this is example code of what i would like to achieve:
Main class:
package clascomms;
public class ClassComms {
private final ClassCommsUi ui;
public ClassComms() {
ui = new ClassCommsUi("Click1","Click2");
}
public void btn1Clicked(){
// this code should run if button 1 is clicked
}
public void btn2Clicked(){
// this code should run if button 2 is clicked
}
public static void main(String[] args) {
ClassComms thisOne = new ClassComms();
}
}
And the UI:
package clascomms;
public class ClassCommsUi extends javax.swing.JFrame {
public ClassCommsUi(String btn1, String btn2) {
initComponents();
jButton1.setText(btn1);
jButton2.setText(btn2);
this.setVisible(true);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("jButton2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addComponent(jButton1)
.addGap(27, 27, 27)
.addComponent(jButton2)
.addContainerGap(33, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addContainerGap(28, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
// If this action happens, method btn1Clicked() in main Class should be called
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
// If this action happens, method btn2Clicked() in main Class should be called
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
// End of variables declaration
}
What you would need could be something like this:
interface TwoButtonsToClick {
void btn1Clicked();
void btn2Clicked();
}
...
public class ClassComms implements TwoButtonsToClick { private final ClassCommsUi ui;
public ClassComms() {
ui = new ClassCommsUi(this, "Click1","Click2");
}
@Override
void btn1Clicked() {
...
and then:
public class ClassCommsUi extends javax.swing.JFrame {
private TwoButtonsToClick mainClass;
public ClassCommsUi(TwoButtonsToClick mainClass, String btn1, String btn2) {
this.mainClass = mainClass;
initComponents();
jButton1.setText(btn1);
jButton2.setText(btn2);
this.setVisible(true);
}
and now your UI class has an instance of the class to call back.
And note: nothing in your code example relates to dynamic loading of classes. All the things you have shown here are of "static" nature. So far, there is no need for any sort of reflection or other dynamic magic.