• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Accessing current instance of a class using Java Reflection

 
Ranch Hand
Posts: 30
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am looking to dynamically create a context menu without using any mapping as the values will always be random.
The issue I am having is setting submenu groups.
I have decided to go with Java reflection for what I am looking to accomplish, but the issue I am having is I cannot seem to access the Current Object without knowing the actual Object.
The problem is having to know the object makes it loose its dynamic ability. From what I can see the only options end up being new instance or knowing the name. Does anyone know a work around to this?

Any Help would be greatly appreciated. Suggestions advising not to do it this way or use mapping ect are not an option. The menu has to be 100% dynamic without 100 if/else statements.

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can get the value of the Field with f.get(this) -- is that what you were looking for?
 
Shawn Franks
Ranch Hand
Posts: 30
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darryl Burke wrote:You can get the value of the Field with f.get(this) -- is that what you were looking for?



No i am looking to get the actual object for example:

JMenu mymenu = f.getType()

Obviously my example is not correct but it is an idea of what im trying to do. I need the existing instance though not a new instance.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm confused as to what exactly you are trying to do. Field#getType() returns Class and can never return a JMenu. But you evidently know that.

Also, in Java you cannot 'get the actual object' -- you get references (or primitives, of course).

Can you post a small compilable example showing a class on which you might want to 'dynamically create a context menu' and tell us what should be in that context menu?
 
Ranch Hand
Posts: 296
Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shawn Franks wrote:I am looking to dynamically create a context menu without using any mapping as the values will always be random.


What are "values", MenuItems?

Shawn Franks wrote:...the issue I am having is I cannot seem to access the Current Object without knowing the actual Object...


What do you mean by "access the Current Object"? In Runtime, I guess? What is "knowing actual Object"?

Despite the fact that you wrote the whole "poem" in the 1'st post, the problem is still very abstract, and there is no way for us to help you without asking a lot of questions.
 
Shawn Franks
Ranch Hand
Posts: 30
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darryl Burke wrote:I'm confused as to what exactly you are trying to do. Field#getType() returns Class and can never return a JMenu. But you evidently know that.

Also, in Java you cannot 'get the actual object' -- you get references (or primitives, of course).

Can you post a small compilable example showing a class on which you might want to 'dynamically create a context menu' and tell us what should be in that context menu?



I already gave examples above. But basically what i need to do is be able to somehow get the JMenu object and use the add() method on it. I know how to set its value but is there any way to access its methods?
The menu options are being pulled form a live database so there could be up to a 1000 different variations of the menu which is why mapping is out of the question. Also the menus need to change on a daily basis if not more.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shawn Franks wrote:

Darryl Burke wrote:Can you post a small compilable example



I already gave examples above.


But nothing remotely compilable.

Shawn Franks wrote:But basically what i need to do is be able to somehow get the JMenu object and use the add() method on it.


What I said in my first response.

edit But really, I don't see why reflection should be needed at all.
 
Shawn Franks
Ranch Hand
Posts: 30
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darryl Burke wrote:

edit But really, I don't see why reflection should be needed at all.



Do you know a better option for dynamically creating a menu when the menuitems are always changing and there is no known sub menu set? I am open to suggestions. I was using reflection to avoid hundereds of if statements.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exactly do you mean by "the current instance of a class"?

There's only one thing that you could call "the current object", and that's what this refers to - it's the object on which the method that is executing has been called on (if it's not a static method).

But there's no such thing as "the current instance" of an arbitrary class. For example, there's no such thing as "the current instance of the JMenu class".

Is that what you're confused about?
 
surlac surlacovich
Ranch Hand
Posts: 296
Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:there's no such thing as "the current instance of the JMenu class".


It could fit if JMenu was a singleton.
 
You have to be odd to be #1 - Seuss. An odd little ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic