AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Swing/EDT - Static vs Instance?

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java GUI Toolkits
View previous topic :: View next topic  
Author Message
Knute Johnson
Guest





PostPosted: Thu May 25, 2006 6:07 pm    Post subject: Swing/EDT - Static vs Instance? Reply with quote



When Sun published the recommendation to create and modify all Swing
components in the EDT, I changed the way I write my GUI apps to mimic
the example in the Java Tutorial. The Tutorial example does the GUI
creation in a static method called on the EDT from the main method.
Before this recommendation we all just extended JFrame and created our
GUIs in the constructor. Using the static method required some changes
to the coding of things like action listeners. None of them
insurmountable just different. But why the static method? Why couldn't
we just extend JFrame as usual and instantiate it on the EDT? I'm not
sure why I didn't think of this before but somebody else's post on
another subject got me to thinking. So I'm curious how others create
their Swing GUIs. Do you follow the Tutorial, use the old method or
something else?

Thanks,

knute...

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test7 {
public static void createGUI() {
JFrame f = new JFrame("test7");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(200,200);
f.setVisible(true);
System.out.println(EventQueue.isDispatchThread());
}

public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
createGUI();
}
};
EventQueue.invokeLater(r);
}
}

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test8 extends JFrame {
public test8() {
super("test8");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200,200);
setVisible(true);
System.out.println(EventQueue.isDispatchThread());
}

public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
new test8();
}
};
EventQueue.invokeLater(r);
}
}

--

Knute Johnson
email s/nospam/knute/
Back to top
Thomas Hawtin
Guest





PostPosted: Thu May 25, 2006 6:07 pm    Post subject: Re: Swing/EDT - Static vs Instance? Reply with quote



Knute Johnson wrote:
Quote:
When Sun published the recommendation to create and modify all Swing
components in the EDT, I changed the way I write my GUI apps to mimic
the example in the Java Tutorial. The Tutorial example does the GUI
creation in a static method called on the EDT from the main method.
Before this recommendation we all just extended JFrame and created our
GUIs in the constructor. Using the static method required some changes
to the coding of things like action listeners. None of them
insurmountable just different. But why the static method? Why couldn't
we just extend JFrame as usual and instantiate it on the EDT? I'm not
sure why I didn't think of this before but somebody else's post on
another subject got me to thinking. So I'm curious how others create
their Swing GUIs. Do you follow the Tutorial, use the old method or
something else?

You can still extend JFrame. That doesn't change. However, needlessly
extending classes has always been a terrible practice and remains so.

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
Back to top
Knute Johnson
Guest





PostPosted: Thu May 25, 2006 7:07 pm    Post subject: Needless extending - was Re: Swing/EDT - Static vs Instance? Reply with quote



Thomas Hawtin wrote:
Quote:

You can still extend JFrame. That doesn't change. However, needlessly
extending classes has always been a terrible practice and remains so.

Tom Hawtin

Tom:

I don't disagree but I often compare my code to code I see on these
lists and in books and think that I don't extend enough classes. I
participated in another thread the other day concerning getters and
setters. A large part of the reason for them being protection when the
class is extended. I can understand a different view for API
development but production code, at least most of the code I've written,
will never be extended. Extending classes several times reminds me of a
discussion I had with a fellow I was working for several years ago. We
were writing some large C programs and he told me that it was not
considered good practice to use more than 5 levels of indirection.

--

Knute Johnson
email s/nospam/knute/
Back to top
Thomas Hawtin
Guest





PostPosted: Thu May 25, 2006 8:07 pm    Post subject: Re: Needless extending - was Re: Swing/EDT - Static vs Insta Reply with quote

Knute Johnson wrote:
Quote:
Thomas Hawtin wrote:

You can still extend JFrame. That doesn't change. However, needlessly
extending classes has always been a terrible practice and remains so.

I don't disagree but I often compare my code to code I see on these
lists and in books and think that I don't extend enough classes. I

There's an awful lot of bad books out there. The vast majority of code
is appalling.

Quote:
participated in another thread the other day concerning getters and
setters. A large part of the reason for them being protection when the
class is extended. I can understand a different view for API

Really?

Quote:
development but production code, at least most of the code I've written,
will never be extended. Extending classes several times reminds me of a

That doesn't matter. The fact that it extends a leaf class unnecessarily
is good enough reason for it to be bad code.

Quote:
discussion I had with a fellow I was working for several years ago. We
were writing some large C programs and he told me that it was not
considered good practice to use more than 5 levels of indirection.

Take it one level at a time.

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java GUI Toolkits All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.