Java vs. C++ for building a GUI which has a C++ backend [closed]
Asked Answered
J

11

25

I currently have a C++ backend that I need to connect with a GUI, and since I've never built a GUI before, I was confused on where to start.

I'm comfortable writing code in C++ and Java, so I'd prefer my GUI to be in one of those languages. Also, the GUI has to be reasonably OS independent over Windows and Linux (and hopefully, hence Macs).

Now I understand that if I use Java to do it, I'll need some wrappers to do it - but I've also heard (strictly second hand) that writing a GUI in C++ is a pain.

I don't want to rewrite too much of my backend code in Java (who does??) and I was hoping for input on:

  • Does either language offer serious advantages/disadvantages compared to the other?
  • How serious is the wrapping issue, and how much rewriting would come in if I used Java.
  • Are there any specific resources I should look at that people think would be relevant?

Thanks and Cheers All :)

Jewelfish answered 8/7, 2010 at 19:24 Comment(6)
I would implement GUI in assembly.Elbaelbart
You still need a library--or at least a protocol--to target.Cower
@Hamish Grubijan: Oh yeah? Well I would implement GUI in raw machine code!Rafaelof
Randolpho - oh yeah ... well I would re-program the monitor's FPGA to display what I want.Elbaelbart
Well, I would use patterns in the microcode to establish electrical patterns on the FPGA pins so that the GUI would be properly displayed in an oscilloscope. With enough G, who needs R and B?Pinero
Real Programmers: xkcd.com/378Pericycle
M
15

You say you already know C++ and Java, and that you never did a GUI before. That means:

  • no matter if you go for a Java GUI or a C++ GUI, you will need to learn how to handle the GUI framework
  • if you chose Java, you also need to learn how to interface between the two languages

So staying in C++ saves you one thing to learn. Well, it's always a good idea to learn something, but might be a bad idea to learn two new concepts at the same time. Anyway, the learning might be the smaller burden, I guess there is a lot of actual work involed, even when you use tools like SWIG.

You might want to know if writing a GUI in Java or doing it in C++ is easier. It depends on the chosen Framework. For Java, you have AWT and Swing which are part of the default Java distribution, and then there is SWT which is used by Eclipse, for example. For C++, there are many toolkits, with Qt, GTK and wxWidgets being the most popular ones, and all three support every major platform. Most of those "C++" GUI toolkits also have a Java binding or even a Java port, so you could use them with Java as well.

So far I've used Swing, Qt and a few others which don't help in your situation (The UI thingy that came with Borland C++ Builder and WinForms on .NET). Basically, the concepts are the same for all those frameworks, and I found none of them being harder or easier than the other. The only exception maybe Java, because I never got those LayoutManagers to work, even though the other toolkits have equivalents to LayoutManagers that are easy to master. But maybe thats just me.

People also will tell you that Java GUIs are always ugly and don't fit the host system. Well, most Java GUIs really are, but IMHO thats not because of Java, but because of bad programming. It takes two lines of code to let a Swing app adapt to the look and feel of the OS, and most programmers simply don't put enough effort into their Java GUIs to copy and paste those two lines... you can imagine how much they care about the rest of their GUI design.

For your current situation, I would recommend a C++ GUI, but if you know how your future plans look like, and if you know you will doing Java GUIs for the rest of your life, then it's probably ok to start that now and take the extra effort of .

And if you chose C++ for the GUI, people will tell you all kind of things to pull you in any direction. All of the three big portable frameworks have their pros and their cons, but I don't believe there is any single best or worst one among them. I'd recommend Qt simply because I already used it - but if I'd happten to have used GTK or wxWidgets instead, I'd probably suggest that.

Macgregor answered 8/7, 2010 at 21:59 Comment(0)
F
34

Have a look at Qt.

In my experience communicating between two different language runtimes is always challenging. If you have a non-trivial application to build the following often pose challenges:-

  • Error Handling.
  • Memory Management.
  • Multithreading and Synchronization Semantics.

