Saturday, 28 December 2013

Exhaustive Java Swing Tutorial for Beginners

swing tutorial for beginners
Here is a kickstarter Java Swing tutorial for Beginners that takes you into the ocean of swing slowly and help you reach the other end of the sea without pain and with a very little effort. Well, is it that easy? Yes, it is. Swing is a big topic, I agree but that doesn't mean it's harder, rather you find it more interesting as you move on.

This blog contains of over 100+ articles just on the Swing topic.

Well, this topic is going to be quite big, just take a look at right side (in your browser), You can see the scroll bar is a million miles far away from your bottom-right corner. But wait. Don't worry. If you want to learn Swing, you any ways have to load dozens of pages, I did it one page and great! you already completed loading this page and everything you need is here. This post is interesting, I promise, and by the time it ends, I'm sure you'll thank me!

In a poll, I have conducted on my facebook fan page, I saw 80% of people answering that Swing is their favorite topic in Java. Is it so for you? Discuss in the comments, why you love swing.

“By all means let's be open-minded, but not so open-minded that our brains drop out.” - Richard Dawkins

Now, when we talk of Swing, we need to talk about the basics of GUI which were already discussed in the AWT tutorial. If you aren't familiar with the AWT, I suggest you to learn it or atleast have an idea of it to understand the Swing effortlessly.

Now that you know, what are the advantages of AWT, when there was no thing called GUI, people faced a lot of problems working with commands. Later GUI was introduced and incorporated into many programming languages including our Java. But that was limited and not advanced. It contained only the core components and even violated the Java paradigm as discussed in the awt tutorial. But do you know? AWT is small, isn't it? Yes, it is when compared to that of the Swing. Swing is relatively a larger toolkit with dozens of components you need to explore.

Is Swing easy for an AWT master?

Yeah! it is easy. But you don't really need to be an AWT master. The only thing, the major difference you can learn between the classes in Swing and the AWT are that in Swing you can see an additional J prefix before any component name. For example, in AWT it is Button, in swing it is JButton, TextField is JTextField in Swing. Also swing incorporates AWT Event handling into it.

But isn't there a difference except the prefix? There is!! A big difference is that all Swing components except the top-level containers (JFrame, JDialog) etc are written in pure Java code. While those in the AWT aren't.

“I don't know, I don't care, and it doesn't make any difference!” - Albert Einstein

The problem with AWT components is that they are written in native code (C/C++). So, to create any component they must depend upon the operating system and so the OS look and feel is incorporated into those components. And as the Operating system changes, the visual appearance (a.k.a. look and feel) of the components changes. Is it a problem? Yes, why not?

The javax.swing package

All the swing is tightly packed into a single package called javax.swing. But why is it given a name javax? Well, javax stands for Java's Extended Technology. I call it the Grandson generation of Java because it is advanced. And Oh no!! Are we into an advanced topic? Yes, swing is an advanced topic but you don't need to get frightened of the word because advanced isn't a synonym for harder.

“There are no secrets to success. It is the result of preparation, hard work, and learning from failure.” - Colin Powell

A sub package the javax.swing.event contains event listeners and event classes associated with several swing components. To understand this, you must go through what is event handling. Swing itself uses AWT event handling as said previously for many of its components, so we can conform that Swing doesn't override the AWT.

Disadvantages of AWT

1. Limited set of components. No sliders, progress bars, spinners..
2. Written in native code, looks different on different operating systems.
3. Not highly customizable
4. OS dependent, for each component creation, the JVM has to make calls to the Operating system.
5. AWT components take up more resources, they are slow because they need interaction with the OS.

“Every advantage has its disadvantage” - Proverb

Note: Even the top-level containers in Swing (JFrame, JDialog etc) must depend on the OS because a window (a top-level container) cannot be created without the permission of OS.

Introduction to Swing

Every topic has an introduction so does the Swing. The introduction of swing is a little big, frankly, but not too big to get frightened, I promise. After including this here, I thought it would be better if I write it as another post. So here is the post that teaches Introduction to swing and contains almost everything you need to know.

Model-View-Controller Architecture

Swing supports MVC architecture (Model-View-Controller) based architecture for its components. Here is a kickstart guide on mvc architecture in swing

Swing Components

You need to have a basic understand of the swing components and containment hierarchy before you penetrate into the examples. After going through above post, just continue reading the rest of this post.

Examples on each Swing Component

JFrame

JFrame is a top-level container which is nothing more than a window with a title bar and an optional menu bar.

JPanel

JPanel is a container which is a sub class of JComponent that stores other components in it. This can be added to any top-level container like JFrame and JDialog to group components.


JLabel

JLabel is a light-weight component that describes other components. Users may not know what a component is for, so we use JLabels to tell that information.


JButton

JButton is a nothing more than a push-button which you might already have gone through many times. User can click on it to get anything done.


JToggleButton

A JToggleButton is another component much similar to a JButton. But the difference is a JToggleButton goes into pressed state when mouse is pressed on it. To get that back into normal state, we need to press again. This component is mostly used when there is an on/off situation. You'll get a better idea of what it is when you see it in practical.


JCheckBox

A JCheckBox lies on a single principle, checked/unchecked. This component is mostly used in when we are checking multiple items, for example in Job search site, you might have undergone this where you select multiple skills, a check box is mostly used.


JRadioButton

JRadioButton is also based on the same principle as that of the JCheckBox. But the difference is that a JRadioButton is typically used when the user has to choose only one among the many items in a group. For instance, you can use it for Male/Female, the user has to select either of it, but not both. In AWT, you don't have specified class for JRadioButton as here.

Note: JButton, JToggleButton, JCheckBox and JRadioButton are all sub-classes of AbstractButton.


JComboBox

JComboBox lets the user choose a single item from multiple items. It only displays single item to the user. You can see it in many sites, where they ask you to choose your country.


JList

JList is another light-weight component similar to a JComboBox but with two major differences. The first is that JList shows multiple items (rows) to the user and also it gives an option to let the user select multiple items.


JTextField

JTextField, as you know, is a component used to write a single line of text. A text field can be seen at this blog's side bar, the subscribe field, where you type your email address.


JPasswordField

JPasswordField is much similar to a JTextField but it allows user to type passwords rather than simple text. You know, your text is visible as it is when you type in your email id, but in the case of password, some round black filled circles are displayed which are used to mask your password, but what you type is the same. This class also gives the option to morph the text with the type of character you want, if you don't like that black spot.


JFormattedTextField

This is a different type of JTextField. This allows user to enter a particular type of data. For example, you can restrict the user to enter only date in it, or a number, or a decimal value ranging between 0.0 and 1.0 etc. Anything else that the user types, will not be accepted. You can better understand this, if you look at the example.


JTextArea

JTextArea is allows you to write multiple lines of text. There are a lot of methods in this that you need to explore.


JMenu

A JMenu holds menu items and even other components. You might have already seen this in Notepad, they are named File, Edit etc. When you click them, you see menu items.


JMenuItem

A JMenuItem as said previously, is an item included in a menu where the user can click on it or invoke it by a shortcut. You can see this in Notepad, New, Open, Save etc. are called menu items. You can invoke them either by click on them or selecting them and hitting enter or via a shortcut.


Menu Button [Custom Component]*

This is a custom component. A menu button can be defined as menu that looks like a button (not like a JButton), when you click on it, the menu is displayed. You can see this type of component in My computer toolbar where you will set the view as Thumbnails or icons etc.


JCheckBoxMenuItem

This is a JMenuItem with JCheckBox in it. You might have seen this in Notepad > Format > Wordwrap.


JRadioButtonMenuItem

This is a JMenuItem with a JRadioButton in it. You can better know it and its uses if you take a look at the example.


JProgressBar

A JProgressBar is used to display progress of a process. You might have seen it a thousand times, at least. Most commonly, you have seen it in software install wizards and download managers. They show how much process was done.


JSlider

JSlider is used to let the user slide among values. You have seen it in Youtube, while playing a video, you can seek the video where ever you want, the one that you click on to do this is an example of slider.


Volume Component [Custom]*

A volume component is a slider with a JMenu. As said previously, you can also add custom components to JMenu apart from JMenuItems. Here a JSlider is added.


JSpinner

A JSpinner is used to spin between values. You might have seen it in setting RGB values of a color in many software. It contains a text field like editor in which you can type a value or it contains two small up and down buttons which allows you to change the values in the editor.


JTable

Who doesn't know what is a table? With the help of this class you can create JTable with column headers, insert custom type of data, components and what not, everything you can insert in a table. But for some things we need to hack.


JTree

A JTree is like a tree that contains several branches, even some branches contain sub branches. More about it clearly, with a diagram is explained in this example, I'm sure you'll understand it.


JToolBar

A JToolBar is used to create tool bars. A tool bar contains multiple components in it. You might have seen a toolbar many times, one such place is your book marks bar in your browser, which is a toolbar.


JTabbedPane

JTabbedPane is used to create tabs. Tabs lessen the space in a container by displaying only one group of data at a time. The best example, is your browser tabs where you open a new tab every time you want to open a new page.


JDialog

JDialog is a top-level container that doesn't contain minimize and maximize buttons, but still it is resizable. A JDialog typically takes a JFrame as a parent.


JFileChooser

A JFileChooser is used to let the user browse for files. You have seen it in Notepad, when you click on Open or Save. That specific dialog is called as a file chooser.


JColorChooser

A JColorChooser is a dialog that comes with several components that help user to choose from a large variety of colors.


No comments:

Post a Comment