How to learn Java Swing and make GUIs quickly? - java

I've been coding in Java for the past year, and i think I have solid OO and
basic OOD skills. I'm looking to build a Java GUI on-top of an existing project which is not complex, but I want to finish it as quick as possible. So I'm looking for an API that would abstract all the nitty gritty implementations and let me focus on the core of my minimal viable product.
I really prefer coding with eclipse and not wasting time trying to learn to get used to another IDE. What would you suggest I do?

Here is the crash course in swing.
First thing to learn is the layout managers.This is the big hurdle, they are finicky things and you have to master them.
Second is listeners,without them nothing happens.
Third is the JComponents themselves,you will slowly learn them as you go along. I would recommend using NetBeans GUI builder to play with them and get a feel for them.
Note that most JComponents have different models you can use rather then the default.
Also the only difference between JPanels and JComponents that I can find is that JPanels have layout managers.
A good place for various swing hacks and tutorials is here:
http://www.java2s.com/Tutorial/Java/0240__Swing/Catalog0240__Swing.htm
http://www.java2s.com/Code/Java/Swing-Components/CatalogSwing-Components.htm

Well, actually the GUI Builder in Netbeans is your best bet to make a GUI really fast. You can do it in a matter of minutes if you have the picture in your head about how it's supposed to look like. After that, the only thing left to do is to bind it to your application; more specifically: make the ActionListeners of your buttons for example do whatever they should.
This of course depends on the application's size. If it's small, then making a GUI for it is easy. If, on the other hand, it has 2000 classes, then it can be more complicated, since the GUI will probably be more complicated.
If the application can be modelled as a Model-View-Controller architecture, that would help you a lot.

I think you'll find out that there is no royal way to GUI in Java. Every approach is a pain in the (*). Even if you use a visual GUI builder, it will STILL be a pain in the (*). As a long-term investment in your skills, I recommend you to bite the bullet and learn to build the GUI programmatically. That's what every experienced Swing dev does anyway, so better head for that goal straight away. You could also start with SWT/JFace, but that APi is more difficult and quirky than Swing. RCP adds yet another layer of quirkiness and complexity over SWT/JFace, so I'd stay away from that while still a beginner, and in fact at all times except if making a massive GUI application.

