Click here to Skip to main content
15,122,149 members
Articles / Desktop Programming / MFC
Posted 3 Dec 2002


40 bookmarked

JLIB - A C++ porting of java.awt.*

Rate me:
Please Sign up or sign in to vote.
4.78/5 (37 votes)
30 Nov 20034 min read
An experimental C++ porting of java.awt.* and partial java.lang.*


JLIB is a C++ porting of Java's "awt" and partial "lang" package, it supports basic GUI and multi-threading programming. Its purpose is to establish a base to study the differences of C++ and Java, especially the differences while writing libraries.


If a C++ programmer has used Java for a while, when he comes back to the world of C++, he will inevitably recall the good times when he was using Java. At least I am in this case.

C++ is more difficult to study and harder to use than Java for most projects, not only because of the inherent complexity of the language, but also because of the lack of easy-to-use libraries. Even worse, there are totally no standard C++ GUI libraries to use. However, sometimes we are forced to use C++, both for technical and political reasons. So, porting some Java code to C++ becomes an interesting exercise for programmers.

At first, I tried to find one on the web. However, the result was disappointing. Only four were found:

  1. "jtk" by Guillaume Desnoix - a library which is exactly what I dream to, but it had been suspended for years.
  2. "jlWindows" (at CodeProject) by Gabriel Bizzotto - this library is a Win32 porting of Java AWT, but as the author is very busy, this project is also suspended. The author gave me some very helpful suggestions and was very kind to let me use the code of "jlWindows".
  3. AWT-Layout's contrib for wxWindows - it implements AWT style LayoutManagers for wxWindows.
  4. "Celios" from ElCel Technology - a commercial library, not free. It includes network, multi-threading and Unicode support, but does not have "awt" support.

I was quite surprised that there were only four such libraries. There must be more but I simply had not found. At that time, I was reading JDK's community source code and had some working code on my hand, I decided to write a new one to test the idea, this is where JLIB comes from.

JLIB should NOT be commercially used in any form. The purpose of JLIB is only for study. I hope it can provide a runnable code base to demonstrate various differences which influence the way libraries are implemented in Java and C++ and test if it is reasonable to port a Java library to C++.

Currently, JLIB supports all basic "awt" components, such as Button, TextArea and Checkbox; some "swing" components and containers, although it lacks some features, such as Slider, ProgressBar and SplitPane. Most layout managers are also ported from JDK directly to C++. JLIB also includes basic threading support, such as Thread, Monitor (implements the wait/notify methods of java.lang.Object) and a simulated synchronized keyword.

Using the code

As JLIB is only for studying, I did not try to compile it with every compiler, currently JLIB can pass gcc3.2 (mingw2.0) and Visual C++ .NET compilation. You can download and decompress to a folder. jlib.sln and jlib.vcproj are project files for Visual C++ .NET and a makefile is for C++.

Currently there are no documents for JLIB. If you want to view documents, refer to corresponding Java documentation.

Now there is only a Win32 implementation, I plan to implement a Linux version in future. Classes of JLIB are presented in a straightforward way for a Java programmer. Here is an example:

// A tanslation of the following java "Hello,world!" program with JLIB.
//import java.awt.*;
//import java.awt.event.*;
//class Hello extends Frame {
//    Label hello = new Label("Hello, world!", Label.CENTER);
//    Hello() {
//        add(hello);
//        addWindowListener(new WindowAdapter() {
//        public void windowClosing(WindowEvent e) {
//            dispose();
//        }
//        });
//    }
//    public static void main(String[] args) {
//        Hello hello = new Hello();
//        hello.pack();
//    }

// windows.h is required
#include <windows.h> 

// one header for one class, tedious but foolproof
#include <jlib/jlib.h> 
#include <jlib/awt/Frame.h>
#include <jlib/awt/Label.h>
#include <jlib/awt/event/WindowAdapter.h>
#include <jlib/awt/event/WindowEvent.h>

using namespace jlib::awt;
using namespace jlib::awt::event;

class Hello : public Frame, WindowAdapter {
    // A label, in C++, can not call constructor at here, call it
    // at the enclosing class's ctor-initilizer.
    Label hello;

