Swing in java is part of Java foundation class which is lightweight and platform independent. It is used for creating window based applications. It includes components like button, scroll bar, text field etc. Putting together all these components makes a graphical user interface. In this article, we will go through the concepts involved in the process of building applications using swing in Java. Following are the concepts discussed in this article:
- What is Java Swing?
- Container Class
- Difference Between AWT And Swing
- Java Swing Class Hierarchy
- Layout Manager
- Example-Chat Frame
What is Swing In Java?
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.
It becomes easier to build applications since we already have GUI components like button, checkbox etc. This is helpful because we do not have to start from the scratch.
Java Full Course – 10 Hours | Java Full Course for Beginners | Java Tutorial for Beginners | Edureka
🔥𝐄𝐝𝐮𝐫𝐞𝐤𝐚 𝐉𝐚𝐯𝐚 𝐂𝐨𝐮𝐫𝐬𝐞 𝐓𝐫𝐚𝐢𝐧𝐢𝐧𝐠: https://www.edureka.co/java-j2ee-training-course (Use code “𝐘𝐎𝐔𝐓𝐔𝐁𝐄𝟐𝟎”)
This Edureka Java Full Course will help you understand the various fundamentals of Java programm…
Container Class
Any class which has other components in it is called as a container class. For building GUI applications at least one container class is necessary.
Following are the three types of container classes:
Panel – It is used to organize components on to a window
Frame – A fully functioning window with icons and titles
Dialog – It is like a pop up window but not fully functional like the frame
Difference Between AWT and Swing
AWT | SWING |
|
|
|
|
|
|
|
|
|
|
Java Swing Class Hierarchy
Explanation: All the components in swing like JButton, JComboBox, JList, JLabel are inherited from the JComponent class which can be added to the container classes. Containers are the windows like frame and dialog boxes. Basic swing components are the building blocks of any gui application. Methods like setLayout override the default layout in each container. Containers like JFrame and JDialog can only add a component to itself. Following are a few components with examples to understand how we can use them.
JButton Class
It is used to create a labelled button. Using the ActionListener it will result in some action when the button is pushed. It inherits the AbstractButton class and is platform independent.
Example:
import javax.swing.*; public class example{ public static void main(String args[]) { JFrame a = new JFrame("example"); JButton b = new JButton("click me"); b.setBounds(40,90,85,20); a.add(b); a.setSize(300,300); a.setLayout(null); a.setVisible(true); } }
Output:
JTextField Class
It inherits the JTextComponent class and it is used to allow editing of single line text.
Example:
import javax.swing.*; public class example{ public static void main(String args[]) { JFrame a = new JFrame("example"); JTextField b = new JTextField("edureka"); b.setBounds(50,100,200,30); a.add(b); a.setSize(300,300); a.setLayout(null); a.setVisible(true); } }
Output:
JScrollBar Class
It is used to add scroll bar, both horizontal and vertical.
Example:
import javax.swing.*; class example{ example(){ JFrame a = new JFrame("example"); JScrollBar b = new JScrollBar(); b.setBounds(90,90,40,90); a.add(b); a.setSize(300,300); a.setLayout(null); a.setVisible(true); } public static void main(String args[]){ new example(); } }
Output:
JPanel Class
It inherits the JComponent class and provides space for an application which can attach any other component.
import java.awt.*; import javax.swing.*; public class Example{ Example(){ JFrame a = new JFrame("example"); JPanel p = new JPanel(); p.setBounds(40,70,200,200); JButton b = new JButton("click me"); b.setBounds(60,50,80,40); p.add(b); a.add(p); a.setSize(400,400); a.setLayout(null); a.setVisible(true); } public static void main(String args[]) { new Example(); } }
Output:
JMenu Class
It inherits the JMenuItem class, and is a pull down menu component which is displayed from the menu bar.
import javax.swing.*; class Example{ JMenu menu; JMenuItem a1,a2; Example() { JFrame a = new JFrame("Example"); menu = new JMenu("options"); JMenuBar m1 = new JMenuBar(); a1 = new JMenuItem("example"); a2 = new JMenuItem("example1"); menu.add(a1); menu.add(a2); m1.add(menu); a.setJMenuBar(m1); a.setSize(400,400); a.setLayout(null); a.setVisible(true); } public static void main(String args[]) { new Example(); } }
Output:
JList Class
It inherits JComponent class, the object of JList class represents a list of text items.
import javax.swing.*; public class Example { Example(){ JFrame a = new JFrame("example"); DefaultListModel<String> l = new DefaultListModel< >(); l.addElement("first item"); l.addElement("second item"); JList<String> b = new JList< >(l); b.setBounds(100,100,75,75); a.add(b); a.setSize(400,400); a.setVisible(true); a.setLayout(null); } public static void main(String args[]) { new Example(); } }
Output:
JLabel Class
It is used for placing text in a container. It also inherits JComponent class.
import javax.swing.*; public class Example{ public static void main(String args[]) { JFrame a = new JFrame("example"); JLabel b1; b1 = new JLabel("edureka"); b1.setBounds(40,40,90,20); a.add(b1); a.setSize(400,400); a.setLayout(null); a.setVisible(true); } }
Output:
JComboBox Class
It inherits the JComponent class and is used to show pop up menu of choices.
import javax.swing.*; public class Example{ JFrame a; Example(){ a = new JFrame("example"); string courses[] = { "core java","advance java", "java servlet"}; JComboBox c = new JComboBox(courses); c.setBounds(40,40,90,20); a.add(c); a.setSize(400,400); a.setLayout(null); a.setVisible(true); } public static void main(String args[]) { new Example(); } }
Output:
Layout Manager
To arrange the components inside a container we use the layout manager. Following are several layout managers:
Border layout
Flow layout
GridBag layout
Border Layout
The default layout manager for every JFrame is BorderLayout. It places components in upto five places which is top, bottom, left, right and center.
Flow Layout
FlowLayout simply lays the components in a row one after the other, it is the default layout manager for every JPanel.
GridBag Layout
GridBagLayout places the components in a grid which allows the components to span more than one cell.
Example: Chat Frame
import javax.swing.*; import java.awt.*; class Example { public static void main(String args[]) { JFrame frame = new JFrame("Chat Frame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 400); JMenuBar ob = new JMenuBar(); JMenu ob1 = new JMenu("FILE"); JMenu ob2 = new JMenu("Help"); ob.add(ob1); ob.add(ob2); JMenuItem m11 = new JMenuItem("Open"); JMenuItem m22 = new JMenuItem("Save as"); ob1.add(m11); ob1.add(m22); JPanel panel = new JPanel(); // the panel is not visible in output JLabel label = new JLabel("Enter Text"); JTextField tf = new JTextField(10); // accepts upto 10 characters JButton send = new JButton("Send"); JButton reset = new JButton("Reset"); panel.add(label); // Components Added using Flow Layout panel.add(label); // Components Added using Flow Layout panel.add(tf); panel.add(send); panel.add(reset); JTextArea ta = new JTextArea(); frame.getContentPane().add(BorderLayout.SOUTH, panel); frame.getContentPane().add(BorderLayout.NORTH, tf); frame.getContentPane().add(BorderLayout.CENTER, ta); frame.setVisible(true); } }
This is a simple example for creating a GUI using swing in Java.
If you’re just beginning, then watch at this Java Tutorial to Understand the Fundamental Java Concepts.
In this article we have discussed swing in Java and hierarchy of Java swing classes. With all the components which comes with swing in Java, it becomes easier to build optimized GUI applications. Java programming language is a structured programming language and with the increasing demand it becomes extremely important to master all the concepts in Java programming. To kick-start your learning and to become an expert in java programming, enroll to Edureka’s Java Certification program.
Got a question for us? please mention this in the comments section of this ‘Swing In Java’ article and we will get back to you as soon as possible.