4 different GUI approaches - which is better?

Hi people!
Continuing my Java experiments - now with GUI writing.
I would very much like to know the pros and cons with these 4 different ways to get exactly the same result.
I'm sure some ways are better than others.
class ExperimentGUI
    void ExperimentGUI() // Example A
        JFrame content = new JFrame();
        JTextArea textView = new JTextArea();
        textView.setPreferredSize(new Dimension(300,300));
        content.add(textView, BorderLayout.CENTER);
        content.pack();
        content.setLocationRelativeTo(null);
        content.setVisible(true);
class ExperimentGUI extends JFrame
    void ExperimentGUI() // Example B
        JFrame content = new JFrame();
        JTextArea textView = new JTextArea();
        textView.setPreferredSize(new Dimension(300,300));
        content.add(textView, BorderLayout.CENTER);
        content.pack();
        content.setLocationRelativeTo(null);
        content.setVisible(true);
class ExperimentGUI
    ExperimentGUI() // Example C
        JFrame content = new JFrame();
        JPanel pane = new JPanel(new BorderLayout());
        JTextArea textView = new JTextArea();
        textView.setPreferredSize(new Dimension(300,300));
        pane.add(textView, BorderLayout.CENTER);
        content.setContentPane(pane);
        content.pack();
        content.setLocationRelativeTo(null);
        content.setVisible(true);
class ExperimentGUI extends JFrame
    ExperimentGUI() // Example D
        JPanel content = new JPanel(new BorderLayout());
        JTextArea textView = new JTextArea();
        textView.setPreferredSize(new Dimension(300,300));
        content.add(textView, BorderLayout.CENTER);
        setContentPane(content);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
}Regards,
Stefan
- a newbie

