Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

actionlistener function  RSS feed

 
Martin Biastoch
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i thing i have to problems with the following code:

1. iCount must be final, but it comes from an for-queue
2. how can execute the function in sFunction[1]
thx for help Martin
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) You can declare a new variable and initialize it to iCOunt's value --

2) You can't. With a huge amount of effort, you could write code that put the string into a .java file as the body of a function in a new class, compile that class by running a Java compiler, load that class, and call the function -- and there would still be all sorts of edge cases that would make this hard to do (handling exceptions, for example.) Java is not a scripting language, and there's no built-in equivalence between text strings and executable code.
What you could do is make sFunctions an array of (for example) objects that implement the Runnable interface; then you could initialize it like this:

Then your ActionListener would look like

But of course, I don't know what you're trying to do, exactly. If you really need to execute arbitrary strings of code, you're much better of using a dynamic language like Ruby.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest's array of runnables might lead you to look at the command pattern. That's a neat way to encapsulate an action - which could be your System.exit(0) - into an object. Here is a little paper You would apply it just as Ernest did - in fact you could say he used runnable as a base command class. Lemme know if that helped.
 
Martin Biastoch
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thx, that work fine.
more information:
i will use it to build a navigationbar
the informations for the bar (button/icon image,label and functions) in a file
now every user can build it "own" navigationbar
i put all functions in a hashtable
the user must write the functionname in the file
(is much better than writing the functioncall in the file)

[ August 01, 2003: Message edited by: Martin Biastoch ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Runnable as your base command might be really cool. It gets all the busy work done by commands off the Swing thread so the UI will remain responsive while they curn away. My current app has some functions built on a vendor framework (not our fault!) that run on the Swing thread and interfere with the UI.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!