1

My question is about using interface class with graphical user interface in java.

Now i have interface class which have methods like this:

public interface management
{
    public void loginActionPerformed(java.awt.event.ActionEvent evt);
}

then i create a class that implements the interface but this class is written with gui design and has a button that will implement the method login

the problem: the button method is private and cannot be converted to public because iam using the drag and drop to make the design.

how to fix this problem or how to use interface class with gui design?

code of the class that has the design:

public class employee extends javax.swing.JFrame implements management
{
private void loginActionPerformed(java.awt.event.ActionEvent evt) {
   // implementation 
   }
}
MHM
  • 19
  • 10
  • This isn't really something that can be fixed: that method should be `public` because the whole point of an interface is to define what your class can do, so the methods implemented from the interface should be `public`. It's not clear to me why you can't just change the word `private` to `public`. – Kevin Klute Dec 02 '15 at 17:27
  • I'm using netbeans 8.0.2 editor... this program not allow the modifier private to be changed to public and don't know the reason. – MHM Dec 02 '15 at 17:32
  • I'm not familiar with the IDE, but what I would say is that it you need to find a way to get it changed to `public` because the `private` keywords defeats the purpose of the interface. Sorry I can't help with specifics. – Kevin Klute Dec 02 '15 at 17:34

2 Answers2

1

Java doesn't allow private methods in interfaces.

A possible workaround solution, is to use another method name in the interface and delegate in the implementation:

public class Employee extends javax.swing.JFrame implements Management
{
   @Override
   public void myLoginActionPerformed(java.awt.event.ActionEvent evt) {
       loginActionPerformed(evt);
   }

   // @javax.annotation.Generated 
   private void loginActionPerformed(java.awt.event.ActionEvent evt) {
       // implementation 
   }
}

with

public interface Management
{
    void myLoginActionPerformed(java.awt.event.ActionEvent evt);
}
Community
  • 1
  • 1
  • a good solution sir and i appreciate it. but the problem is that the private method is a button i should click on when running my application. so the private method implementation should be implemented directly when clicking the button – MHM Dec 02 '15 at 17:43
  • With the elements you posted I don't see why, maybe you should clarify your question –  Dec 02 '15 at 17:57
  • I provide full details in my question... and i write that the method is a button that should be clicked on... also i provide details and said using interface class with graphical user interface... please review it again – MHM Dec 02 '15 at 17:59
0

Finally i found the solution for my question:

Interface class like this:

public interface Management
{
     public void login(Account a); // object from a class instead of gui frame.
}

The class implemented the interface:

public class Employee extends javax.swing.JFrame implements Management
{
    private void loginActionPerformed(java.awt.event.ActionEvent evt) {
    Account a = new Account();
    login(a); 
    // button method that should clicked on.
   }
}

 public void login(Account a)
 {
     // implementation of what button should do when clicked on.
 }

Special thanks to @RC :)

MHM
  • 19
  • 10