Document Details

NobleLead

Uploaded by NobleLead

Tags

Java Swing GUI development Java programming

Full Transcript

**Java Swing Tutorial** **Java Swing tutorial** 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 light...

**Java Swing Tutorial** **Java Swing tutorial** 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, JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc. ### Difference between AWT and Swing There are many differences between java awt and swing that are given below. **No.** **Java AWT** **Java Swing** --------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- 1) AWT components are **platform-dependent**. Java swing components are **platform-independent**. 2) AWT components are **heavyweight**. Swing components are **lightweight**. 3) AWT **doesn\'t support pluggable look and feel**. Swing **supports pluggable look and feel**. 4) AWT provides **less components** than Swing. Swing provides **more powerful components** such as tables, lists, scrollpanes, colorchooser, tabbedpane etc. 5) AWT **doesn\'t follows MVC**(Model View Controller) where model represents data, view represents presentation and controller acts as an interface between model and view. Swing **follows MVC**. ### What is JFC The Java Foundation Classes (JFC) are a set of GUI components which simplify the development of desktop applications ### Hierarchy of Java Swing classes The hierarchy of java swing API is given below.hierarchy of javax swing ### Commonly used Methods of Component class The methods of Component class are widely used in java swing that are given below. **Method** **Description** ------------------------------------------- --------------------------------------------------------------- public void add(Component c) add a component on another component. public void setSize(int width,int height) sets size of the component. public void setLayout(LayoutManager m) sets the layout manager for the component. public void setVisible(boolean b) sets the visibility of the component. It is by default false. Java Swing Examples ------------------- There are two ways to create a frame: - By creating the object of Frame class (association) - By extending Frame class (inheritance) We can write the code of swing inside the main(), constructor or any other method. File: FirstSwingExample.java JButton class declaration ------------------------- Let\'s see the declaration for javax.swing.JButton class. 1. public class JButton extends AbstractButton implements Accessible   ### Commonly used Constructors: **Constructor** **Description** ------------------- ----------------------------------------------------- JButton() It creates a button with no text and icon. JButton(String s) It creates a button with the specified text. JButton(Icon i) It creates a button with the specified icon object. ### Commonly used Methods of AbstractButton class: **Methods** **Description** ------------------------------------------ ------------------------------------------------------- void setText(String s) It is used to set specified text on button String getText() It is used to return the text of the button. void setEnabled(boolean b) It is used to enable or disable the button. void setIcon(Icon b) It is used to set the specified Icon on the button. Icon getIcon() It is used to get the Icon of the button. void setMnemonic(int a) It is used to set the mnemonic on the button. void addActionListener(ActionListener a) It is used to add the action listener to this object. Example of displaying image on the button: ------------------------------------------ 1. }     Output: ![JAVA Jbutton 3](media/image3.png) JLabel class declaration ------------------------ Let\'s see the declaration for javax.swing.JLabel class. 1. public class JLabel extends JComponent implements SwingConstants, Accessible   ### Commonly used Constructors: **Constructor** **Description** --------------------------------------------------- ------------------------------------------------------------------------------------- JLabel() Creates a JLabel instance with no image and with an empty string for the title. JLabel(String s) Creates a JLabel instance with the specified text. JLabel(Icon i) Creates a JLabel instance with the specified image. JLabel(String s, Icon i, int horizontalAlignment) Creates a JLabel instance with the specified text, image, and horizontal alignment. ### Commonly used Methods: **Methods** **Description** -------------------------------------------- --------------------------------------------------------------------- String getText() t returns the text string that a label displays. void setText(String text) It defines the single line of text this component will display. void setHorizontalAlignment(int alignment) It sets the alignment of the label\'s contents along the X axis. Icon getIcon() It returns the graphic image that the label displays. int getHorizontalAlignment() It returns the alignment of the label\'s contents along the X axis. Java JTextField =============== The object of a JTextField class is a text component that allows the editing of a single line text. It inherits JTextComponent class. JTextField class declaration ---------------------------- Let\'s see the declaration for javax.swing.JTextField class. 1. public class JTextField extends JTextComponent implements SwingConstants   ### Commonly used Constructors: **Constructor** **Description** -------------------------------------- -------------------------------------------------------------------------- JTextField() Creates a new TextField JTextField(String text) Creates a new TextField initialized with the specified text. JTextField(String text, int columns) Creates a new TextField initialized with the specified text and columns. JTextField(int columns) Creates a new empty TextField with the specified number of columns. ### Commonly used Methods: **Methods** **Description** --------------------------------------------- --------------------------------------------------------------------------------------------------------------------- void addActionListener(ActionListener l) It is used to add the specified action listener to receive action events from this textfield. Action getAction() It returns the currently set Action for this ActionEvent source, or null if no Action is set. void setFont(Font f) It is used to set the current font. void removeActionListener(ActionListener l) It is used to remove the specified action listener so that it no longer receives action events from this textfield. Java JTextField Example ----------------------- Output: JAVA Jtextfield 1 JTextArea class declaration --------------------------- Let\'s see the declaration for javax.swing.JTextArea class. 1. public class JTextArea extends JTextComponent   ### Commonly used Constructors: **Constructor** **Description** ------------------------------------------ ---------------------------------------------------------------------------------------------------- JTextArea() Creates a text area that displays no text initially. JTextArea(String s) Creates a text area that displays specified text initially. JTextArea(int row, int column) Creates a text area with the specified number of rows and columns that displays no text initially. JTextArea(String s, int row, int column) Creates a text area with the specified number of rows and columns that displays specified text. ### Commonly used Methods: **Methods** **Description** ------------------------------------- -------------------------------------------------------------------- void setRows(int rows) It is used to set specified number of rows. void setColumns(int cols) It is used to set specified number of columns. void setFont(Font f) It is used to set the specified font. void insert(String s, int position) It is used to insert the specified text on the specified position. void append(String s) It is used to append the given text to the end of the document. Java JPasswordField =================== The object of a JPasswordField class is a text component specialized for password entry. It allows the editing of a single line of text. It inherits JTextField class. JPasswordField class declaration -------------------------------- Let\'s see the declaration for javax.swing.JPasswordField class. 1. public class JPasswordField extends JTextField   ### Commonly used Constructors: **Constructor** **Description** ------------------------------------------ ---------------------------------------------------------------------------------------------------------- JPasswordField() Constructs a new JPasswordField, with a default document, null starting text string, and 0 column width. JPasswordField(int columns) Constructs a new empty JPasswordField with the specified number of columns. JPasswordField(String text) Constructs a new JPasswordField initialized with the specified text. JPasswordField(String text, int columns) Construct a new JPasswordField initialized with the specified text and columns. Java JPasswordField Example --------------------------- Output: ![Java Jpasswardfield 1](media/image5.png) Java 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. JCheckBox class declaration --------------------------- Let\'s see the declaration for javax.swing.JCheckBox class. 1. public class JCheckBox extends JToggleButton implements Accessible   ### Commonly used Constructors: **Constructor** **Description** ------------------------------------------ -------------------------------------------------------------------------------------- JJCheckBox() Creates an initially unselected check box button with no text, no icon. JChechBox(String s) Creates an initially unselected check box with text. JCheckBox(String text, boolean selected) Creates a check box with text and specifies whether or not it is initially selected. JCheckBox(Action a) Creates a check box where properties are taken from the Action supplied. ### Commonly used Methods: **Methods** **Description** ------------------------------------------ ------------------------------------------------------------------------- AccessibleContext getAccessibleContext() It is used to get the AccessibleContext associated with this JCheckBox. protected String paramString() It returns a string representation of this JCheckBox. Java JCheckBox Example ---------------------- Output: JAVA Jcheckbox 1 Java JRadioButton ================= The JRadioButton class is used to create a radio button. It is used to choose one option from multiple options. It is widely used in exam systems or quiz. It should be added in ButtonGroup to select one radio button only. JRadioButton class declaration ------------------------------ Let\'s see the declaration for javax.swing.JRadioButton class. 1. public class JRadioButton extends JToggleButton implements Accessible   ### Commonly used Constructors: **Constructor** **Description** ------------------------------------------ --------------------------------------------------------------------- JRadioButton() Creates an unselected radio button with no text. JRadioButton(String s) Creates an unselected radio button with specified text. JRadioButton(String s, boolean selected) Creates a radio button with the specified text and selected status. ### Commonly used Methods: **Methods** **Description** ------------------------------------------ ------------------------------------------------------- void setText(String s) It is used to set specified text on button. String getText() It is used to return the text of the button. void setEnabled(boolean b) It is used to enable or disable the button. void setIcon(Icon b) It is used to set the specified Icon on the button. Icon getIcon() It is used to get the Icon of the button. void setMnemonic(int a) It is used to set the mnemonic on the button. void addActionListener(ActionListener a) It is used to add the action listener to this object. Java JRadioButton Example ------------------------- Output: ![JAVA Jradiobutton 1](media/image7.png) **Java JComboBox** The object of Choice class is used to show popup menu of choices. Choice selected by user is shown on the top of a menu. It inherits JComponent class. **JComboBox class declaration** Let\'s see the declaration for javax.swing.JComboBox class. 1. public class JComboBox extends JComponent implements ItemSelectable, ListDataListener, ActionListener, Accessible   **Commonly used Constructors:** **Constructor** **Description** ------------------------------ ------------------------------------------------------------------------- JComboBox() Creates a JComboBox with a default data model. JComboBox(Object\[\] items) Creates a JComboBox that contains the elements in the specified array. JComboBox(Vector\

Use Quizgecko on...
Browser
Browser