Be aware that if you declare variable in a local method or constructor, you will have problems with access in other places. Ex:
import java.awt.*;
import java.awt.event.*;
public class ClunkyGui {
//  private  TextArea ta;  // As oposed to global var
//  private  Frame    f;   // As oposed to global var
  public ClunkyGui() {
    Frame    f  = new Frame("ClunkGui");
//    f  = new Frame("ClunkGui");
    TextArea ta = new TextArea(10,20);
//    ta = new TextArea(10,20);
    Button   b  = new Button("GO!");
    b.addActionListener(new BJActionListener());
    Panel    p  = new Panel();
    p.setLayout(new FlowLayout(FlowLayout.CENTER));
    p.add(b);
    f.add(p,BorderLayout.NORTH);
    f.add(ta);
    f.pack();
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        f.dispose(); // <access pblm
        System.exit(0);
    f.setVisible(true);
  private class BJActionListener  implements ActionListener {
    public void actionPerformed(ActionEvent ae) {
      ta.setText("HI!"); // <access pblm
  public static void main(String[] argv) {
    new ClunkyGui();
}Now you can code around this and should be able to avoid such things, but just be aware of this.
~Bill

Similar Messages

  • Array of structures - two ways to approach - which is better?

    Folks - I've been trying to create an array of structures,
    but I bumped into problem that lead me to an example that is
    completely different. I'm trying to figure out which is valid. In
    general, it comes down to this:
    (1) myStructure
    .firstName
    vs
    (2) myStructure.firstName
    The problem with the first is that I can't find a way to
    discover the size of the array, which leads me to suspect this is
    not a valid method. The problem with the second is that you must
    redefine the structure for EACH array element, which seems like it
    would add a lot of overhead.
    Any advice? Sample code attached.
    Thanks!
    ************* Sample (1) **************
    <CFSET sCust=StructNew()>
    <CFSET vCustCount = #rsMailAuth.recordcount#>
    <CFSET i = 0>
    <CFOUTPUT QUERY="rsMailAuth">
    <CFSET i = i+1>
    <CFSET sCust.userId
    = #rsMailAuth.userId#>
    <CFSET sCust.validEmailSource =
    #rsMailAuth.validTcEmails#>
    <CFSET sCust.authKeyword
    = #rsMailAuth.tcEmailKeyword#>
    <CFSET sCust.authKeywordLoc =
    #rsMailAuth.tcEmailKeywordLoc#>
    </CFOUTPUT>
    ************* Sample (2) **************
    <cfset strTest = arrayNew(1) >
    <cfset strTest[1] = structNew() >
    <CFLOOP INDEX = "i" FROM="1" TO="5" >
    <cfset strTest
    = structNew() >
    <cfset strTest.id = i>
    <cfset strTest
    .name = "[email protected]">
    </CFLOOP>
    <cfoutput>#arrayLen(strTest)#</cfoutput>

    Sorry - simple english...
    I am collecting a series of emails from a pop email server.
    As I parse these into the individual parts (subject, body, from,
    etc) I want to stuff them into a structure. each email would be a
    new index. so, the 2nd email 'from' info would go into
    myStructure.from[2] or myStructure[2].from.
    I know there are lots of ways around this, I just have a
    natural predisposition to use structures when possible as it makes
    it easier for me to keep track of things and makes it easier to
    follow the code.
    I can make either of these work, I'm just not sure if they
    are valid in the world of coldfusion8.

  • Which is better approach to manage sharepoint online - PowerShell Script with CSOM or Console Application with CSOM?

    Which is better approach to manage sharepoint online - PowerShell Script with CSOM or Console Application with CSOM?
    change in sharepoint scripts not require compilation but anything else?

    Yes, PowerShell is great, since you can quick change your code without compilation.
    SP admin can write ps scripts without specific tools like Visual Studio.
    With powershell you can use cmdlets,
    which could remove a lot of code, for example restarting a service.
    [custom.development]

  • Which approach is having better performance in terms of time

    For large no of data from more then two diffrent tables which are having relations ,
    In Oracle in following two approaches which is having better performance in terms of time( i.e which is having less time) ?
    1. A single compex query
    2. Bunch of simple queries

    Because their is a relationship between each of the tables in the simple queries then if you adopt this approach you will have to JOIN in some way, probably via a FOR LOOP in PL/SQL.
    In my experience, a single complex SQL statement is the best way to go, join in the database and return the set of data required.
    SQL rules!

  • Which is better, Double Buffering 1, or Double Buffering 2??

    Hi,
    I came across a book that uses a completely different approach to double buffering. I use this method:
    private Graphics dbg;
    private Image dbImage;
    public void update() {
      if (dbImage == null) {
        dbImage = createImage(this.getSize().width, this.getSize().height);
        dbg = dbImage.getGraphics();
      dbg.setColor(this.getBackground());
      dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
      dbg.setColor(this.getForeground());
      paint(dbg);
      g.drawImage(dbImage, 0, 0, this);
    }that was my method for double buffering, and this is the books method:
    import java.awt.*;
    public class DB extends Canvas {
         private Image[] backing = new Image[2];
         private int imageToDraw = 0;
         private int imageNotDraw = 1;
         public void update(Graphics g) {
              paint(g);
         public synchronized void paint(Graphics g) {
              g.drawImage(backing[imageToDraw], 0, 0, this);
         public void addNotify() {
              super.addNotify();
              backing[0] = createImage(400, 400);
              backing[1] = createImage(400, 400);
              setSize(400, 400);
              new Thread(
                   new Runnable() {
                        private int direction = 1;
                        private int position = 0;
                        public void run() {
                             while (true) {
                                  try {
                                       Thread.sleep(10);
                                  }catch (InterruptedException ex) {
                                  Graphics g = backing[imageNotDraw].getGraphics();
                                  g.clearRect(0, 0, 400, 400);
                                                    g.setColor(Color.black);
                                  g.drawOval(position, 200 - position, 400 - (2 * position), 72 * position);
                                  synchronized (DB.this) {
                                       int temp = imageNotDraw;
                                       imageNotDraw = imageToDraw;
                                       imageToDraw = temp;
                                  position += direction;
                                  if (position > 199) {
                                       direction = -1;
                                  }else if (position < 1) {
                                       direction = 1;
                                  repaint();
              ).start();
         public static void main(String args[]) {
              Frame f = new Frame("Double Buffering");
              f.add(new DB(), BorderLayout.CENTER);
              f.pack();
              f.show();
    }which is better? I noticed smoother animation with the later method.
    Is there no difference? Or is it just a figment of my imagination??

    To be fair if you download an applet all the class files are stored in your .jpi_cache, and depending on how that game requests its graphics sometimes they are stored there to, so really if you have to download an applet game twice, blame the programmer (I've probably got that dead wrong :B ).
    But, what's wrong with Jars. They offer so much more.
    No offence meant by this Malohkan but if you can't organize your downloaded files the internet must really be a landmine for you :)
    Personally I'd be happy if I never seen another applet again, it seems java is tied to this legacy, and to the average computer user it seems that is all java is capable of.
    Admitidly there are some very funky applets out here using lots of way over my head funky pixel tricks, but they would look so much better running full screen and offline.

  • Which is better for doing animation? Flash 4, 5, MX, CS, or others?

    Which is better for doing animation? Flash 4, 5, MX, CS, or
    others?
    I am used to using brush function more conveniently in Flash
    4 than the other versions I ve tried. However, Flash 4 doesn't have
    pen tool.
    Free News Reader
    http://put.hk
    http://put.hk/reader/forums.macromedia.com/macromedia.flash.html

    1Evan2Wing3 wrote:
    > Which is better for doing animation? Flash 4, 5, MX, CS,
    or others?
    >
    > I am used to using brush function more conveniently in
    Flash 4 than the other versions I ve tried. However, Flash 4
    doesn't have pen tool.
    all pretty identical tho I would go with cs, just because I
    like the GUI much
    more than other versions...
    Best Regards
    Urami
    "Never play Leap-Frog with a Unicorn."
    <urami>
    If you want to mail me - DO NOT LAUGH AT MY ADDRESS
    </urami>

  • Which is better?    Two Oracle user in one instance OR  in two instances?

    Which is better?
    I could not find any benchmark for number of instances per user
    in the same machine.
    suppose that you have two major Oracle user, from performance
    point of view it is better to make separate instances in same
    machine or keep these two users in the same instance.

    Hi.
    I understand that you will use oracle for two different
    applications on the same host.
    Each instance has its SGA and background processes. So, using
    two different schemas(users) in one instance you can share
    phisical memory between the applications. Concerning background
    processes, you can start as many of them as you wish, so it's
    not a problem. If you decided that one DBW is not enough, you
    could configure oracle to use two or more.
    best regards,
    Andrew

  • Which is better(generally): MacBook Pro with Retina Display or MacBook Air?

    Which is better(generally): MacBook Pro with Retina Display or MacBook Air?

    Better for what? These are two very different computers. Tell us which things you are looking for and we may be able to help you. Examples - weight, standard RAM and flash memory available, video resolution, etc.
    See the technical specifications articles for each -
    MacBook Air.
    MacBook Pro retina display.
    Best of luck.

  • Why assertion instead of if statement ?  Which is better to use ?

    Why assertion instead of if statement ? Which is better to use ?

    Never assertion instead of if statement. The two have fundamentally different intents. Using assert to control program flow is a bad idea

  • Which is better for education iPad or Macbook air?

    I am a grade 8 student and I walk around quite a bit in school and I would like to know which one is better for me?
    Which is better for school, and no I can not get the discount.
    Macbook Air or
    iPad
    If macbook Air which size and specs would you reccomend?
    And if iPad which storage option, white or black do you reccomend?
    And should I buy the iPad 2 or the new one.

    Great question.
    Word processing for iOS is fantastic, but not as good for citations and things as on the macbook air, though footnotes will be preserved.
    The new textbook options on ipad are compelling. You should find out if any of your school textbooks are on there by searching the itunes store. If so, ipad is your choice.
    They are different categories of products. the macbook air is an excellent computer.
    But I guess I feel this way -- why don't you buy a desktop for the home. and an ipad for school? If you feel you can get all of your notetaking, paper drafts etc. done on ipad, then you can use mountain lion (coming out this summer) to keep it all in sync when you go home.
    I do have to warn you that I tried taking my ipad to work. If you use google docs at all, you may find some limitations sometimes. If you stick within the icloud universe, (pages, keynote, and numbers), you may be mostly happy.

  • Which is better, many PXI cards or fewer PXI cards with more SCXI cards?

    I have a pretty basic question.  Which is "better", using multiple PXI cards for analog inputs or a single PXI card and multiple SCXI cards?  For example, if I have say 100 channels to sample (all at the same rate), I could use 2 6225s at 80 ch each.  Or I could use 1 6220 and 4 SCXI 1100s (32 ch each).  What are the pros and cons with either approach?  Keep in mind sample rate probably isn't an issue (less than 1 kHz) but it is the same for all channels.
    Thanks.

    The pros/cons will depend on your application now and in the future.  I recommend contacting your local NI sales representative.  Synchronizing across a PXI system is easy to do via the backplane.  There are PXI/SCXI combo chassis available, you could use PXI multifunction DAQ cards and then SCC for signal conditioning for thermocouples for additional functionality later.  There are many possibilities that may be best discussed with your sales representative.
    Regards,
    h_baker
    National Instruments
    Applications Engineer
    Digital Multimeter Resources

  • Which is better Apple Ear Pods vs. In Ear Earbuds

    So which is better? I can't really tell the difference because there like the same. Unless I'm not paying attention to the bass and tremble.

    Haven't tried multiple EAR files before but encountered constraints when using one EAR file.
    For example, we may need to wait for another colleague to complete his portion on programming so that we can proceed to implement the entire EAR file. Is this also a constraint for multiple EAR files application ?
    If we can separate the EAR files into different modules, different colleagues can work on different modules concurrently. It could be more efficient... Is that the case ??

  • Email settings: POP vs. IMAP...Which is better?

    I use my iPhone for work, like many of us do.  I came from a balckberry, so I am use to getting my emails instantly..... I will say that is about the only thing I miss.....
    I can set up my work email as either a pop or imap.  Which is better?  Do they both fetch? I wish the iPhone offered push email (I'm sure I am not the only one).
    thoughts, comments, ideas, sloutions???/

    The only reason push access is supported with every email account accessed with a Blackberry is because every email account is routed through RIM's dedicated email delivery service for the Blackberry which includes some features not available otherwise.
    Different from a Blackberry, the iPhone's Mail app communicates direct with the incoming mail server for an email account - no different from accessing the account with an email client on your computer. The email account must support Push access with the iPhone's Mail app, and that is with an Exchange account, a MobileMe account, and with a Yahoo account created with the Yahoo account preset. A Gmail account can be accessed as an Exchange account with the iPhone's Mail app.

  • Singleton vs static - which is better?

    Of the two approaches -
    a class which can be used by accessing its ONLY instance(singleton) or a class which has a set of static methods which can be invoked on the class itself
    which is better and why? Or are these just two 'styles' of programming?
    I always get confused as to which way to go? I tend to prefer to have static methods on the class instead of a singleton.
    All insights/comments/ideas welcome.
    Thanks

    Well, there are a few questions you can ask - does the method cause any changes of state, or is it a pure function? If the latter, static is probably the way to go.
    The way you are talking, I gather that there is some state involved.
    Next question: does it make sense for there to be more than one of these per JVM? Not only in the way you currently envision it, but at all, anywhere. For example, consider the list of Strings that the String class keeps so it can consolidate memory and avoid duplication (see String.intern() ). That list makes sense to be static.
    On the other hand, the representation of the state of a board game should not be static, because someone could want to write a program which has multiple games within it - or you could within one game wish to have contingencies or undo-ability (essentially, it might not be as singleton as you think).
    Next, if you think the methods will ever need to be overridden, use a singleton, because static methods aren't, well, dynamic! (in case the singleton is a one-at-a-time singleton but it makes sense to have a change of identity over time).
    I have never written a true singleton - though often written classes which I did not PLAN on instantiating more than once.

  • Which is better to sync, wifi or iCloud?

    I heard that you shouldn't sync with itunes and icloud.  Which is better, and how do you set that up?

    Where did you hear you shouldn't use these?
    You're comparing apples and oranges. Wifi sync is a way to wirelessly sync with iTunes on your Mac or PC. This includes content that you've purchased, content that you've ripped from your own CD collection, as well as things like playlists, song ratings, etc. This is just to replace plugging in your USB cable as you've always had to do in the past.
    iCloud is a way to store some of your content on Apple's servers and retrieve it on any iCloud enabled device. Note this includes only content you've purchased (music, movies, tv shows) or content you've created using iCloud-enabled apps like Pages, Numbers, or Keynote. iCloud doesn't let you sync "metadata" such as playlists, song ratings, etc. if you add a song to a playlist on your iPhone, it won't show up on that same playlist on your iPad, Mac, or PC because you use iCloud. It only gets moved on those other devices after you sync. iCloud also provides a means to keep calendars, contacts, notes, and reminders the same across multiple devices. Finally, iCloud let's you backup your device to the cloud instead of to your Mac or PC, which is handy feature as you don't have to worry about losing your backup should your Mac or PC fail.
    You can choose to use iCloud and not wifi sync, or use wifi sync and not iCloud, or use both or use neither. They are independent of one another and provide different functions.
    Personally, I use both. I wirelessly sync so my iDevices are up to date, and I take advantage of iCloud for my backups, calendar, contacts, etc.

Maybe you are looking for

  • Why the 2LIS_08TRTK extractor can not get  all data

    Hello, BW Gurus. Why the 2LIS_08TRTK and 2LIS_08TRTLP extractors can not get all data. I had used the RSA3 and get 10 registers, when a check at the VTTK table I had 20 registers, I didnt use filters at RSA3, could you help to know what happen o corr

  • Optical Drive gone on MacBook Trying To Install using Remote Disk

    I've followed the instructions on using Remote Disk to reinstall my MacBook system software but the MacBook does not see the Remote Disk from my MacBook Pro. They are both on the same network and I've followed the instructions but when I hold down th

  • Web Service wdsl file getting generated as a HTTPS protocol instead of HTTP

    Hi Experts, I have a   requirement I have created a web service in development client  which is used in interactive adobe form. After moving the web service to production only defination is getting generated. I have tried to manually create the servi

  • USB Mouse Constantly Freezing

    With more and more frequency my Logitech MX-Laser cordless USB mouse is freezing. It seems that when I have it connected via my powered USB hub, freezing occurs regularly. If I connect directly to the computer the problem seems to disappear. I know t

  • Where is this mystery project file?!

    I have just opened FCE 4 and staring me in the face was an old project I have not used for a while. However, what is even more weird is that the title is totally different from what it was and I cannot find this new project file anywhere on my comput