Java Foundation Class (JFC) : Features and Terminologies of GUI
Java Foundation Class (JFC) : Features and Terminologies of GUI
GUI stands for Graphical User Interface. GUI consists of what the user sees on the screen and code to process users action such as clicking of mouse or typing on the keyboard. interactive input and output All passes through the GUI. GUI is made up of one or more component as frame, buttons, checkboxes, such radio buttons, list boxes, dialogs boxes, Windows etc. Every GUI based application haves one main container window which holds other components. In Java this window can be Frame or Applet. Such a window have title bar. border, buttons for closing, maximizing, minimizing the window Application can have components which are used to perform some action. These components are not a container and thus can not hold other components, e.g. Label, List boxes. Combo boxes. Text Fields etc. In GUI based application, there is zero or more sub-container which resides on main container and used to hold the other components, eg. JApplet ,JPanel, JDialog etc. There are some schemes to lay out the components in the display area of the container. Such .schemes called as Layout Managers. Layout are Manager provides a way to arrange the components in the container, Apart from adding the components, one can draw the custom drawing on the display area. One can achieve Graphics Context to draw the Lines, Images, and Pictures etc. In GUI every component is drawn and painted when the application for that GUI starts. This painting of componentsis handled by separate thread called Event Dispatch Thread. Every GUI must respond to the user's action for that GUI must support event handling. In eventhandling mechanism, oneor more components generate the event and there are one or more Listeners are registered to handle these events. These listeners provide the methods to handle These events and perform some action.
Swing
Swing is a set of classes that provides more powerful and flexible components than are possible with the AWT.Swing components are not implemented by platform-specific code. Instead, they are written entirely in Java and, therefore, are platform-independent. The term lightweight is used to describe swing elements, whereas AWT components are dependent on the operating system that is why they are known as heavyweight components.
Some swing components are heavyweight component eg. JApplet, JDialog. J Frame these entire components have a native support and these components are directly derived from its AWT counterparts. . Swing components have pluggable look and feel means these components looks similar in any operating system. They are adaptable to all operating system. Swing API provides set of classes and interfaces to develop 100% pure Java Programs. All these classes starts with the letter J. Some classes provides additional methods to develop a attractive GUI. Swing you can provide Tool tip text to every component by using method setToolTipText(). You can add images and pictures in almost all swing components of swing by using setlcon() method In Swing all components are added on Glass pane, in swing we can not add components directly on main container. In swing components like JTextArea and JList does not have in-build scroll bars, to give scroll bars to such a components we must enclose these components in the JScrollPane. All Swing components follows Model View Architecture (MVC) design pattern in which manipulation of data is separate from rendering and drawing of the component
JComponent class
JComponent is superclass for all swing components. It is a subclass of Java.awt.Container class which in turns subclass of Java.awt.Component class. It is an abstract class and you can not create a object of this class by using new operator. Package Methods Constructor : javax.swing JComponent( ) - Default JComponent constructor.
Name Description void addXXXListener(XXXListcner ohj) This method is used lo register a Listeners for a specific type of event with the component.
void repaint() void repaint(long msec) void repaint(int x.int y, int h, int w) void repaint(long msec,int x,int y, int h, int w) void setBackground(Color c) void setBorder(Border b) void setDoubleBuffered( Boolean b)
void setPreferedSize(Dimension d)
void setSize(Dimension d) void setSize{int w.int h) void set ToolTipText() void setVisible(Boolean b) void update(Graphics context)
This method is used to repaint the current component. Second version repaints the components after specified no of millisecond. You can repaint the specific area of the component by using third version. This method is used to set the background color for the component. This method is used to set the Border for the Component. If we pass a true parameter, then this method allows the component to be painted in the offscreen buffer and paint it on the main screen when entire bytes are available. It is used to eliminate flickering effect of the screen. It is used to set any component enabled or disabled. It is used to set the font of the text appearing on the Component. This method is used to set the foreground color of the component. Foreground color is the color of text appearing on the component. This methods sets the ideal size for the component. It takes a parameter of type Dimension which can be created by passing the two integer parameters in the constructor of the Dirnesion class. You can set a size of the component by set size method. You can pass either Dimesion object as a parameter or two integer object which specifies width and height. This method is used to associate tool tip string with the component. You can set component visible or invisible by using this method. This method in turns calls paint method to paint the component.
2. container It is subclass of Component class. This represents a Container in which we can add different component. Container can have title, border, background color and size, java.awt Package: java.awt Constructor : Container()
Methods Name :
void add (Component comp) void add (Component comp. Object constraint)
Component [ ] getComponents( ) Layout Manager getLayout( ) void remove ( Component c) void setLayout( Layout Manager m) void paintComponent ( Graphics g )
JDialog : This class extends java.awt.Dialog class. Dialogs arc used lo accept some inputs from User. Default layout manager for JDialog class is BorderLayoui. Package : javax.swing Constructor : JDialog ( Frame parent) This constructor creales a new JDialog object which appears on specified parent Frame. JDialog (Frame parent, boolean modal): This constructor creates a new JDialog object which appears on the specified parent frame. If we pass second parameter as 'true' Ihen we can not work o the parent window when dialog is visible, such a Dialog boxes are called as Modal dialog boxes. If we pass 'false" then we can work on the parent window when dialog is visible such a dialog box is called as non-modal dialog box. JDialog (Frame parent. String title ) : This constructor creales a dialog box which have some title. JDialog ( Frame parcnl. Siring title. Boolean modal ) : This constructor creales a Dialog box which appears on specified parent by the user and modal or non-modal nature is specified. Methods Name void hide ( ) void show( ) Container getContentPane() void setLayout(LayoutManager) void setJMenuBar(JMenuBar) boolean isModal ( ) Descripjion This method is used to hide the Dialog box. 1 his method is used to show the Dialog box. This method returns , a JDialog Content Pane for the
'
I his method sets the LayoutManauer for the Dialog This method sets the J Menu Bar to the JDialog box This method checks whether the dialog is modal or non-modal
JFrame :
This class extends Java.aw t.Frame class. This object is a window with some title and Border. Frame can have menu bar.Every GUI based application of Swing have at least one JFrame object. Default layout for JFrame is BorderLay out. Package javax.swing
Constructor JFrame() This constructs a new frame that is initially invisible. JFrame(String title) This constructor a new frame that is initially invisible,with specified title. Methods
Container getContentPane() void setLayout(LayoutManager) void setJMenuBar(JMenuBar) This method returns , a Content Pane for the JFrame I his method sets the LayoutManauer for the JFrame This method sets the J Menu Bar to the JFrame
This class extends Jcomponent class.JPanel object doesnot have its own window.It is always added to the window.One panel can have more than one panel in it.This is required to add component at proper
position and divide the screen into numbers of region.Default layout manager for panel is FlowLayout . Package:javax.swing Constructor: JPanel() This is default constructor for creating Panel. JPanel(LayoutManager lm) This constructor creates panel with layout manager specified Methods
Container getContentPane() void setLayout(LayoutManager) void setJMenuBar(JMenuBar) This method returns , a Content Pane for the JFrame I his method sets the LayoutManauer for the JFrame This method sets the J Menu Bar to the JFrame
Paints icon image on JFrame Sets the layout for the frame
JButton Perpose ; This class is used to create a push buttons. Package : javax.swing Constructor JButton( ) Create a empty button having no title and icon. JButton(String str) Create a button having label. JButton(lcon i) Creates a button having icon image. JButton(String str, Icon i) Creates a button having siring label and icon image. Methods : This class is subclass of Abstract Button class. So it uses following important methods of abstract button class. Name void addActionlListenerfActionLislener obj) Siring geText() void set Text! (Siring s) void setHorizontalTextPosition(int pos) void selVcrticalTextPositionfint pos) void set lcon( Icon i) vois setMnemonic(char c) Description This method is used to register the push button to throw an event. This method returns the text associated with the button. This method is used to set new Label of the button Sets the horizontal text position relative to the graphics. Sets the vertical text position relative 10 the graphics. This method sets the Icon to the button Sets the mnemonic character to the button.
import java.awt.*; import java.awt.event.*; import javax.swing.*; /* <applet code="JButtonDemo" width=250 height=300> </applet> */ public class JButtonDemo extends JApplet implements ActionListener { JTextField jtf; public void init() { // Get content pane Container contentPane = getContentPane(); contentPane.setLayout(new FlowLayout()); // Add buttons to content pane
ImageIcon lotus = new ImageIcon("lotus.gif"); JButton jb = new JButton(Lotus); jb.setActionCommand("Lotus"); jb.addActionListener(this); contentPane.add(jb); ImageIcon rose = new ImageIcon("rose.gif"); jb = new JButton(Rose); jb.setActionCommand("Rose"); jb.addActionListener(this); contentPane.add(jb); // Add text field to content pane jtf = new JTextField(15); contentPane.add(jtf); } public void actionPerformed(ActionEvent ae) { jtf.setText(ae.getActionCommand()); } }
JLabel This class is used to create single line read only text which describe the other
component. Package : javax.swing Constructor JLabel() JLabel(String str) Jl.abel(lcon i) Jl.abel(String str,Icon Methods Name void set Horizontal Alignment(int a) String getText() void setText( String s) void set ion( Icon i) Description This method sets the alignment of the text. This method returns the text associated with the Label This method is used to set the text to the Label This method sets the Icon to the icon
Create a empty label having no text and icon. Create a label having some text. Creates a label having icon image. i) Creates a label having string text and icon image.
Icons
Icons are encapsulated by the ImageIcon class, which paints an icon from an image. constructors are ImageIcon(String filename) This constructor creates object with image which is specified by filename ImageIcon(URL url) This constructor creates object with image in the resource identified by url.
Method
int getIconHeight( ) Returns the height of the icon in pixels. int getIconWidth( ) Returns the width of the icon in pixels. void paintIcon(Component comp, Graphics g, int x, int y) Paints the icon at position x, y on the graphics context g. Additional information about the paint operation can be provided in comp.
import java.awt.*; import javax.swing.*; /* JAVA <applet code="JLabelDemo" width=250 height=150> </applet> */ public class JLabelDemo extends JApplet { public void init() { // Get content pane Container contentPane = getContentPane(); // Create an icon ImageIcon ii = new ImageIcon("Lotus.gif"); // Create a label JLabel jl = new JLabel("Lotus", ii, JLabel.CENTER); // Add label to the content pane contentPane.add(jl); } }
JCheckBoxse Package Constructor: This class is used to create check box with a text or icon. javax. swing JCheckBox() Create a empty checkbox having no title and icon. JCheckBox (String I) Create a checkbox having label. JCheckBox (Icon i) Creates a checkbox having icon image. JCheckBox (Siring I, Icon i) Creates a checkbox having string label and icon image. JCheckBox (String I. Icon I. boolean selected) Creates a checkbox having string label and icon image and default selected if true is passed. JCheckBox (String I, boolean selected) Creates a checkbox having string label and option for default selected policy.
import java.awt.*; import java.awt.event.*; import javax.swing.*; /* <applet code="JCheckBoxDemo" width=400 height=50> </applet> */ public class JCheckBoxDemo extends JApplet implements ItemListener { JTextField jtf; public void init() { JAVA // Get content pane Container contentPane = getContentPane(); contentPane.setLayout(new FlowLayout()); boxes to the content pane JCheckBox cb = new JCheckBox("Sandal"); cb.addItemListener(this); contentPane.add(cb); cb = new JCheckBox("Rose"); cb.addItemListener(this); contentPane.add(cb); cb = new JCheckBox("Lotus");
cb.addItemListener(this); contentPane.add(cb); cb = new JCheckBox("Mogara); cb.addItemListener(this); contentPane.add(cb); // Add text field to the content pane jtf = new JTextField(15); contentPane.add(jtf); } public void itemStateChanged(ItemEvent e) { JCheckBox cb = (JCheckBox)ie.getItem(); jtf.setText(cb.getText()); } }
JRadioButton
Purpose :
Package : Constructor:
This class is used to create radio button with a text or icon. Once a radio button is created they must be kept in one group,which is created by using the ButtonGroup class Radio buttons must be configured into a group. Only one of the buttons in that group can be selected at any time. javax.swing J RadioButton() Create a empty radio button having no title and icon.
import java.awt.*; import java.awt.event.*; import javax.swing.*; /* <applet code="JRadioButtonDemo" width=300 height=50> </applet> */ public class JRadioButtonDemo extends JApplet implements ActionListener { JTextField tf; public void init() { Container contentPane = getContentPane(); contentPane.setLayout(new FlowLayout()); JRadioButton b1 = new JRadioButton("C++"); b1.addActionListener(this); contentPane.add(b1); JRadioButton b2 = new JRadioButton("Java"); b2.addActionListener(this); contentPane.add(b2); JRadioButton b3 = new JRadioButton("SmallTalk"); b3.addActionListener(this); contentPane.add(b3); ButtonGroup bg = new ButtonGroup(); bg.add(b1); bg.add(b2); bg.add(b3); tf = new JTextField(5); contentPane.add(tf); } public void actionPerformed(ActionEvent ae) { tf.setText(ae.getActionCommand()); }