1

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!

1
  • instead of myFrame.setTitle("Conference Call"); Apr 5, 2012 at 18:00

6 Answers 6

8

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); } }); }
0
2

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.

1
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.

1

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
    }

}    
0

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); 
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.