Apart from increasing one level of indirection due to wrappers, it requires a lot of thinking like circumstances where you need to pass data structures across GUI and backend etc.

For example:- Consider passing a Java String from GUI to backend C++. Essentially, we have to extract the characters from a Java String object and make them available to the C++ developer without leaking the memory which holds them. This is an example of a basic problem (there are other aspects too like the encoding in which the characters are to be returned).

Facesaving answered 8/7, 2010 at 19:26 Comment(0)
M
15

You say you already know C++ and Java, and that you never did a GUI before. That means:

  • no matter if you go for a Java GUI or a C++ GUI, you will need to learn how to handle the GUI framework
  • if you chose Java, you also need to learn how to interface between the two languages

So staying in C++ saves you one thing to learn. Well, it's always a good idea to learn something, but might be a bad idea to learn two new concepts at the same time. Anyway, the learning might be the smaller burden, I guess there is a lot of actual work involed, even when you use tools like SWIG.

You might want to know if writing a GUI in Java or doing it in C++ is easier. It depends on the chosen Framework. For Java, you have AWT and Swing which are part of the default Java distribution, and then there is SWT which is used by Eclipse, for example. For C++, there are many toolkits, with Qt, GTK and wxWidgets being the most popular ones, and all three support every major platform. Most of those "C++" GUI toolkits also have a Java binding or even a Java port, so you could use them with Java as well.

So far I've used Swing, Qt and a few others which don't help in your situation (The UI thingy that came with Borland C++ Builder and WinForms on .NET). Basically, the concepts are the same for all those frameworks, and I found none of them being harder or easier than the other. The only exception maybe Java, because I never got those LayoutManagers to work, even though the other toolkits have equivalents to LayoutManagers that are easy to master. But maybe thats just me.

People also will tell you that Java GUIs are always ugly and don't fit the host system. Well, most Java GUIs really are, but IMHO thats not because of Java, but because of bad programming. It takes two lines of code to let a Swing app adapt to the look and feel of the OS, and most programmers simply don't put enough effort into their Java GUIs to copy and paste those two lines... you can imagine how much they care about the rest of their GUI design.

For your current situation, I would recommend a C++ GUI, but if you know how your future plans look like, and if you know you will doing Java GUIs for the rest of your life, then it's probably ok to start that now and take the extra effort of .

And if you chose C++ for the GUI, people will tell you all kind of things to pull you in any direction. All of the three big portable frameworks have their pros and their cons, but I don't believe there is any single best or worst one among them. I'd recommend Qt simply because I already used it - but if I'd happten to have used GTK or wxWidgets instead, I'd probably suggest that.

Macgregor answered 8/7, 2010 at 21:59 Comment(0)
H
10

Depending on your needs, a simple web interface might be the simplest when you have no existing frontend code. Embed a tiny web server in your application and open a browser on "http://localhost:12345" (or what port you end up using).

Hand answered 8/7, 2010 at 20:55 Comment(2)
downvote? For suggesting a web gui? Oh well :)Juieta
Fanatics, I tell you ;-). This is also a good alternative, how many apps are web enabled today... I think they read Web and that scared the downvoters. I put you again at level 0. :-)Bancroft
C
4

I can't say much about coupling Java and C++, but I suggest you have a look at Qt. It's a C++ library for a lot of things, like file and network access, but it's most famous for GUI development. It also has a nice IDE were you can build your GUI with drag-and-drop. I would also say that Qt is as OS-agnostic as it gets for GUI libraries.

Cuneiform answered 8/7, 2010 at 19:31 Comment(0)
C
2

Writing a GUI in C++ is no more of a pain than doing it in Java.

There are numerous cross-platform GUI libraries. GTK, gtk--, FoX, WX, etc. I would not recommend Qt since it's not really C++ (uses an extended version of the language requiring a special preprocessor before compilation). Plus it costs a fortune if you don't want to give away your product.

BTW, that's not how the word "hence" is used.

Cower answered 8/7, 2010 at 19:31 Comment(5)
Many commercial applications can use Qt via the LGPL at no cost.Dafna
+1 for not recommending Qt. I would also mention Ultimate++, gtkmm and FLTK among portable C++ libraries.Madoc
I'll check out GTK - thanks :) As for the word 'Hence', I used it in the context that my GUI working in Linux would translate to Mac OS X. I didn't really state the OSX part clearly, I'll admit, but I've seen hence used in published text similarly. But then again, interpretations make a language...Jewelfish
If the choice is between Java and C++, the argument that "Qt isn't really C++" doesn't count since Java neither is "really C++". Also the (free) LGPL version of Qt is very well applicable to most commercial products. Not to speak of the Qt designer which beats most open source GUI designers ...Real
I'm a huge fan of GTK+, but I think that Qt is worth recommendation in this case.Gavriella
S
1

You did not mention the richness of the interaction between front and back ends, which would weight the importance of the existing language in your decision.

I've worked with Qt, Swing and SWT, and typically used both C++ and Java code with all of these toolkits. The interaction between languages can add additional cost/risk. However, sometimes that cost is warranted given other benefits.

If for any reason you choose a Java front end, look at JNA and SWIG.

Stalinsk answered 8/7, 2010 at 23:0 Comment(0)
E
1

Let's all be honest here. C++ just ain't on the map when it comes to portable GUIs.

Java has a consistent, portable, widely used, thoroughly documented, mature GUI toolkit. C++ has a bunch of half-assed OSS libraries that barely work, none are truly portable, plus some expensive commercial libraries that don't work on all targets they claim to, work spotty on the remaining targets, and invert control so that you are stuck in their weird framework.

Unless you need C++ for other reasons (of which there are plenty), choose Java for the GUI. The cross-over coding is trivial for someone who knows both languages, but it can get messy to manage, so you'll want to minimize the native interface as best you can. My advice here is to make a pact with your team that you will never attempt to hold pointers (or references) across the interface. It just gets messier if you do, and no debugger can save you when the lines get tangled. Instead, use integer or string keys and pass them across the native interface.

Ekaterinoslav answered 8/7, 2010 at 23:5 Comment(3)
While you may be honest, you are misinformed. The best and most popular UI libraries are written in either C, C++ or C#. Java doesn't look good even on ONE platform, never mind cross platform.Istle
The problem is: you are both right (John and rpg). C++ UI libraries are not very good, and Java UI does not look nice on any platform.Madoc
It's not a matter of being informed. It's a matter of being experienced. :) I have professional experience with just about every GUI toolkit ever written, portable or native. Java's is the best out there in terms of toolkit. In terms of how it LOOKS... well, you have a point. Java's GUI looks better than arbitrary web apps, and better than the lesser toolkits like Tk, FLTK, etc., but worse the native desktop toolkits (.NET, Cocoa). It's by no means perfect. Though I dislike Java in general, I have to give credit where credit is due, and admit that Java's GUI toolkit is pretty solid.Ekaterinoslav
D
1

How about eclipse? Looks good and performs well on all platforms. My guess is most of eclipse is Java.

Dour answered 28/6, 2012 at 7:24 Comment(1)
IBM used it for Notes, but it's far from a general-purpose GUI framework.Rastus
M
0

Why you don't you learn native C++ GUI like WINAPI or X11? Then you can make console software to compatible one of them like winehq, cygwin or other open sources compatibility or emulated software, since you never developed GUI on C++. Don't go for Java because it uses much more RAM. Of course, unless you have lots of RAM. For example, Eclipse (which is coded in Java) can use 500mb of RAM when running with no project opened.

Mccay answered 23/1, 2017 at 7:54 Comment(0)
B
-1
  1. Wrapping is not rewriting, it's just an adapter to make both languages meet. It is straight-forward
  2. As you don't seem to be fixed on a language, I would choose a .NET Gui (With C++ CLR) you'll have a GUI that is machine independent and you can easily communicate with your existing code.

For beginners WinForms is maybe easier, but try to use WPF, it's the most modern variant for GUI development in the .NET world.

