'Handling button event from another class in Jframe

I am using netbeans to make a GUI. To make it simple, let say what I want to do is that when I press a button it just opens a new JFrame and print a text on a JPanel, a text entered by the user.

It sounds easy if I just complete the automatically generated function from Netbeans :

private void popUpButtonActionPerformed(java.awt.event.ActionEvent evt) {
    // I added these three lines                                         
    JFrame newFrame = new myPopUpWindow();
    myPopupWindow.getTextLabel().setText("Hello" + enteredText.getText());
    this.dispose();
}

And everything works as I want. Now what I want to do is having a cleaner code. I want to put these two line in a class ButtonHandler.java in a function handleHelloPopUp(String enteredStringText) Now comes the dilemma:

  • If I make handleHelloPopUp static, I get some error when I access some non static variable
  • If I don't make handleHelloPopUp I just have to pass it as argument in my Jframe -> horrible code structure

What is the best way to do this ? Please help ...



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source