
To kick-start your learning and to become an expert in java programming, enroll to Edureka’s Java Certification program. 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. With all the components which comes with swing in Java, it becomes easier to build optimized GUI applications. In this article we have discussed swing in Java and hierarchy of Java swing classes. This is a simple example for creating a GUI using swing in Java. Panel.add(label) // Components Added using Flow Layoutįrame.getContentPane().add(BorderLayout.SOUTH, panel) įrame.getContentPane().add(BorderLayout.NORTH, tf) įrame.getContentPane().add(BorderLayout.CENTER, ta) JTextField tf = new JTextField(10) // accepts upto 10 characters JPanel panel = new JPanel() // the panel is not visible in output JMenuItem m22 = new JMenuItem("Save as") Example: Chat FrameįtDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) GridBagLayout places the components in a grid which allows the components to span more than one cell. Flow LayoutįlowLayout simply lays the components in a row one after the other, it is the default layout manager for every JPanel. It places components in upto five places which is top, bottom, left, right and center. The default layout manager for every JFrame is BorderLayout. To arrange the components inside a container we use the layout manager. It inherits the JComponent class and is used to show pop up menu of choices. It is used for placing text in a container. It inherits JComponent class, the object of JList class represents a list of text items.ĭefaultListModel l = new DefaultListModel() It inherits the JMenuItem class, and is a pull down menu component which is displayed from the menu bar. It inherits the JComponent class and provides space for an application which can attach any other component. It is used to add scroll bar, both horizontal and vertical. JTextField b = new JTextField("edureka") It inherits the JTextComponent class and it is used to allow editing of single line text. It inherits the AbstractButton class and is platform independent. Using the ActionListener it will result in some action when the button is pushed. Following are a few components with examples to understand how we can use them. Containers like JFrame and JDialog can only add a component to itself. Methods like setLayout override the default layout in each container. Basic swing components are the building blocks of any gui application.
#Flowlayout not defned windows
Containers are the windows like frame and dialog boxes.

Panel – It is used to organize components on to a windowįrame – A fully functioning window with icons and titlesĭialog – It is like a pop up window but not fully functional like the frame For building GUI applications at least one container class is necessary.įollowing are the three types of container classes: Container ClassĪny classwhich has other components in it is called as a container class.

This is helpful because we do not have to start from the scratch.

It becomes easier to build applications since we already have GUI components like button, checkbox etc. It is platform independent unlike AWT and has lightweight components. It is build on top of the AWT API and entirely written in java.

It is a part of the JFC( Java Foundation Classes). Swing in Java is a lightweight GUI toolkit which has a wide variety of widgets for building optimized window based applications. Following are the concepts discussed in this article: In this article, we will go through the concepts involved in the process of building applications using swing in Java. Putting together all these components makes a graphical user interface. It includes components like button, scroll bar, text field etc. It is used for creating window based applications. Swing in java is part of Java foundation class which is lightweight and platform independent.