Personnally I would use C#/.NET for the GUI and use a C++ CLR wrapper DLL. But it is not the only solution.

Under Linux, the best .NET implementation is MONO. For all WinForms apps I have developped (they are not the craziest ones), they ran without change. With C++/Qt you will have to recompile for each target OS.

Bancroft answered 8/7, 2010 at 19:32 Comment(6)
He asked for platform independant. Windoze, Linux and Mac. I think that counts out C#/.NET.Idiomorphic
The Mono version of GTK might allow it.Cower
One downvote, I knew it was a stupid idea to make a .NET suggestion to C++ fanatics :-DBancroft
A C# answer doesn't belong in a C++ question and your answer only targets a subset of those the OP wants.Cower
Maybe the OP didn't seem very tied to a specific language and probably did not consider this - in my opinion valid - alternative into his decision yet. If he is not sure to use Java or C++ why not also check for a .NET solution with C++/CLR (not necessarily C#). According to me the downvotes are not justified. This is a community with a democratic voting system. Let's see what happens over time.Bancroft
WPF is not cross-platform. Also, the recompilation part is completely irrelevant. If we remove all the chaff from your answer we get WinForms, which is a decent alternative, but I personally wouldn't pick it because Mono is still much less popular than either Java or C++.Istle
G
-1

Do not use Java for creating GUIs unless platform independence is a must. The user experience will be sluggish and interoperability to C++ will be a pain.

For creating native GUI in C++, you can either use GTKmm along with a library like Boost or QT. Additionally, these libraries are available for most platforms (GNU/Linux, Windows, OS X) so your application can be recompiled anywhere.

edit: use GLADE for quickly creating GUI and fill in the signal slots with C++ code in GTKmm.

Gnatcatcher answered 8/7, 2010 at 20:20 Comment(11)
So how would say, using gtkmm in C++ pose platform independence problems?Jewelfish
gtkmm wouldn't pose any platform independence problems except for distribution, just that you need to recompile your application for each platform. using Java, you could just give away jar files.Gnatcatcher
Using Java, you just give away jar files along with compiled for each platform C++ backend - in this case.Gavriella
The "sluggish" claim is false.Stalinsk
@Andy: Agreed; "sluggish" is an understatement. ;-)Advanced
It is definitely possible to make responsive GUIs in Java. If you don't like Swing, SWT is pretty good, and there are bindings for Qt as well (haven't tried the latter personally). That said, writing GUIs in Java always leaves me with a bad taste in the mouth.Edora
this is FULL of FUD, not all Java GUI's are "sluggish", just poorly written ones. And with a clean C++ API, SWIG would make short work of exposing a C++ API to Java.Supercilious
I do love Java, have been actively using it for 6 years - while not trying to start a religious war, all I am saying is if most of your code is already in C++, I don't see a point in using Java just for creating GUI. Except for platform indep, Java doesn't offer any clear advantages over C++, auto garbage collection is overrated and is partly responsible for the sluggishness. "I've also heard (strictly second hand) that writing a GUI in C++ is a pain." -- if you use the right tools, it is no pain at all. @fuzzy lollipop "this is FULL of FUD" -- thats what she said ;-)Gnatcatcher
Well then... the Java fans are welcome to point out ONE cross-platform Java UI that both looks good and is responsive. Good luck finding one.Istle
@Istle - Eclipse and Azureus are significant cross platform applications written in Java. I would claim both look good or at least no-less ugly than most tools. Eclipse can be sluggish and consumes large amounts of memory but I would say that is not primarily the fault of the GUI. Both use SWT, but I think for most applications Swing is now perfectly adequate. It's look-and-feel and performance have improved massively since it first appeared in 1997. I think dismissing it out of hand, the way many are doing, constitutes FUD. However that doesn't mean I'm advocating Java for the given issue.Refrigerate
Sluggish is truly an understatement. For example, I'm using Intellij IDEA Community Edition, which uses pure Swing as its GUI library, and it works perfectly with 8 GB RAM + i5-3320m.Hooky

© 2022 - 2024 — McMap. All rights reserved.