Results 1 to 9 of 9
Thread: question on swing coding?
- 01-05-2013, 04:41 AM #1
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
question on swing coding?
also, I dont quite understand the use of brackets in this coding. Could you also explain that? Thanks in advanced guys.Java Code:import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class Example extends JFrame { public Example() { // what does this do? Why is it necessary? why does it have parentheses? initUI(); // what does this do?also, Why are there parentheses? } public final void initUI() { // what is "final" in this? Why is it final? JPanel panel = new JPanel(); getContentPane().add(panel); panel.setLayout(null); panel.setToolTipText("A Panel container"); JButton button = new JButton("Button"); button.setBounds(100, 60, 100, 30); button.setToolTipText("A button component"); panel.add(button); setTitle("Tooltip"); setSize(300, 200); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { //absolutely no idea what this does... public void run() { Example ex = new Example(); */what does this do? Why is it necessary? I dont understand this.\* ex.setVisible(true); } }); } }Last edited by MW130; 01-05-2013 at 04:49 AM.
- 01-05-2013, 05:23 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Re: question on swing coding?
As you've been advised in your other posts in more words than this - take a step back. I get the feeling you are biting off way more than you can chew - break things down so that you can learn one step at a time. One of your best bets is to start learning from the basics, then build up - every one of your question can be answered in the following website, should you choose to take the time to step through it
The Java™ Tutorials
- 01-06-2013, 02:59 AM #3
Member
- Join Date
- Dec 2012
- Posts
- 74
- Rep Power
- 0
Re: question on swing coding?
I know that the following just creates an object which is a JFrame. A JFrame is Java's class for a window.
All of the other business about Runnable, the run() method and invokeLater() all have to do about Threads.Java Code:public static void main(String[] args) { Example ex = new Example(); // <<<< this creates an Example obect which is a JFrame (aka a window) ex.setVisible(true); }
Here is a link to the documentation of invokeLater()
Here is a link to a tutorial on Threads:
Defining and Starting a Thread (The Java™ Tutorials > Essential Classes > Concurrency)
- 01-06-2013, 03:01 AM #4
Member
- Join Date
- Dec 2012
- Posts
- 74
- Rep Power
- 0
Re: question on swing coding?
I know that the following just creates an object which is a JFrame. A JFrame is Java's class for a window.
All of the other business about Runnable, the run() method and invokeLater() all have to do about Threads.Java Code:public static void main(String[] args) { Example ex = new Example(); // <<<< this creates an Example obect which is a JFrame (aka a window) ex.setVisible(true); }
I think that the window will work in the simplified main() method that I wrote. I'm not sure that I understand either why we would use invokeLater()
- 01-06-2013, 03:26 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: question on swing coding?
Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)I'm not sure that I understand either why we would use invokeLater()
-
Re: question on swing coding?
Using SwingUtilities.invokeLater(new Runnable() {....}); guarantees that the code inside of the Runnable will be queued onto the Swing event thread. Not doing this when creating your JFrame/GUI will usually work, but it may not always work and it may fail in both a predictable and unpredictable fashion, the former I've seen when using certain look and feels, and the latter is at especial risk since we're dealing with a threading issue.
- 01-06-2013, 09:12 PM #7
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
Re: question on swing coding?
ok but one question when it says example ex= new example, why does it say the "ex" and why does it say "new" example? Thanks
- 01-06-2013, 09:27 PM #8
Re: question on swing coding?
In the very first response in this thread, you've been advised to go through the tutorial. A forum is no substitute for systematic learning.
If you consistently ignore all attempts to help you by pointing you to learning material, I'll close this thread as it's going nowhere.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
-
Re: question on swing coding?
Similar Threads
-
guys....im having problem with this coding question.do help me
By ah choy in forum New To JavaReplies: 8Last Post: 05-03-2012, 06:37 PM -
Huffman coding algorithm question?
By knguye88 in forum New To JavaReplies: 1Last Post: 03-12-2012, 09:06 AM -
Beginner Question About Java Coding
By Humphrey Bogart in forum New To JavaReplies: 3Last Post: 03-09-2011, 09:41 PM -
question about coding conventions
By gib65 in forum New To JavaReplies: 8Last Post: 08-05-2010, 04:24 AM -
swing coding
By priya_gurnani166 in forum AWT / SwingReplies: 8Last Post: 01-30-2009, 07:18 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks