Java developer moving into Java Desktop development - java

I've been writing Java web (JSF, Struts, JSR168) apps for a number of years. With the recent departure of a colleague, it looks like I'm going to be building more client applications. I've used Eclipse since the beginning and I'm really comfortable with it. It feels as though SWT is waning (just an opinion based on literature I can find) and Swing is surging.
My question:
Is there value in learning Swing through writing apps by hand in Eclipse (as opposed to using Matisse in Netbeans)?

Yes, it is very valuable to learn coding Swing apps by hand. One reason for this is that no GUI-Designer I know always does the things you want them to do. The other - and in my opinion more important reason - is that most GUI builders (especially NetBeans') generate all and everything into one single class. This can be very annoying because the maintainability is reduced a lot (separation of concerns). Also many GUI builders have blocked regions, i.e. you cannot modify the generated code by hand. Or if you do the GUI builder will overwrite it the next time you use it.
However that standard LayoutManagers coming with Swing are very complicated. This is why I suggest that you give MigLayout or JGoodies FormLayout a try. On the JGoodies site you also find other libs for alternative Look&Feels, Binding and Validation, which can be very useful.
I would also suggest that you have a look at the Presentation Model pattern (Martin Fowler), which helps a lot if you want to unit-test your GUI-behaviour.
If you then are motivated to improve the design and add some very cool effects to your application check out the book Filthy Rich Clients.
Hope it helps.

Yes, definitely - even if you plan on using Matisse most of the time, you will benefit from having at least a reasonable understanding of the Swing code under the hood.

It sure will help. Maybe you'll reach the end of what Matisse can do, and want to tweak some of the code by hand. At this point it will be better to know what happens under the hood.

Note that using Matisse and writing Swing by hand are not mutually exclusive at all. Matisse just produces Swing code, and you can customize it. You can make part of your GUI with Matisse, and the part next to it by hand, for instance. Get the best of both worlds - that's how I do it, at least.
Fast way to learn how to do something in Swing is to do it in Matisse, and then see the code it produced.

You definitively need to know how Swing works since you will most likely have to adapt and change things which the GUI prgram expects you to know how works and what it is called.
The Java Tutorial has a good Swing trail: http://java.sun.com/docs/books/tutorial/uiswing/

Related

Swing Generator

I need to develop some java gui using swing.
A few years ago I did develop a bit with swing.
But it was pretty exhausting, you see, back than there weren't much tools to help you.
But I do believe today it should be easier, there must be tools.
I would like to use some kind of a generator or maybe a utility or even a framework.
I know Eclipse has a few plugins and Netbeans has a nice built in tool. I have also heard good things about Jide framework.
But still, I don't really know any of them.
Is there a tool/Utility/Generator/Framework that you have used and really helped you?
Obviously I would prefer something that is free, but if you know something that is really really good and cost money, that could work too.
Thank you.
Actually the faster way to write Swing frames that I've found so far is to use Groovy SwingBuilder but you have to include groovy's jar inside your project to use it. This is an example taken from their website, it's mainly synctactic sugar but it helps:
def frame = swing.frame(title:'Frame', defaultCloseOperation:JFrame.EXIT_ON_CLOSE, pack:true, show:true) {
vbox {
textlabel = label("Click the button!")
button(
text:'Click Me',
actionPerformed: {
count++
textlabel.text = "Clicked ${count} time(s)."
println "Clicked!"
}
)
widget(sharedPanel())
widget(sharedPanel())
}
}
Otherwise there are some "visual" approach to the problem but they don't let you integrate so quickly with the interface you wrote (for example jvider), NetBeans and Eclipse have some plugins to do the dirty work but as before: you can't integrate so seamlessy.
For quite a few years I've used NetBeans built in "Matisse" form editor. It's got the advantage of being built into the IDE, so that the form editing and code generation is rather seamless. If you're new to Swing it can be a big boost (as long as you continue to use NetBeans as your IDE).
There are some issues, however:
You can't easily move a form to another IDE. The generated code may work, but the form data itself is stored an XML file.
If two more people edit a form at the same time, conflicts can arise in SVN/CVS that are nearly impossible to reconcile. The XML is not really human readable.
In the past I found the form XML file to corrupt itself, which would lead to form generation errors.
The form editor can get a mind of its own sometimes, destroying your arrangement when you do something as simple as resize a control.
In spite of all the above, I've continued to use it as nothing else has seemed appealing to me.
My suggestion would be WindowBuilder Pro. It's a GUI designer that comes as a plugin for the Eclipse IDE.
You can use it to generate Swing, SWT or GWT GUIs and even generate some Eclipse RCP code if you need to. I must say that it works quite well for me. I haven't used it to generate any Swing code, though, as I prefer to work with SWT instead.
In terms of GUI Builders, NetBeans and IDEA (now going open source with the 9.0 Beta) both have free GUI Builders.
GUI Builders have all the kinds of downsides that Jason spoke to. If you want good control over what is going on, but still want something sane to write a complex GUI by hand, check out MigLayout.
Eclipse's Visual Swing is a promising upstart. It isn't the most stable thing in the world, but it does all the form stuff in code, so there are no separate files which are hard to read and merge in source control. When you first try it, it takes some getting used to. But once you learn to work around the bugs you pretty rapidly develop Swing GUIs.
I would go w/ matisse if you wnat to use a gui builder, and there are shortcomings as Jason mentioned, but you also need to include a jar file that is used by the matisse layouts, this may or may not be a problem.
But if you are going to be developming UI's in swing, you will soon come to find out most of the gui builders have many shortcomings, and when you have to change something the generated code is not always clear and is bloated. You will also find that once you become proficient you can write a ui much faster by hand than the gui builder, and you will really understand what is going on.
So I say stay away from gui builders, you will create better and more maintainable UI's, and w/ some time and experience will not want to use a gui builder. Just start w/ some of the basic Swing tutorials on layout.
May I humbly suggest Metawidget?
It takes a different approach to UI development - removing much of the error-prone, tedious work whilst still retaining the flexibility of traditional toolkits. It integrates with your existing front-end (Swing, in your case) and back-end (Java, I'm guessing), and works at runtime (no code generation).
There's a few short intro videos here:
http://metawidget.org/videos.html
If you get chance to take a look, I'd be most grateful for your feedback.
Regards,
Richard.
You can avoid GUI builders generated code by using ReflectionUI.
Advantages: you instantly generate your GUI and then you customize it visually (or with few code if you need something very specific).

Building a GUI in Java [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
What mechanism do you prefer for building a GUI: from scratch or using GUI-building software?
If the question is about GUI development using Swing then IMO it is best to code everything manually without a WYSIWIG tool.
In order for a manual UI design/coding to be efficient a really good layout manager should be used. MigLayout is one of such layout managers. Since I've started using it I have never looked back at WYSIWIG tools -- a sheet of paper, a pencil, MigLayout and you get a full control over your design and code.
Also tools like Balsamiq simplify making GUI mock ups for quick prototyping if the paper and pencil approach is not suitable.
My personal opinion is never use visual editors. Until there is a single standard for GUI-building in Java you will find yourself tied to an IDE. The best IDE for building Java GUI has switched around quite a bit in the last 10 years (I wonder how many projects were hit by a massive maintenance overhead from switching away from JBuilder for example).
For most applications, the actual layout of GUI code is probably something like 5-10% of the total amount of time you are likely to spend writing the app as a whole. Therefore even halving this by using a designer is not really saving you very much in the grand scheme of things.
Taking this together with the lack of flexibility and more complicated maintenance, it's almost always better just to write the GUI by hand.
I actually enjoy building GUIs with the NetBeans GUI Builder; the thing is, it is fairly customizable - it allows you to change the code used for auto-generation and the auto-generated code [no pun intended] (which is necessary for custom components), it also allows "easy" event-handling and binding. And NetBeans GUI Builder is not restricted to GroupLayout but rather allows you to choose which LayoutManager to use (a somewhat hidden setting in the form's properties dialog).
But it is not always the best choice; as others already said: MiG Layout is a good choice when you need to have full control of your application, knowing every single JLabel, every JButton etc.
I must confess I prefer Netbeans' GUI builder for the ease of use and WYSIWYG view.
But I also think that it does not even matter if you have created the GUI-Code by hand or with a GUI-Designer; adding Controls / custom Components dynamically works with both, doing this and that works with both etc.. The only thing that matters is that it works.
(From my point of view I do not see a point in coding a GUI manually if it can be done faster with a GUI-Builder, as I do not see why one should bother coding it by hand without some kind of benefit)
I use GUI builders to rapidly prototype designs and stuff like that. But when it comes down to building the actual production GUI, I code it from scratch. I'm not morally opposed to GUI builders, but I prefer the greater control over my code I get from writing it by hand.
Like any code generator tool, IMHO, you shouldn't use a GUI builder until you at least understand the concept - in this case, the Swing layout managers are particularly tricky.
If you give me the choice between NetBeans and by-Hand I'd definitely choose by-Hand. I was never really convinced by the NetBeans GUI builder's approach. At least the last time I used it, it relied on some proprietary sidecar files...
Look at Visual Editor for for Eclipse
You will still want to understand what the editor is doing for you.
You need to know how Swing works to be able to make things work properly. Nothing like building by hand to learn that :)
THEN the Netbeans GUI builder is rather nice.
Edit: I would strongly recommend going through the Java Tutorial Swing Trail - http://java.sun.com/docs/books/tutorial/ui/index.html
Both - use a WYSIWYG editor to get the ball rolling, but at the end of the day you'll need to manipulate your controls and in some cases create them programmatically at run time. Eclipse has a variety of free editors as plugins. Go to eclipse.org and look through the plugins listing.
Depends on your requirements. Some GUI building software work really well with simple applications, however, if your client requires many custom components the third party GUI builders may not work.
Also, it depends on the amount of forms and the complexity of them. If you need to build a form for each table in the database, for example, a third party GUI builder may be faster and cheaper.
In my experience, it really varies from project to project.
If you want a complex gui, then use something like netbeans to build it and create all of the events. If you're looking for a fairly simplistic gui, then building one by hand is still an option.
My company uses Netbeans for GUIs and eclipse for the regular programming.
We have looked but we haven't found any other (free or at least cheap) tool that does GUIs as good as NetBeans.
I used to write them from scratch with a plain text editor...
That depends what the reasons you're making the UI for,
I found that there are two reasons to make UI:
First is for clients, in these cases you want to UI to be polished and to handle really well in projects like this the core or essence of the project is the UI itself.In these cases it's usually the best to hand-craft the code for your UI because it's probably non-trivial code.
Second reason is usually quick & dirty UI where the core of the program is something else and the UI is created usually for Control aspects on your main application and you are the main client. In these cases the best thing to do is make the UI as quickly as possible as you don't want to "waste" time working on it and its more legitimate to use WYSIWYG editors.
I always find a GUI builder to be a good idea to begin with and then I end up in a situation where I am wasting my time fixing things to my own ways rather than that of the IDE and making them actually suit. And don't get me started on the messy code that GUI builders generate! Hence, it's often more efficient to build from scratch; at least for me...
I prefer hand coding the GUI myself so I can have complete control. For complex layouts I will use JGoodies Form Layout. Their syntax for specifying the layout makes it easy to draw the GUI on paper then transfer this to code.
I tried the Netbeans GUI builder and was impressed by how easy it was to use. Then I looked at the code it generated using GroupLayout and decided that by going that way you were basically locking yourself into Netbeans which was something I did not want to do.
I started by coding it manually using Swing. Now I use WYSIWYG editors, but I think it's important to know what's behind the editor. Also you might need to update the code manually, WYSIWYG editors often can't do exactly what you want.
Having created Java GUI's extensively, I can assure you the process is quite often painful. I'm well aware that Java's strength does not lie in it's GUI, but I'm surprised that a decent free interface builder tool hasn't been released.
Sometimes I'll use Eclipse's Visual Builder tool for quick and dirty mock-ups. It's code generation is a bit scary IMHO. I believe Netbean's Visual Editor is the best available at the moment (for free anyway); however, I find myself hand rolling GUI code for consistent cross-platform layouts. If your disapointed with these free GUI editors (like me), quite honestly, just use a layout library (like MIG) and just do it yourself. It'll be slow at first, but you'll have a lot more control and less editor newonses.
It really does depend on what you are trying to accomplish with the GUI. When I was tossing together ideas for a Computer Science class and testing functionality, the Netbeans WYSIWYG editor was great since it automatically generates a lot of the code for you. However, I found you tend to get better control with doing it by hand (in Java) than learning Netbeans.
I tried to use Eclipse's Visual Swing plugin, but on some occasions, the code it generated just disappeared, and I was left with a screen full of errors. In all the GUI apps I write, I use Netbeans, even though I hate the code generated when using GroupLayout. However, if layout code is not your concern, I think you'll be very pleased with the development speed it offers you. The only thing you'll have to do is implement the event logic and other logic you may require.
Eclipse's Visual Editor is a great choice, because the code that it generates is very straightforward and there's no hidden magic. Plus, you can make changes by hand and the editor will reflect them.
I test-drive the model of the UI, so that I have the functionality of the UI enclosed in a class that is completely decoupled from the GUI library and can be easily tested in isolation. Then I create a thin wrapper layer on top of the model, so that the GUI's event handlers are mostly one-liners that delegate to the UI model.
I've been using mostly IntelliJ IDEA's GUI builder (using the JGoodies Forms layout manager). It has been quite handly for laying out components in a static layout. But I think that for example with MiGLayout it's quite easy to write the GUI layout code by hand, so a GUI builder is not necessarily needed, and it helps to avoid tool lock-in. And dynamic layouts will anyways need to be written by hand.
But before writing any code, I first design and test a paper prototype of the UI, so as to have a clear vision of what needs to be done.
If you aim to create the final Java GUI, I recommand the Eclipse plugin named "Jigloo GUI Builder", which can generate clean and maintainable Java code.
If you just want to create the GUI quickly for review, you can try the UI prototyping tool like "ForeUI".
There's nothing wrong with using a GUI Designer when :
You understand what the IDE does and what it generates, how to customize the IDE output
You understand the code actually generated by IDE
Understanding at least the basics of Swing is important. Imagine the following scenario which happens just too often.
Q:"I have 10 years of experience writing swing applications"
A:ok
Q:How would you debug this code
A:Oh wait, there's no Netbeans form file??? You guys are not high tech!
Q:Hum.. you don't understand it?
A:Of course not, it's like machine code, you need to refractor it all and use an IDE!
I find that GUI builders can often complicate things and I'd rather know what was going on when the GUI was built than just use the variables created. I think because I don't know everything about Swing and layout managers as well, I am afraid to generate code that I have no conception of or don't know truly how it works.
When I was doing simple scripting using AutoIt that I knew would only be used on machines that had big enough screens I would use the GUI form editor. However, with commercial applications and those which may run on machines with any specifications I would generally shy away from GUI builders.
That said I think my decision may be influenced by my lack of knowledge regarding layout managers and maybe in the future I will start to embrace them more, as I can see nothing wrong with using them when you have a full understanding. Unlike some programmers may state, it is only a lazy way of building a GUI if you use the software with lazy intentions, and not for efficiently as the applications were written for.
To answer the original question directly, 99.5% of the time I will write the GUI from scratch!

Is XML or XUL the future of Java GUI building?

After spending a lot of time and code on programming in Swing, I thought this can't be state-of-the-art Java GUI building. After not finding a user-friendly visual gui bilder for eclipse I stumbled upon declarative GUI building with XML UI toolkits... and I thought: This must be it! I think it's the right way to go, easy and also close to web-programming.
But after looking around in the web and on SO, I got the impression that it is not very common! Although there are many implementations and APIs, it seems like most of them are kind of dead and had no updates in the last 5 years..
So I wonder: Is my feeling right, that XML is not very widespread for java GUIs? And if so - what are the reasons? Maybe it couldn't become accepted or it has some major drawbacks or people are doing everything in the web instead with fatclients or there are better alternatives, maybe javafx?
I just need to know if it is worth spending time in that area or better look for alternate ways. As I dont read developer magazines I just don't know what the trends in gui building are and which technologies are believed to have a future. But I can't imagine that people still spend so much time on writing nasty swing (or swt) apps.
There new fresh and interesting approach - it uses YAML. Check it out at http://code.google.com/p/javabuilders/
Sun's answer to that seems to be JavaFX.
It has a declarative language for specifying the GUI and there will be builder apps as well.
There is some prototype work going on for e4 (Eclipse 4), which would allow building a GUI by editing an Ecore model and customizing it via CSS.
See this blog post for some details and instructions to try it out yourself.
Five to ten years ago XML was very popular. Although fine to provide some kind of standardisation for transferring data between heterogeneous systems, it's not suitable for programming. It always starts with the easy stuff, and lets declare everything. But any real system requires code. XML then falls down. There is also the problem that XML is a bad syntax for humans, and even languages like Java are easier to read.
On the other hand, there is clearly a need for the rails of GUIs. Naked Objects is the closest reasonable attempt I have seen.
I came to the same conclusions as you about the declarative frameworks out there. It is not worth learning a new GUI syntax unless it's widely supported. XUL as an interface language is widespread, but there is no java rendering framework for it. I'd say HTML+CSS+Javascript and a Servlet container is the best Java platform for GUIs today, but sadly I haven't found a platform independent way to display web pages like a desktop application.
If you use Eclipse, you can now use WindowBuilder to help you in creating Swing apps in a user-friendly visual GUI builder. It's now available as a free download, and Google has donated the Swing GUI builder framework to the Eclipse Foundation.
I'd second Thilos suggestion with javaFX.
Additionally the trend is pointing to webapps, so I think that in the long run javaFX and web based UIs will catch up to swing+swt GUIs.
I do all my swing stuff by hand and none of my application is nasty. If you do not know how to create usable and good looking UI there is no technology to help you.
The answer is surely not going to be XML. What problem are you trying to solve? You want to have reusable building blocks, and a compact way of describing them. I don't see XML helping you there.
[edit] Creating a java code equivalent of XUL would be an immense improvement on XUL.
XML makes it really, really hard to do good separation of concerns and once and only once. It is however perfect for the mongolian horde approach. You need a layer on top of swing, it only provides the building blocks.

Is using a visual GUI editor detrimental to learning GUI design?

I've played around with the netbeans visual editor for java and it seems very intuitive and simple to use but I can't help but think: is this detrimental to my learning? Should I be getting my hands dirty and doing everything manually? How do professionals in the field handle user interface design?
What would you guys recommend I do?
The main drawback of GUI builders is that they make it easy to do simple things and very difficult to do complicated things. It's the same as building a website with a real design tool vs. using a simple website builder like iWeb or FrontPage.
The three most important topics in GUI framework are learning the event model, the layout model, and the intricacies of each widget. GUI builders isolate you from the latter two and kind of restrict your exposure to your first.
In the long run, it's detrimental to the things you can achieve, and therefore detrimental to your mastering of GUI creation.
In addition, maybe behaviors that are very important to the users (e.g., blanking out one widget in response to change in another) are quite tricky to write, especially with automatically generated code. Complex GUIs often rely on models, and GUI builders often through everything together, so it's much easier to end up with horrible unmaintainable code.
For learning design I think a visual GUI editor is fine. Design has nothing to do with what tools you use to create the design. If your goal is to learn how to create a pleasing user experience a visual editor might be very helpful.
For learning how to create an application with a graphical user interface I think it may be detrimental, though that really depends on the individual. At some point I do think you need to "get your hands dirty" so you can learn exactly how widgets interact with business objects and with the user.
I would recommend that for every new component you use, you first handcode it at least once just to make sure you understand.
But theres no need to get redundant and just spend all your time coding locations and sizes when the visual editor does it for you.
What I would do is just make sure you understand child forms and parent forms and leave the repetitive technical things to the editor.
Understand the underlying code. If that means practice by writing a form window so be it.
However, don't waste time on it or pain yourself. It is hardly ever required to actually go in and play with the underlying generated code.
I would definitely not recommend getting too far consumed by it. It is simple and repetitive code that is generally quite simple to figure out when the time actually shows it self.
I recommend getting your hands dirty with the underlying parts of the code. The problem I find with Visual Editors is that they can often generate unclean code, or code that is difficult to understand when someone else needs to make changes.
Once you have a clear understanding of JComponents then you can play around with the visual editor to understand the code that the editor generates. I recommend playing around with the examples provided by the Sun Website for Java GUI programming.
i use GUI editors just to make the template/repetitive tasks through applications, and make the dynamic elements by hand

What's the definitive Java Swing starter guide and reference?

Obviously the Java API reference, but what else is there that you all use?
I've been doing web development my entire career. Lately I've been messing around a lot with Groovy and I've decided to do a small application in Griffon just to experiment more with Groovy and also break some ground in desktop development. The only thing is I'm totally green when it comes to desktop apps.
So, world, where's a good place to start?
The Swing Tutorial is very good. Apart from that, the Swing API is obviously the reference, however it's also a treasure trove of fairly good source code! Add the API source to your IDE and you can jump directly to the implementation to all the Swing classes. This is a great way to explore the functionality, see how various Swing components work and learn a good Swing "style". Furthermore, it's great to be able to step through the API classes if things don't seem to work and you have no idea why! Adding the API source to the IDE has the additional benefit that you get all the JavaDocs along with it, although all modern IDEs can also pull them from the net -- you do not want to program desktop Java without the documentation available from within the IDE!
NetBeans and other IDEs do make the creation of IDEs very easy, but be aware that there is a lot more to Swing than just containers and layout managers. In fact, containers and layout managers are among the easier things, and I'd recommend learning to use them by hand, too. There is nothing at all wrong with using a GUI builder, but in some cases it's overkill, and then it's nicer to just quickly whip up a GUI from source. In other cases you need to be able to create a GUI dynamically and then GUI builders are no use at all! For creating very complex layouts from source, I recommend FormLayout, which has its own set of quirks, but which does scale (in terms of programming effort) to very big frames and layouts.
If you've only done Groovy so far, you'll be surprised how well documented Swing and the rest of the Java API is and how well everything is integrated. It might also take some getting used to a different style of programming, using the debugger more often and println-debugging less, etc. There might also be some "boiler-plate" code that will be very annoying. ;) Enjoy.
The Sun Java tutorials are pretty good. I cannot vouch specifically for the Swing one as it has been ages since I've done any Swing development and I have not read it myself.
Creating a GUI with JFC/Swing
When it comes to developing java desktop applications, I would highly recommend using the IDE environment Netbeans. Especially when it comes to the development of Swing based applications.
I recommend you to play around with netbeans. It will allow you to build complete GUIs using only your mouse. Once you get familiar with Swing components, start using the Java API. Thats how I started.
The O'Reilly Swing Book is a pretty good reference, it has a good overview of general Swing concepts and covers each of the major classes. I used it recently when I had to refresh my memory on Swing.

Categories

Resources