CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2005
    Posts
    114

    No explicit loop necessary?

    From my knowledge of C++ applications, once a window is created, it is necessary to perform a continual loop to check for windows messages. If no loop is executed, the program exits. Why do Java programs with a window (or frame rather) created continue to run even though there is no loop? And why do ones without a window created exit? I'm not cirtisizing the language at all, but this just doesn't make sense to me. What happens when a window is created that keeps the program running, even while lines of code following the one declaring the window's creation run concurrently, and after the main function exits (or seems to exit)? Does it have something to do with threads? Whatever it is, please explain it to me. I am eager to program in this language, but I must admit my knowledge of basic language features and mechanics is limited. I haven't studied it in a while, neither have I used it. But if someone can just answer the question I asked, that would be nice.

  2. #2
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: No explicit loop necessary?

    And needing to write for every application the same (or similar) continuous loop that checks if a new message was sent to the application and dispatch it when it happens to keep it alive makes more sense?

    In Java when a window (or frame) is created it will remain there until you dispose of it, that's why the app continue to run without closing immediately after the code that creates and possibly shows the frame finished.

    The loop you have to explicitly write in C/C++ for every application is provided in Java by the classes that implement the GUI objects. You only have to attach listeners to the frame and/or its components that will be call when the event that they are listening to occurs.

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: No explicit loop necessary?

    The GUI library supplied with Java (called 'Swing') handles the event thread and event dispatching. Have a read of the Java Tutorial, especially Concurrency In Swing.

    The purpose of computing is insight, not numbers...
    R. Hamming
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  4. #4
    Join Date
    May 2009
    Posts
    2,413

    Re: No explicit loop necessary?

    Quote Originally Posted by Guidosoft View Post
    Whatever it is, please explain it to me.
    Strictly this is not a programming language issue but a question on how you use the language to interact with the operating system in order to create an application.

    The C++ and Java approaches are somewhat different in this respect. C++ is a systems language so here you've got several options. You can use C++ to interact intimately with the OS, that is making direct calls to a low-level API. In the Windows case it's called Win32 and this seems to be the approach you've been using.

    But there are other possibilities in C++ as well. One way is to use a portable Graphical User Interface (a GUI package). Prominent examples are WxWidgets, FLTK and Qt. Here the actual calls to the different OSes are hidden and you develop your application using an OS independent abstraction. I'm mentioning this because using a GUI is the only way available to you in Java (but you could use the same approach in C++).

    The most common GUI package in Java is Swing. Here's an introduction,

    http://java.sun.com/docs/books/tutor...ing/index.html

    Swing can be quite daunting because it's a huge library. And it's a totally different approach to creating a GUI application than the low-level OS specific approach you're used to. But it's the Java way so if you're going to use Java you don't have much of a choise. And after you've mastered Swing you'll have a much easier time using one of the equivalent package in C++.

    Good luck.

  5. #5
    Join Date
    May 2009
    Posts
    2,413

    Re: No explicit loop necessary?

    There are essentially two ways of working with Swing, either you program it directly "by hand" or you use a visual editor. If you have developed programs using the MFC and Microsoft Visual Studio you already know how the latter works.

    Both major Java IDEs, Eclipse and Netbeans, have "visual" capabilities which let you put together a GUI application largely by dragging & dropping graphics widgets, like buttons and menues etcetera, to whereever you want them in the windows belonging to your application.

    I think maybe Netbeans is a little easier because the visual editor is built-in whereas Eclipse requires you to add one on. At least that was the case the last time I looked a few years ago.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured