What are the lightweight/ heavyweight components in WinForms? - java

I know there are lightweight components and heavyweight components in AWT and SWING. But what is the counterpart to those in C# .NET WinForms?

In AWT, light weight components are written in Java and directly drawn to the screen by Java but are not known to the OS, where as heavy weight components are provided by the OS.
In WinForms all components are provided by the OS or derived from Control which is provided by the OS. Therefore this distinction does not exist in WinForms.
However; you could, in theory, have your own drawing logic drawing your own light weight controls on a form. See: https://stackoverflow.com/a/22354467/880990. If you need to interact with these controls, you will need to create hit tests in order to see which one of them was clicked and create your own interaction logic.

Related

Can I apply a Swing look and feel to AWT widgets?

I'm developing on a legacy Java desktop application mixing Swing and AWT controls. I can successfully apply the system look and feel to Swing controls of course, but can I do the same for AWT ones? If not, can I implement some bridge in any way?
The basic answer is "no". The AWT components are backed by native OS components, either directly (via OS supplied libraries) or indirectly (via AWT based native components). It's possible that AWT will be linked to "older" component libraries so some of the components won't match the current OS "look and feel", which will be using newer component libraries.
In either case, you can't effect how these components look (and certainly not through Swing's Look and Feel API).
Ok, this might be a little hear say, but The Story Of Awt says "Java's GuiToolkit had to use the native widgets provided by the host window system, whether Windows, Motif, or the Mac. Mimicking the look-and-feel in Java wasn't good enough; it had to be the real native components."

Differences between components and lightweight/heavyweight

What is the difference between JPanel and JFrame and relationship to lightweight, heavyweight?
JPanel is container that allows to put several UI components together. JFrame is a window written using Swing.
All Swing components are so-called "lightwight" components because they are written in java. If for example you run Swing application and try to analyze it using UI analyzing tool (e.g. WinSpy in windows) you see only one element: the window (JFrame) itself. All other components are drawn from OS point of view.
Heavyweight API - AWT uses portable elements provided by OS. Since java must be portable among various operating system AWT is very limited. It implements only the minimal subset of screen elements supported by all platforms. However the AWT elements are mapped directly to the appropriate platform elements, so UI discovery tool will see them. These elements are named "heavy weight".
Heavyweight components like "AWT" components must be drawn using native GUI on a specific platform.
Where lightweight components like "Swing" components are drawn by java and don't rely on native GUI.
The basic difference between Swing and AWT is that Swing APIs are purely Java libraries i.e. they don't at all depend on the native libraries to draw graphical components. Because of this feature they provide a consistent look and feel on all platforms. AWT libraries require the support of native graphics libraries and some of their GUI components look different on different platforms. Moreover, Swing components are not inherently Thread safe, you explicitly have to write synchronized code to manipulate or redraw them whereas AWT components can be trusted in a multithreaded environment. AWT Components are called heavyweight components because of their dependency on native libraries. Swing components are called lightweight due to their independence of native libraries. Hence Swing operations are much faster because each and every operation is taken care by the Java runtime env and no delegation of events or commands to the native libraries is required.
A JFrame is a Swing container with an interface box, and can be a standalone application (it has the top box with abilities to minimize, maximize, and exit) whereas a JPanel is everything a JFrame is (a Swing container) minus the ability to be a standalone. For a JPanel to work, it must be inside something like a frame, like a JFrame.
The other answer has a good definition of lightweight and heavyweight components.
use:
I use JPanels within JFrames to easily run multithreaded programs that integrate together (I have a JPanel runnable class inside my class that extends JFrame, I run that as its own thread and the JPanel class can change the value of variables in the JFrame class). I use it to easily get different threads to interact with each other.
**You can have multiple JPanels within a JFrame.

SWT and AWT, what is the difference?

I understand that this question is rather general and kind of a holy war. Could you explain to me why SWT is successful when AWT is not, while these two frameworks use the same idea of native ui controls. What makes SWT different comparing to AWT? Just few words if possible.
Thank you.
AWT is the original cross-platform, native-peer based GUI widget set. It drew a lot of complaints for not being perfectly consistent across platforms. Sun built the Swing widget set to answer those concerns, building it with pure Java (no native peers), but people complained that it was slow and ugly.
IBM built SWT as a native-peer based competitor to Swing. It succeeded because it looks good, performs well and is pretty consistent across platforms. It cemented it's position by getting used by many popular applications, Eclipse and Vuze being the most obvious.
SWT provides a lot richer set of native heavyweight widgets than AWT - a proper comparison would be SWT vs AWT/Swing. Due to that, SWT looks more native than AWT/Swing. While this could be considered a success, it can also be a drawback, depending on what you need to implement. In case of SWT you are using only heavyweight native widgets with all possible OS limitations. With AWT/Swing you have the option to mix lightweight Swing (pure Java) widgets with native AWT widgets for better performance.

AWT XOR SWING together in one application?

I am looking into developing an Java GUI for a biological analysis tool. My question is, Can I use both AWT and SWING libraries under the same Model View Control design pattern? Or,are they two libraries controlled in a distinct way? I would like to know If I can bring them in under the same roof for purposes of reusing existing code written with both libraries. Thank you very much for your time.
Swing is built on top of AWT, you can mix Swing and AWT and it will technically work, but with some limitations:
heavyweight vs lightweight components: components in AWT are heavyweight - they correspond to a native OS window. This means that all AWT components appear above sibling Swing components. (E.g. put a awt List and a swing JList in the same container, and the List will always appear above the JList.)
Look and Feel: the AWT components look and feel native, since they are native components. The Swing components have a pluggable look and feel, which defines their appearance and behavior. If you choose the L&F to match the native platform, these components are only "immitations" of the native look and feel, and can behave differently from their AWT counterparts. (E.g. JButton vs awt Button.)
For these reasons, it might be wise to use just one UI library, presumably Swing.
EDIT: JDK 6 (Update 12) offers seamless integration of heavyweight and lightweight components, so mixing will work seamlessly. So the first point is no longer true - but having inconsistent look and feel between the two UI toolkits still stands.
Hi Yes you can use both. But there is no need, even more it is highly not recommended to mix these two.
For example you can have some serious problems as AWT doesn't have 'depth' concept. No different layers, etc. Other problems are for example, look of components of these frameworks differs.
EDIT:
I am loving this resource. Go ahead and read it there are all issues mentioned, with illustrative examples, which you have to be aware of when you are going to mix these two. http://java.sun.com/products/jfc/tsc/articles/mixing/
Oi,Boro.

If Swing has more features to design a form. Then what is the use of AWT in java?

In java, Swing has more features than the AWT components.
For example,
In AWT,
TextArea ta;
Button btn;
But its same in Swing as,
JTextArea ta;
JButton btn;
But swing component has good in look.
Then what's the need of AWT. Is there any useful feature?
Swing and AWT both provide user interface components, however Swing is built on top of AWT. It provides richer tools like icons, tool tips, etc. which are not available in AWT. Also, Swing is meant to be portable, while AWT (in theory) will match more of the system's look and feel.
Most IDEs that give you GUI building features (NetBeans, etc) will do so using Swing components. AWT is useful since it provides the foundation on which Swing is built.
More details here.
AWT is useful if you want truer O/S fidelity and can accept a lowest-common-denominator for widget support.
It's also immensely useful for building your own light-weight GUI on (suppose, for a game engine). For example, we needed a GUI system which would run on handhelds in JME PP 1.0 as well as desktops and thin clients, where we needed only a few basic components, but really minimal overhead, so I built a GUI toolkit which runs within an AWT Panel. It worked out really well.
AWT was the GUI package available in Java, then along came Swing, which was built on top of AWT in an attempt to make things look nicer and consistent across platforms. The problem is, they both look terrible. I am stuck with AWT in my project unfortunately, because it is a Windows Mobile project and our VM only supports AWT.
If you want a java GUI that actually looks nice (well nicer than Swing or AWT) then you want to check out SWT from IBM.
AWT was the first try on UI for Java, later Swing provided a better alternative because they use "light" components ( the component draw the UI them selves instead of relaying on native code ) and those light components were way lot flexible and maintainable then AWT.
AWT still has it's place as the underlying "technology" that interface Swing and the OS ( JComponent inherits java.awt.Container after all) , so, its place in the low level of UI. For instance, things like translucency, shapes etc. are placed in AWT package and they are used by Swing components.
New platform UI features will be added to Java in the following releases and many of them will be added to the java.awt. package.
AWT is still commonly used for applets! Most web browsers support AWT classes, so AWT applets can run without the Java plugin. Swing applets, however, require the Java plugin. Swing is not supported by older JVMs.
AWT is slighter faster because it uses native OS peer components, whereas Swing draws all its own components at the bit level. Swing components may not look and behave exactly like their native counterparts. AWT applications, however, will look like native apps and so will look different on different platforms. In contrast, Swing apps will look the same on all platforms (although this can be changed with code).

Categories

Resources