• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question regarding lambda expression inside constructor  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would the event class look like given what can be seen in the picture and how would the functional interface be implemented in order to make it work? (This code is supposed to simulate fetching an array of employees/offices/services from a server, server class works fine)
Thanks in advance.
Screenshot1.jpg
[Thumbnail for Screenshot1.jpg]
 
Author
Ranch Hand
Posts: 157
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Remy,

Not sure someone is willing to debug an image! To help us help you, you might post a self-contained example in code (instead of a partial graphic image), so that we can try to run it an see why it is not working.
 
Joe Riviera
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first event is supposed to simulate what I mentioned in the OP and the second one is supposed to modify a widget title, but that's not the main issue here.
 
Joe Riviera
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not running because I have no idea what parameters the Event constructor is supposed to take and I cannot figure out what the lambda expression is supposed to mean (The code is a sample of the main method that was written by someone else)
 
Pierre-Yves Saumont
Author
Ranch Hand
Posts: 157
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You posted what seems to be a screen copy of an IDE showing errors. We don't know what these errors are. We would probably know by simply putting the pointer over an error, which we can't do with an image.

We can guess that either the parameters of the addEvent method or the parameters of the Event constructor are not correct. We could verify that by looking at the addEvent method implementation and/or at the Event constructor. Again not possible with an image.

Your IDE should be able to show you the implementation of the addEvent method and the Event constructor. We can't get it from the image you sent.

So either you can have access to the addEvent implementation and/or the Event constructor, or you might look at the API documentation for these.

Or at least, tell us where the addEvent method and the Event class come from.
 
Joe Riviera
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The addEvent is in the class itself and I do know that the error is from the Event contructor. The addEvent method takes an Event as a parameter which is quite obvious.

However, I'm at a loss as to what the Event constructor should look like and the error message isn't helping much:

"The contructor Event(int (<no type> client) -> {}) is undefined."

The error might make sense to you if you're familiar with lambda expression but for me, I genuinely have to idea how to set up my Event constructor and the error is not helping much.

Thanks in advance.
 
Pierre-Yves Saumont
Author
Ranch Hand
Posts: 157
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to access the code for the Event constructor and post the signature here. Might be something like Ctrl+click on "Event" in "new Event(...";

This should display the code of the constructor. You need only the signature, which will be something like public Event(int param1, FunctionalInterface param2).

The names of the parameter are irrelevant. "FunctionalInterface" should be replaced by the name of a functional interface. Ctrl+click on this name and post what you find.
 
Joe Riviera
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I did is add an ActionListener interface to the Event class and this interface includes 2 methods, what I'm unable to do is implement the functional interfaces in a way that the addEvent method works differently each time. Any help is appreciated.
Screenshot_1.jpg
[Thumbnail for Screenshot_1.jpg]
 
Pierre-Yves Saumont
Author
Ranch Hand
Posts: 157
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you added an ActionListener parameter to the Event constructor, and you write:



the reason why it does not compile is that the type of the client parameter is ActionEvent, because the interface is:



So you should change the parameter name into:



Now you see that you can only call methods of the ActionEvent class. clickMenuItem is not one of them.

You might also see how difficult it is to help you if you don't post the code.
 
Joe Riviera
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given that I would like to keep the signature of the addEvent method intact, what would be the best way to change the Event class for clickMenuItem and editWidgetTitle to work, preferably using 2 different functional interfaces? Thanks.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!