Sounds to me that eclipse RCP could be your friend (if you're looking at a rich client solution). Just grap a distribution of eclipse for plugin development (much faster than installing the plugins to your existing copy), create one of the sample projects and start hacking.
For web applications, I'd recommend the playframework. It integrates quite well with eclipse.

Related

Custom Java Swing GUI vs. Netbeans Drag n' Drop Editor

Are there any advantages to hand-coding your Java Swing GUI rather than using the Netbeans Drag n Drop editor? Maybe such as run-time? It seems to me that the Editor auto-creates quite a bit of extra code but I don't know if that has much impact on the runtime of loading the views. The only reason I'm currently using the editor is because it makes the GUI building quick and simple. But are there things you can do only by hand-coding that isn't possible through the editor?
Also is there a better GUI Framework? I've peeked at Java FX, but I really want the kind of effect that the Github desktop software has. It looks so slick and clean, almost like a webapp. Although I'm sure they did that in C++. It just seems to me like there isn't much for Java when it comes to graphics. One of my friends commented that all Java programs look the same and so far it seems true.
I know there are similar posts, but quite frankly they aren't very recent, such as Write gui programatically, or using an advanced gui editor (Java Swing)? which was posted in 2010 or Learning Java Swing (GUI builder or not?) which was also in 2010. If there's nothing new, sorry to bother you.
Are there any advantages to hand-coding your Java Swing GUI rather than using the Netbeans Drag n Drop editor?
There are lots, but lets start with the obvious...
Doesn't lock you into a single development environment. This means your code is sharable to others who may not be using Netbeans. This is of particular importantance when you are using things like Maven based projects
Encourages you to learn the API. You get a good understanding of how the API actually works and learn plenty of tricks to make fluent and flexible interfaces.
Encourages greater separation of respobsibilty. It's all to easy to simply keep dragging components onto a form, which does nothing for encapsulation or management. By dividing your UI into areas of responsibility it makes it easier to mange the distinct states and relations between your components. It also makes the code generally easier to read, as you don't need to keep discarding content which is not relevant to what you are trying to figure out.
Better extendability. Extending components is not easy at the best of times, but is made next to near impossible with form based components (or at least an order of magnitude more difficult)
Netbeans form editor code is...a mess...
Don't get me wrong, I use the form editor, but I've been hand coding Swing GUI's for ten years before I picked up the form editor. I like the form editor for laying out final screens and tend to use hand coded UIs for every step up to that point, putting the pegs in the hole so to speak.
The short answer is - until you can make a multi-level GUI by hand, stay away from the form designers - IMHO.
I know some people think they make great learning tools, I think the opposite, I think they allow you to develope lazy and bad habits which will affect everything you do into the future.
While the development enviroment shouldn't make a difference, not everyone will want to use Netbeans, best to learn to do without
Also is there a better GUI Framework?
That's rather a matter of opinion. Swing brings a concept of skinning through the use of the look and feel architecture, which provides you with the ability to change the way that components look and feel.
JavaFX provides similar functionality through the use of CSS
Swing is also highly customisable, it's very easy to get into the "paint" level of the components and even generate your own should you want to.
The ability to make a "slick" UI isn't about the toolkit but down to the developer. It's easy to just drop fields and other components onto a container, but it will take a developer with a good understanding of the underlying API and some talent for what actually looks good to make it look "slick".
Pretty doesn't always equal functional. The best UI's are ones you don't have to think about, that don't get in your way and just work. When you've been working with a program for hours, days, weeks, you're not going to be looking at how pretty the UI looks but how well it lets you get what you want done - IMHO
The #1 thing that hand-coding accomplishes that the GUI Builder cannot is: dynamically changing interfaces.
For example:
If you wanted to build a form that involves several JTextFields, but you aren't sure how many there will be when you build your GUI. The program will need to populate the JTextFields dynamically. This is impossible with a GUI Builder.
Also, when you have complex interfaces, the resizing behavior of GroupLayout can be cumbersome and confusing. On the flip side, hand-coded layouts are a lot of work when you are writing a complex interface (but, at least their resize behavior is predictable).
A classic GUI builder should be used for prototyping your GUI. Once you know exactly what you want, you should get rid of it and clean the code (factorize it, document it, ... in short make it simple & readable).
One of my friends commented that all Java programs look the same and so far it seems true.
Not absolutely true: JavaFX look is customizable through css. Swing is also fully customizable through the LAF system or by overriding the paint* methods (more time-consuming I admit).
If you look for something easy without the drawbacks of generated code I recommend you have a look at ReflectionUI: easy appearance customization (and that's not the only benefit):.

Should I learn swing to make GUI or I should simply use any IDE

I want to know that what would help me in the long run like what the industry's trend is?
Does everyone uses GUI builder, or is it like that java isnt popularly used for GUI and acts mostly as a background operations language?
please guide me..
IDE may not be enough for designing everything (but it helps lot in development), its always better to learn API and IDE both together.
I would learn Swing, because it is useful to know the general ideas of how it works. As for actually generating GUIs, I would use and IDE builder because it's faster. You may find that it's useful to know how to manually fix up the generated code, so knowing Swing would be useful. Often you want to customize how part of the GUI works, and you would have to use the language directly to do that. The IDE builder can be a useful tool for quickly building most of the GUI.
This is just a comment, I am no expert.
My Recommendation for Java is to learn how to do in swing manually.
However I am recently using the Windows Builder Pro that is included in the latest Eclipse Juno. And it creates the code, so you can go back to direct coding without compromise.
There must be better out there, but for my small and simple apps saves me time.
https://developers.google.com/java-dev-tools/wbpro/userinterface/
It is included in Juno just right click a JFrame type class and select Edit with Windows Builder.
You may use the IDE tool, create the code then see it and enhance or add as you like. But for a starter you can add a Full menu, buttons and several controls in no time while maintaining a single .java file.
I suggest you learn swing because GUI builders functionality is limited. But it is ok to use GUI builder if you are developing simple apps. I prefer coding my own UI
You should definitely go for Swing. Using an IDE may seem easy in the beginning but you have to remember that it is way too restrictive. And if you do end up learning swing you can take the thing to the next level, if you know what I mean. It also depends on whether you like to write code or not because if you don't then naturally swing will not interest you. Here's a tutorial. Hope it helps. :)
If you want to be a professional you'll need to master Swing along with all its nuances such as the LookAndFeel and Layouts. NullLayout was my favorite layout of choice while getting started, but you'll definitely need to learn GridLayout and GridBagLayout as well. Mastering the behavior and listener models will require work, but are a must for any mid-level and beyond GUI programmer.
The IDE builders are good for getting it started, but I would be leery of trusting the code that they generate, especially if you have to be personally accountable for it. Even if you must use an IDE WSIWYG you'd better make sure you know what its doing.

What to use for writing a user interface?

I've written a series of classes to interperet data from a serial port. This data needs to be displayed on the screen. I am trying to go for straight up eye candy with a refined look.
Swing UI- Easy to program, yields mediocre results
Open GL- Hard to program, yields good results.
Are there any other options?
Give SWT a go, the API is a lot simpler than Swing. Here is a recent question outlining the differences between SWT and Swing.
I'm not aware of any alternatives, but in my opinion it is possible to write beautiful GUI with Swing. Just changing the ugly default look and feel goes a long way. Visual appeal just doesn't seem to be a top priority for most Java coders and therefore you have to make an effort to make anything pretty.
JavaFX maybe? Didn't tried it yet, it obviously offers some eye candy.
You can use NetBeans combined with JOGL (Java bindings for OpenGL) or simply with Java2D.
NetBeans has an easy GUI Builder for Swing applications -- you can make beautiful interfaces (if you get into it), easily change the Look&Feel, etc etc.., and you can use Java2D or JOGL for visualizing your data and embed it in the GUI to display it.
There's a plugin for JOGL for NetBeans so getting started is hassle free.
use html, css, javascript. Much easier to get wizz-bang UI than using Swing or related technologies.
For my projects at work, we have purchased a licence of JFormDesigner.
This is really a great tool that saved us a lot of time.
Writing Swing application by hand can be a real pain and is awful to maintain.
This tool choose the right approach : The model-view-controller.
The GUI builder creates XML files for you that represent the interface. These XML files are loaded at runtime and transformed into Swing objects. The builder also generates Java stubs that you can fill with event handlers.
It also handles Localization and nice layout goodies.
Give it a try.

Write gui programatically, or using an advanced gui editor (Java Swing)?

I am planning to write a Swing-based application (using Netbeans 6.8).
It seems that Netbeans has a very advanced GUI Editor... Still I have my doubts regarding the code generated by it. Additionally I don't like the fact the part of the code is locked (still I understand the need).
Has anybody used Netbeans GUI Editor with success ?
Does it "scale" ?
Yes, the NetBeans GUI builder is one of the finest around. I was particularly impressed with how well it handles the infamous GridBagLayout.
Borland JBuilder was in my experience the very best round-trip GUI editor; if I remember correctly, it produced code without "locked areas" and would usually continue to work gracefully with GUI code that you had manually tweaked.
Still, I have yet to find a Java Swing GUI editor that doesn't occasionally choke on a complex layout, i.e. enter a state where further editing is no longer feasible and/or requires some backing up or fixing.
Thus, I prefer to hand-code all my GUIs. Heinz Kabutz of JavaSpecialists tells me his GUI editor is vim. However, I admit that most of my other friends and colleagues consider me crazy for doing this.
With the newest versions of Netbeans, you can share projects between Eclipse and Netbeans. So, if you don't like coding in it much, you can use it just for the GUI features and then edit the code in Eclipse. Make sure you create your project in Eclipse first, and then use the importer. They will share the same directory.
http://netbeans.org/kb/docs/java/import-eclipse.html
I have always prefered to write my Swing UIs by hand. I've tried Netbeans but I just didn't trust it to do the right thing for a complex UI (although it was quite awesome for a small simple UI).
I think a lot of us fear writing Swing Layouts by hand because of GridBagLayout. Its not that hard once you've used it a few times. I used to swear by it. However, recently I've moved to a new job and their existing code uses TableLayout instead. Its much simpler and seems just as powerful to me. I've grown to prefer it to GBL. I think you should consider it if you're new to writing Swing.
I'm sorry but I'm not sure about your question of scale.
Best of luck!
Funny side story about JBuilder. My friend who taught me swing taught me to code it by hand, and he always had me put the layout code in a jbinit() method. For several weeks I assumed this was just some code style he used. It was only later I learned that it was an auto-generated method of JBuilder at the time. I didn't need to be using it at all. I was such a n00b :)
I recently (4 weeks ago) switched from eclipse/SWT to Netbeans 6.8/Swing (after being fed up with some plugin licensing issues in Eclipse). I came in cold not being familiar with either Netbeans or Swing and was quickly impressed. I managed to convert 4 years of SWT experience into Swing experience very quickly and now consider Netbeans the best Java GUI editor. Whilst some parts of autogenerated code are read only - this is a good thing. Eclipse allows you to edit the autogenerated code and this can cause major headaches with layouts, alignment and so on if you make a mistake. Netbeans allows you can create custom 'event handling methods' (my terminology only) which gets the autogenerated block to invoke your methodd at particular points in the lifecycle so you can access the 'read only' code whenever you need to.
So stick with NetBeans, its the best Java Swing environment there is.
You do NOT want to code by hand, for any reasonably sized app. Let the GUI do most of the plumbing for you (especially when using the the different layouts - this alone will make you tear your hair out), so you can focus on the service/business logic.
I've used netbeans, WindowsBuilder plugin in eclipse, and resorted to just writing Swing code by hand. I definitely appreciate the fact that netbeans and windows builder write the swing code for you, but I like knowing every inch of the code in my project and know where to go when I want more complex and add details. Having said that I think netbeans is worth a try too but I was a bit nervous of going through the generated java code, whereas if I had written them from scratch, I don't have this worry.
WindowsBuilder plugin for eclipse totally screwed up my eclipse, the content assist became permanently slow, even after removing it. While the Swing Data Binding framework has some learning curve (at least for me because I didn't know it was the same as JFace Data Binding Framework), I had trouble finding documentation for it, and was referred to the JFace wiki....
Overall, you are right, I think you do get to think more about your application logic than writing plumbing code spent on the gui, but if you write them yourself it just gets faster and faster. I also don't like the fact that I need to be dependent on a gui editor to create my application.

How can I create a GUI in Java using only code and not the graphical drag and drop? [uber-newbie]

I've been told that creating GUI's using drag and drop generates unnecesary code and is frowned upon by most Java developers.
I'm used to dragging and dropping things using Windows Forms **but ** I'm also very comfortable creating things using XAML for WPF applications.
I'm a bit lost however on how to create a basic GUI in Java using only code.
What I'm using: Netbeans 6.8
What I want to do: Create a simple Window that will act as the main one
that opens up when the application
launches.
How can I create a simple Window with the standard window buttons (close, minimize, maximize) and have a little button on the middle of it. Using only code.
Also, what type of project do I create using NetBeans?
Thanks for the help.
I feel like this kind of question is best answered with a link to a tutorial, so here you go:
From java.sun.com, Trail: Creating a GUI With JFC/Swing
The link provided by danben is really helpful. If you really want to lay out everything from scratch, then you have to be able to answer such questions as
What is container? Which components are containers?
How components are positisioned within a container (layout manager)?
The link provided will give you the answer.
I heard that one before. I don't know what is with java developers and always insisting that you have to write the gui by code and can't use drag and drop tools. For me I don't care if it generates unnecessary code. I personally don't think it will make that much of a difference.
The only thing I would never recommend is using a designer for websites since they usually never look the way you designed it when you actually test it on a browser but for windows GUI just use a designer.
I look at it this way. There might be a bit more of unnecessary code but your bosses won't care about that. They will be more concerned why your taking so much longer on something and costing the company so much more money on little to no effect to the final product.
To answer your question you probably can pick up any book from java and they will have tutorials on how to make GUI's by code.
To do it programatically, you have to be able to understand code in general. As far as I know Java does not have a simple XML based layout language for GUI-s. So first you have to learn how to program. (But you can try JavaFX's script language, might be easier to master first.) You can easily learn your way in the simple cases from the generated code, then continue using swing javadoc and google.
GUI builders are handful, nevertheless. It is a plausible that once you are required to mock up a GUI layout as a prototype, tracer bullet, presentation material etc. Generated code might be even completely irrelevant.

Categories