Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a Swing code turn into a real graphical application

This question might not seem very technical but, out of curiosity,I want to know that:

How does bare/raw Swing code written by me, turn into a wonderful graphical application?

For example:

  • Like when we make a JFrame visible, or when we place a JButton on it. Who makes it happen? OS or Java or JVM.

  • Who makes the colors come up?

  • What is the process going on behind the scenes?

I ask this maybe because its the first time in my life that I made a RealWorld graphical application and these questions popped up in my mind!

Thanks in advance!

like image 455
hasankamal Avatar asked Jun 20 '12 20:06

hasankamal


People also ask

How is Swing related to GUI programming?

Swing in Java is a lightweight GUI toolkit which has a wide variety of widgets for building optimized window based applications. It is a part of the JFC( Java Foundation Classes). It is build on top of the AWT API and entirely written in java. It is platform independent unlike AWT and has lightweight components.

What is GUI explain the steps to build GUI?

GUI stands for Graphical User Interface. It refers to an interface that allows one to interact with electronic devices like computers and tablets through graphic elements. It uses icons, menus and other graphical representations to display information, as opposed to text-based commands.


2 Answers

In Swing, all the GUI components are written entirely in Java and are rendered using Java. For example, JButtons would be drawn by Java and not by any internal OS stuff. Thus, Swing components are called "light-weight components" because they are managed and rendered by Java instead of by the OS (or any native widget toolkit).

Note that Java also has a toolkit called AWT. Swing is based on nested and inherited methods from AWT, except that it creates native widgets using the OS.

So at some point, Swing will have to create a window on the screen and draw on it. The magic that actually creates the window is handled by AWT. Notice that JFrame extends from java.awt.Frame which means that although JFrame may be rendered mostly by Java, it is backed by a heavy-weight native OS window.

like image 188
Jack Edmonds Avatar answered Oct 04 '22 23:10

Jack Edmonds


In short, there's an AWT toolkit, which is the layer that does all the window management and drawing. It is calling native platform specific code inside the JVM. It is also responsible for java2d drawing. It can use accelerated directx or opengl pipelines.

Swing is developed on top of it. Swing actually draws every button and every object with plain java code. The drawing is handled to a current look and feels that decide how to draw components. So you can override their paint methods, and add some extra things without any problems.

Metal and Nimus LaF are 100% java2d drawn, so inside of them you will find things like drawRectangle and drawLine to draw components. Native look and feels, like windows, gtk, access current operating systems theme to draw something that looks similar to native widgets. That is why they do not always look like native applications.

There are also other gui toolkits for java, like SWT, used, for example, in Eclipse. What it is doing, is getting the window from the AWT, and then putting 100% native widgets on it. It is much better integrated with the OS, looks better, works faster, uses less memory. But with it, you'd have to distribute your application with os specific native libraries and it is less customizable, compared to Swing.

like image 20
Denis Tulskiy Avatar answered Oct 05 '22 00:10

Denis Tulskiy