Nội dung text Full Notes.pdf
Unit 1 GUI Programming [12 Hrs.] Introduction to Swing: 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. Difference Between AWT and Swing AWT SWING • Platform Dependent • Platform Independent • Does not follow MVC • Follows MVC • Lesser Components • More powerful components • Does not support pluggable look and • Supports pluggable look and feel feel • Heavyweight • Lightweight Java Swing Class Hierarchy
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: 1. Panel – It is used to organize components on to a window 2. Frame – A fully functioning window with icons and titles 3. Dialog – It is like a pop up window but not fully functional like the frame Top level Containers o It inherits Component and Container of AWT. o It cannot be contained within other containers. o Heavyweight. o Example: JFrame, JDialog, JApplet Lightweight Containers o It inherits JComponent class. o It is a general purpose container. o It can be used to organize related components together. o Example: JPanel JFrame(a top level window in Swing) Whenever you create a graphical user interface with Java Swing functionality, you will need a container for your application. In the case of Swing, this container is called a JFrame. All GUI applications require a JFrame. In fact, some Applets even use a JFrame. Why? You can't build a house without a foundation. The same is true in Java: Without a container in which to put all other elements, you won't have a GUI application. In other words, the JFrame is required as the foundation or base container for all other graphical components. Java Swing applications can be run on any system that supports Java. These applications are lightweight. This means that don't take up much space or use many system resources. JFrame is a class in Java and has its own methods and constructors. Methods are functions that impact the JFrame, such as setting the size or visibility. Constructors are run when the instance is created: One constructor can create a blank JFrame, while another can create it with a default title. When a new JFrame is created, you actually create an instance of the JFrame class. You can create an empty one, or one with a title. If you pass a string into the constructor, a title is created as follows: JFrame f = new JFrame(); //Or overload the constructor and give it a title: JFrame f1 = new JFrame("BCA Sixth Sem");
JComboBox JComboBox is a component that combines a button or editable field and a drop-down list. The user can select a value from the drop-down list, which appears at the user's request. If you make the combo box editable, then the combo box includes an editable field into which the user can type a value. String arr[]={“BCA”,”BBA”,”MCA”,”MBA”}; JComboBox cmb=new JComboBox(arr); //or JComboBox cmb=new JComboBox(); cmb.addItem(“BCA”); cmb.addItem(“BBA”); cmb.addItem(“MCA”); cmb.addItem(“MBA”); JList JList is a component that displays a list of objects. It allows the user to select one or more items. String arr[]={“BCA”,”BBA”,”MCA”,”MBA”}; JList cmb=new JList(arr); JTextArea A JTextArea is a multiline text area that displays plain text. It is lightweight component for working with text. The component does not handle scrolling. For this task, we use JScrollPane component. JTextArea txt=new JTextArea(); JTable The JTable class is a part of Java Swing Package and is generally used to display or edit two- dimensional data that is having both rows and columns. It is similar to a spreadsheet. This arranges data in a tabular form. // Data to be displayed in the JTable String[][] data = { { "Kundan Kumar Jha", "4031", "CSE" }, { "Anand Jha", "6014", "IT" } }; // Column Names String[] columnNames = { "Name", "Roll Number", "Department" }; // Initializing the JTable JTable j = new JTable(data, columnNames);