Ch no 2 Swings.pdf
Document Details
Uploaded by EnthralledSilicon
Bharati Vidyapeeth’s Institute of Management Studies & Research
Tags
Full Transcript
SWINGS Marks 10 COURSE OUTCOME Develop program using GUI Framework using (AWT & Swing) java Swing is a part of Java Foundation Classes (JFC) that is used to create window-based applications. It is built on the top of AWT (Abstract Windowing Toolkit) API and entirely written in jav...
SWINGS Marks 10 COURSE OUTCOME Develop program using GUI Framework using (AWT & Swing) java Swing is a part of Java Foundation Classes (JFC) that is used to create window-based applications. It is built on the top of AWT (Abstract Windowing Toolkit) API and entirely written in java. Unlike AWT, Java Swing provides platform- independent and lightweight components. The javax.swing package provides classes for java swing API such as JButton, JTextField, JRadioButton,JComboBox, JCheckbox, JTabbedPane, JScrollPane,Jtree,JTable.JProgressBar etc. DIFFERENCE BETWEEN AWT & SWING MVC ARCHITECTURE Swing API architecture follows loosely based MVC architecture in the following manner. Model represents component's data. View represents visual representation of the component's data. Controller takes the input from the user on the view and reflects the changes in Component's data. Swing component has Model as a seperate element, while the View and Controller part are clubbed in the User Interface elements. Because of which, Swing has a pluggable look-and-feel architecture. SWING FEATURES Light Weight − Swing components are independent of native Operating System's API as Swing API controls are rendered mostly using pure JAVA code instead of underlying operating system calls. Rich Controls − Swing provides a rich set of advanced controls like Tree, TabbedPane, slider, colorpicker, and table controls. Highly Customizable − Swing controls can be customized in a very easy way as visual apperance is independent of internal representation. Pluggable look-and-feel − SWING based GUI Application look and feel can be changed at run-time, based on available values. WHAT IS JFC The Java Foundation Classes (JFC) are a set of GUI components which simplify the development of desktop applications. Features of JFC Swing GUI components. Look and Feel support. Java 2D. AWT AND SWING HIERARCHY SWING CLASSES Japplet JApplet is a class that represents the Swing applet. It is a subclass of Applet class and must be extended by all the applets that use Swing. Japplet is rich in functionality that is not found in applet ex Japplet supports various panes i.e Content pane,glass pane. Whenever we require to add a component to it, the component is added to the content pane. Syntax Container getContentPane(); The add() of container can be used to add a component to a content pane void add(comp); IMAGEICON A ImageIcon control is an implementation of the Icon interface that paints Icons from Images. Constructor ImageIcon(String filename) ImageIcon(URL location) Methods int getIconHeight() int getIconWidth() Image getImage() void paintIcon(Component c, Graphics g, int x, int y) void setImage(Image image) JLABEL The class JLabel can display either text, an image, or both. Constructors & Methods PROGRAM import javax.swing.*; import java.awt.*; public class Buttons extends JApplet { Container c; JLabel l1; public void init() { c = getContentPane(); setLayout(new FlowLayout()); ImageIcon i = new ImageIcon("java.PNG"); l1=new Jlabel(“sunset”,I,Jlabel.CENTER); c.add(l1); } } PROGRAM import javax.swing.*; class LabelExample { public static void main(String args[]) { JFrame f= new JFrame("Label Example"); JLabel l1,l2; l1=new JLabel("First Label."); l1.setBounds(50,50, 100,30); l2=new JLabel("Second Label."); l2.setBounds(50,100, 100,30); f.add(l1); f.add(l2); f.setSize(300,300); f.setLayout(null); f.setVisible(true); } } PROGRAM import javax.swing.*; import java.awt.*; import java.applet.*; class LabelExample1 extends JFrame { JLabel l1,l2; public LabelExample1() { setLayout(new FlowLayout()); l1=new JLabel("First Label1."); l2=new JLabel("Second Label2."); add(l1); add(l2); } public static void main(String args[]) { LabelExample1 f=new LabelExample1(); f.setSize(400,600); f.setVisible(true); } } JCheckBox The JCheckBox class is used to create a checkbox. It is used to turn an option on (true) or off (false). Clicking on a CheckBox changes its state from "on" to "off" or from "off" to "on ".It inherits JToggleButton class. Constructors & Methods PROGRAM import javax.swing.*; import java.awt.*; public class check extends JApplet { Container c; public void init() { c = getContentPane(); setLayout(new FlowLayout()); JCheckBox c1=new JCheckBox("male"); c.add(c1); JCheckBox c2=new JCheckBox("Female"); c.add(c2); } } o/p JBUTTON The class JButton is an implementation of a push button. This component has a label and generates an event when pressed. It can also have an Image. Constructors & Methods JRADIOBUTTON The JRadioButton class is used to create a radio button. It is used to choose one option from multiple options. Constructors & Methods PROGRAM import javax.swing.*; import java.awt.*; public class radio extends JApplet { Container c; public void init() { c = getContentPane(); setLayout(new FlowLayout()); JRadioButton b1=new JRadioButton("male"); c.add(b1); JRadioButton b2=new JRadioButton("Female"); c.add(b2); ButtonGroup bg=new ButtonGroup(); bg.add(b1); bg.add(b2); } } Output JCOMBOBOX JComboBox is like a drop down box — you can click a drop-down arrow and select an option from a list. Constructor o Methods PROGRAM import javax.swing.*; import java.awt.*; public class Combo extends JApplet { Container c; JComboBox j; public void init() { c = getContentPane(); setLayout(new FlowLayout()); j= new JComboBox(); j.addItem("India"); j.addItem("srilanka"); j.addItem("pakistan"); c.add(j); } } Output JSCROLLPANE A JscrollPane is used to make scrollable view of a component. When screen size is limited, we use a scroll pane to display a large component or a component whose size can change dynamically. Constructors ScrollPaneConstants are ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED; ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER; ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS; ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED; ScrollPaneConstants. HORIZONTAL_SCROLLBAR_NEVER; ScrollPaneConstants. HORIZONTAL_SCROLLBAR_ALWAYS import javax.swing.*; import java.awt.*; import java.applet.*; public class scroll extends JApplet { Container c; JScrollPane j; public void init() { c = getContentPane(); c.setLayout(new BorderLayout()); JPanel p=new JPanel(); for(int i=0;i