        : Frame("Hello"), 
        hello("Hello, world!", Label::CENTER) { // construct the label
        // add the label into the frame.

        // no non-static inner class in C++, so let the enclosing
        // class to be the listener.

    void windowClosing(const WindowEvent&) {

    // the entry point of the program, will be called from JLIB.
    // ArgumentList is simply a typedef of std::vector<sring>.
    static void main(const ArgumentList&) {
        Hello hello;

MAIN_CLASS(Hello) // the class that has the static main method

Points of Interest

There are really some interesting and annoying points while writing JLIB, I will post it here later.


Some example AWT programs from JDK are ported to JLIB, here are the screenshots:









  • Dec 4, 2002 - First revision.
  • Dec 25, 2002 - Second revision.
    1. Toolkit::getComponent(HWND) was implemented to support "opposite" component in FoucsEvent and WindowEvent. Pointers to components are put into a vector (Toolkit::displayableComponents). The index in the vector was kept into the "userdata" of the corresponding HWND. Any time a Windows message is received, the index is retrieved from the window handle to look up the Component* in the vector. When a window handle should be destroyed, if it is not the last one in the vector, the last will be swapped to the freed slot and the index recorded in the HWND will be adjusted, then the last entry in the vector will be popped. This scheme features a constant lookup time while keeping memory footprint minimized. For details, see Toolkit::getComponent(HWND), Toolkit::addDisplayable(Component*)and Toolkit::removeDisplayable(Component*).
    2. TabbedPane was implemented. Icon and ImageIcon were added.
    3. IGraphics was discarded, the return value of Component::getGraphics() was changed to std::auto_ptr<Graphics>. IImage was discarded and Image Toolkit::getImage() was replaced by std::auto_ptr<Image> Toolkit::createImage().
    4. Graphics::draw3DRect() and Graphics::fill3DRect were added. Color::brighter() and Color::darker() were added.
    5. Two more JDK samples were ported to JLIB - "Blink" and "DrawTest".
    6. Header files were rearranged to speed up compilation. Defined _T macro if it has not been defined to eliminate excessive inclusions of <tchar.h>.
    7. Many bug fixes (probably more were introduced).
  • Nov 17, 2003 - Third revision.
    1. Added consume() support for InputEvents.
    2. EventListeners can be automatically removed from EventDispatchers during destruction. This can greatly ease the management of EventListeners.
    3. Merged jlib::awt::event namespace and jlib::awt namespace as the two namespaces are mutually dependent.
    4. Project files for VC7 were rearranged to facilitate building demos.
    5. Decent make files for gcc are provided. To build the project, you need mingw32 and msys, you can download them from SourceForge.
    6. Minor improvements and bug fixes.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
Germany Germany
No Biography provided

Comments and Discussions

Generalwhy do not try acdk at sourceforge Pin
jackyxinli14-Oct-10 16:18
Memberjackyxinli14-Oct-10 16:18 
hi, hzy,
i remember i'd visited acdk at sourceforge when i read your article. and this project had implemented most features of jdk package, i recommend it to you and try it, very nice.
project link is:[^]
Generalwin ce Pin
chelsea_zhai28-Jan-10 22:11
Memberchelsea_zhai28-Jan-10 22:11 
GeneralBuffered image class Pin
Vincent3434534310-Oct-05 2:18
sussVincent3434534310-Oct-05 2:18 
GeneralThe Cursor problem and suggestion Pin
gongwx16-Jun-05 17:38
Membergongwx16-Jun-05 17:38 
GeneralSome problem Pin
Tu Nam12-Apr-04 3:50
MemberTu Nam12-Apr-04 3:50 
GeneralNewJ Library for C++ Pin
domKing9-Dec-03 14:35
MemberdomKing9-Dec-03 14:35 
GeneralNewJ Library for C++ Now Supports JNI Pin
domKing19-May-04 13:02
MemberdomKing19-May-04 13:02 
GeneralInteresting, but PortableGui is most developed at this time. Pin
Rodrigo Pinto Pereira de Souza4-Dec-03 2:39
MemberRodrigo Pinto Pereira de Souza4-Dec-03 2:39 
QuestionAutomatic translation? Pin
Tobix3-Dec-03 23:56
MemberTobix3-Dec-03 23:56 
AnswerRe: Automatic translation? Pin
whymegod4-Dec-03 9:24
Memberwhymegod4-Dec-03 9:24 
GeneralRe: Automatic translation? Pin
pizzafroide211-Jul-06 8:06
Memberpizzafroide211-Jul-06 8:06 
AnswerRe: Automatic translation? Pin
hzy6-Dec-03 17:19
Memberhzy6-Dec-03 17:19 
AnswerRe: Automatic translation? Pin
Vitaly Shelest8-Feb-06 23:10
MemberVitaly Shelest8-Feb-06 23:10 
GeneralHey Dude Pin
whymegod3-Dec-03 16:40
Memberwhymegod3-Dec-03 16:40 
GeneralVery good! Pin
TomM2-Dec-03 5:37
MemberTomM2-Dec-03 5:37 
GeneralRe: Very good! Pin
hzy6-Dec-03 17:10
Memberhzy6-Dec-03 17:10 
GeneralOh! Pin
Stephane Rodriguez.3-Jan-03 1:53
MemberStephane Rodriguez.3-Jan-03 1:53 
Generalwell done Pin
aamironline2-Jan-03 18:00
Memberaamironline2-Jan-03 18:00 
GeneralAwesome Pin
Peter Kohout5-Dec-02 12:25
MemberPeter Kohout5-Dec-02 12:25 
GeneralNice Pin
joegood5-Dec-02 9:15
Memberjoegood5-Dec-02 9:15 
GeneralThis is cool! Pin
Daniel Turini5-Dec-02 8:43
MemberDaniel Turini5-Dec-02 8:43 
Generalgreat work Pin
eco4-Dec-02 23:37
Membereco4-Dec-02 23:37 
QuestionWhy? Pin
Nemanja Trifunovic4-Dec-02 11:24
MemberNemanja Trifunovic4-Dec-02 11:24 
AnswerRe: Why? Pin
Jörgen Sigvardsson4-Dec-02 11:41
MemberJörgen Sigvardsson4-Dec-02 11:41 
GeneralRe: Why? Pin
Giles4-Dec-02 12:03
MemberGiles4-Dec-02 12:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.