Reputation: 79
i have some code that i am trying to recompile and understand, but i have a method that does not compile and i wish to find the good work around. the method is as follows.
private void launchEventPanel(String title) {
EventQueue.invokeLater(new Runnable(title) {
public void run() {
JFrame myFrame = new JFrame();
myFrame.setTitle("Conference Call");
myFrame.setIconImage(CallConference.this.myCore.myPanel.myIconManager.getPromptIcon(CallEMart.class.toString()));
myFrame.getContentPane().add(CallConference.this.myEventPanel, "Center");
myFrame.pack();
myFrame.setVisible(true); } }); }
the second line of the EventQueue.invokeLater does not compile, i get the error "Anonymous class implements interface, cannot have arguments".
any help and work around is highly appreciated. thanks!
Upvotes: 1
Views: 3441
Reputation: 131
You can convert your anonymous class into nested class. It will allow you to pass parameters into constructor.
private static final MyRunnable implements Runnable {
private final String title;
public MyRunnable(String title) {
this.title = title;
}
@Override
public void run() {
// use title here
}
}
Upvotes: 1
Reputation: 38132
Well, as the message says: java.lang.Runnable is an interface so you cannot pass title to its constructor. Use:
EventQueue.invokeLater(new Runnable() {
instead.
Note that title isn't used anywhere. If you need it inside the Runnable, you need to declare it final:
private void launchEventPanel(final String title) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame myFrame = new JFrame();
myFrame.setTitle(title);
Upvotes: 0
Reputation: 2069
Runnable
is an interface and so it does not contain a constructor which accepts a string, which you are doing in this line: EventQueue.invokeLater(new Runnable(title) {
If you want to use title
in public void run()
, just make title a final argument and you are free to use it within that method.
Upvotes: 0
Reputation: 23465
Well, it is what it says, you can't give arguments to a Runnable
"constructor" because there is no constructor -- it's an interface.
Instead, declare title
final
, and use it directly inside the inner class.
Upvotes: 2
Reputation: 5134
That's because Runnable is just an interface and it doesn't take any argument as an anonymous class. To get around it, you can assign final to the upper parameter:
private void launchEventPanel(final String title) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame myFrame = new JFrame();
myFrame.setTitle("Conference Call");
myFrame.setIconImage(CallConference.this.myCore.myPanel.myIconManager.getPromptIcon(CallEMart.class.toString()));
myFrame.getContentPane().add(CallConference.this.myEventPanel, "Center");
myFrame.pack();
myFrame.setVisible(true); } }); }
Upvotes: 8
Reputation: 49734
private void launchEventPanel(final String title) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame myFrame = new JFrame();
myFrame.setTitle("Conference Call");
myFrame.setIconImage(CallConference.this.myCore.myPanel.myIconManager.getPromptIcon(CallEMart.class.toString()));
myFrame.getContentPane().add(CallConference.this.myEventPanel, "Center");
myFrame.pack();
myFrame.setVisible(true); } }); }
And you're done. Although as far as I can see, you're not actually using the value of title
anywhere in your code.
Upvotes: 1