I am looking at some programs online and see that they close with
}
});
}
It looks strange to me to have
});
and not just
}
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
I am looking at some programs online and see that they close with
}
});
}
It looks strange to me to have
});
and not just
}
Please provide an example. I suspect there's more going on there than you've described; more than a program closing but likely a closing to a statement that included the creation of an anonymous inner class or a similar construction.
This is what was written as copied from the tutorial. I am just concerned having to learn this on my own. I an absorbing things bit by bit
thanks for your help
import javax.swing.*; public class FrameExample_Extended{ private static void createAndShowGUI() { //This is to turn on the Default 'Look and Feel'. JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("[=] Hello World [=]"); // To make sure the default operation on pressing the close button on the window-bar // is to exit the program. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // To size the window to fit all the widgets in, without working out the size manually. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
Last edited by helloworld922; September 7th, 2013 at 11:34 AM. Reason: please use [code] tags
That is the end of a statement. Look at the SwingUtilities.invokeLater(...) method in the code.
Is the parameter for the SwingUtilities.invokeLater(...) method. That ); is the closing parentheses for the invokeLater method.new Runnable() { public void run() { createAndShowGUI(); } }
NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:
When asking for help, please follow these guidelines to receive better and more prompt help:
1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
2. Give full details of errors and provide us with as much information about the situation as possible.
3. Give us an example of what the output should look like when done correctly.
Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/
Thanks
How important is this SwingUtilities.invokeLater(...) method
Is it used often in programs or can I get by without ever using it
Thanks for posting the second bit in code or highlight tags.
The first example you posted,
is constructing an anonymous inner class, Runnable, which requires a method run(). If you trace the construction carefully, you'll see the invokeLater() method takes a Runnable argument and the Runnable class is constructed between the open and close parentheses of invokeLater. The semi-colon is necessary at the end to end the statement that starts with SwingUtilities. . . .public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. SwingUtilities.invokeLater( new Runnable() { public void run() { createAndShowGUI(); } } ); }
Using SwingUtilities.invokeLater() runs or starts the Swing application on the Event Dispatch Thread, or EDT, and yes, it's important. There is more than one way to accomplish the same thing, but learn this way, memorize it, and use it. There are many Swing tutorials on the web that are bad examples because they don't launch their apps this way. It's wrong, it's bad, and now you know better. You can learn more about why it's important by searching and reading up on "Concurrency in Swing," and "Swing on the EDT," and similar searches.
Thanks
The whole thing seems a bit advanced to me but when I go into it further I will remember this advice
The ; just ends a statement. For examples:
frame.pack(); frame.setVisible(true);
frame.pack() has no parameters.
frame.setVisible(true) takes one parameter, a boolean, in this example, true.
SwingUtilities.invokeLater takes one parameter, a Runnable object. Rather than passing such an object in, there is one being created on the spot. This is where all of the {} come in to play.
Consider this:
Now this:SwingUtilities.invokeLater(MyRunnable);//This assumes MyRunnable implements Runnable.
See how the ); still ends the statement as in the above examples. The problem here is that the Runnable interface requires the implementation of a method with the signature: public void run()SwingUtilities.invokeLater(new Runnable() { });
Which leads to this:
So in the end it is really not a }); but the } closes off the implementation of the new Runnable, the ) closes off the end of the method invokeLater and the ; closes off the statement.SwingUtilities.invokeLater(new Runnable() { public void run() { //the statements to be executed } });
Thanks again for this comprehensive reply I will study it as soon as I get a chance
However how does MyRunnable implements Runnable. is it like a function within a function
As soon as I think it through I will probably ask the question in a different way
MyRunnable is just a theoretical class that could exist. Created to show how invokeLater(); is just another method call, just another statement, and has to end with a ;
The idea to grab is that the parameter being passed to the invokeLater method is an anonymous class defined inside the parentheses of the method, resulting in the odd looking series of });
Some reading on the subject
The others provided better answers, but here's the short version... Any time you see {, (, or [, there must be an accompanying }, ), or ] and vice-versa. Each of the first three opens some sort of element, and the last three close that element. If you open something but don't close it, or try to close something that isn't opened, the program just doesn't compile at all, full stop. And the closings have to come in the reverse order of the openings. So if you see {(, you need to close it with )}.}
});
}
It looks strange to me to have
});
and not just
}
It's kind of like those Russian nesting dolls. If you're putting away the whole set, you need to close each and every one of them. You can't put the small and large dolls away, then forget about the medium one. Further, you can't put away the small one, then the large one, then the medium one. You have to pack them away the same way you unpacked them.