| These are derived from the JRadioButton class, which is a concrete implementation of AbstractButton.
Few constructors of Radio Buttons are :
JRadioButton(Icon i)
JRadioButton(Icon i, boolean state)
JRadioButton(String s)
JRadioButton(String s, boolean state)
JRadioButton(String s, Icon i)
JRadioButton(String s, Icon i, boolean state)
6. Combo Boxes: We get this component from the JComboBox class, which extends JComponent. A combo box basically contains a list of elements which are put in a list and only one element can be displayed in the combo box at a time.
It also has few constructors which are given below:
JComboBox( )
JComboBox(Vector v)
7. Tabbed Panes: They are derived from the Jtabbed pane class. It appears more like a group of folders in a file. When any folder is selected, its contents are displayed and here also like the combo box only one element can be selected at a time.
We can create a tabbed pane in an applet by going through the following steps:
1. A JTabbedPane object is created.
2. Then Call addTab( ) to add a tab to the pane.
3. Then Repeat step 2 for each tab.
4. Finally add the tabbed pane to the content pane of the applet.
8. Scroll Panes: As we are quite familiar with the look of a scroll Pane. It basically presents itself like a rectangular area in which a component may be viewed. It can be either horizontal or vertical as per the requirement.
These are implemented in Swing by the JScrollPane class, which extends JComponent.
Scroll Pane is created in an applet by going through the following steps:
1. A JComponent object is created.
2. Then a JScrollPane object is created.
3. And finally add the scroll pane to the content pane of the applet.
Few Constructors of it as given below:
JScrollPane(Component comp)
JScrollPane(int vsb, int hsb)
JScrollPane(Component comp, int vsb, int hsb)
9. Tree: A tree is a component that presents a hierarchical view of data. Trees are implemented in Swing by the JTree class, which extends JComponent.
We can create the tree in an applet by the following steps:
1. JTree object is created first.
2. JScrollPane object is created
3. Add the tree to the scroll pane.
4. And finally add the scroll pane to the content pane of the applet.
Few constructors of tree are:
Tree(Hashtable ht)
JTree(Object obj[ ])
JTree(TreeNode tn)
Few of its methods are:
1.void addTreeExpansionListener(TreeExpansionListener tel)
2.void removeTreeExpansionListener(TreeExpansionListener tel)
where, tel is the listener object.
3.getPathForLocation : The getPathForLocation( ) method is used to translate a mouse click on a specific point of the tree to a tree path. Its syntax is :
TreePath getPathForLocation(int x, int y)
Here, x and y are the coordinates at which the mouse is clicked.
4. add () : Add are derived from the JText Component and this is a subclass of JComponent. We have been provided with the five functional text controls in swing which are:method is used to create a hierarchy of tree nodes, Its syntax is :
void add(MutableTreeNode child)
10. Swing Text Components:
All the text components available in swing
1. JtextField
2. JtextArea
3. JeditorPane
4. JpasswordField
5. JtextPane
If we have to show the hierarchy of these components we can have the simplest form which explains their level.
JComponent
JTextComponent
JtextField------------------ JpasswordField
JtextArea
JeditorPane----------------- JtextPane
Now we will have the brief understanding of these components one by one:
1. JtextField: one of the simplest components of the text Components is JTextField.It basically has a one line of text to be implemented in any application program, and it can also scroll the text if the size exceeds the size of the field. It displays its contents in the form of simple Text which is written.
How to create a Text Field: When we want to create a Text Field we can implement it with any of the following attributes:
1. The Document in which it will hold its text.
2. The initial content of the text field.
3. The number of columns to be displayed.
If we enter a text string to initialize the control, then it will be stored in the Document and will be displayed when the component appears on the screen.
The control remains empty if we do not give any text.
We have setText method and getText methods for these Text Controls. We can set or change the text with the help of setText method and can use getText method to retrieve the text from the control.
Content Alignment: We can decide the alignment of the Text Field control by:
JTextField.LEFT: For Left Alignment
JTextField.RIGHT: For Right Alignment
JTextField.CENTER: For Centre Alignment
Whenever the user presses the enter key the JtextField throws ActionEvents which tells that the user has finished with that field.
The JTextField class contains the following standard methods for working with ActionEvents:
1.public void addActionListener(ActionListener l)
2.public void removeActionListener(ActionListener l)
3.public ActionListener[] getActionListeners( )
4. public void postActionEvent( )
The JTextField class contains the following constructors:
1. public JTextField( ) : This is used to create a new text field which is without any content.
2. public JTextField(String text) : This constructor will create a new text Field with the given text passed as parameter in the same..
3.public JTextField(int columns) : This will create a new text field with the number of columns mentioned as parameter.
4. public JTextField(String text, int columns) : This will create a new text field with the number of columns and the text passed as parameters.
GIVE EXAMPLE
2. JpasswordField : This components is derived from the Jtextfield Component and so everything which is applicable to JtextField is also applicable to JpasswordField. The main thing which is to be noticed that the text which is entered in this is represented with a special symbol”*” as it represents the password for the authentication reasons and by doing this the numbers of characters can be counted but not exactly seen.
The UI Delegate is very much responsible not to display the exact characters which are entered by the user but display it as “*” for the security purposes.
The JpasswordFiled class contains the following constructors:
1.public JPasswordField( ) : This is used to create a new password text field with zero columns.
2.public JPasswordField(String text): This will create a new field containing the text (displayed using the echo character).
3. public JPasswordField(int columns) : This is used to create a new password field with the number of columns passed as parameter.
4. public JPasswordField(String text, int columns) : This method is used to create a new password field with the given number of columns having the given text passed as string in the method.
5. public JPasswordField(Document doc, String text, int columns) : This constructor creates a new password field that uses the specified document model and number of columns.
JpasswordField contains the following methods:
1.public void cut( )
2.public void copy( )
3.public string getText(int offs, int len) throws BadLocationException
4. public boolean echoCharIsSet( )
GIVE EXAMPLE
3. JtextArea Control: This control is implemented when we want to display two lines of text. We can also have the new line feature in this control and can also decide the number of rows and columns for this control.
JTextArea has Line separators in text files. newline (\n), carriage return (\r), or carriage return newline (\r\n), depending on the platform. Swing's text components remember which line separator was originally used, but always use a newline character to represent one in memory. So we should try to use \n when working with the content of a text component.
The JtextArea class contains the following constructors:
1.public JTextArea( ) : This constructor is used to create a default text area.
2. public JTextArea(int rows, int columns) : This will help us to create a text area with the number of rows and columns given in the constructor.
3. public JTextArea(String text) : It will create a text area which displays the specified text.
4. public JTextArea(String text, int rows, int columns) :This create a text area with the specified number of rows and columns displaying the given text passed as parameters in the constructor.
5. public JTextArea(Document doc) :This creates a text area that uses the specified Document.
Few Methods related to the JTextArea are as mentioned below:
1. public void append(String str) :This method appends the given text to the end of the document.
2. public void insert(String str, int pos) : This method will insert the given text at the position mentioned as integer position variable.
3. public void replaceRange(String str, int start, int end) : : This method will replace a section of the document beginning with the character at the start position and ending with the character at the end position with the given string. It will return null if the string is deleted before the operation.
4. public int getLineStartOffset(int line) throws BadLocationException : This method will return the character offset from the starting of the document that marks the beginning of the specified line number.
5. public int getLineEndOffset(int line) throws BadLocationException : This method will return the character offset from the beginning of the document that marks the end of the specified line number. This is actually the offset of the first character of the next line.
6. public int getLineOfOffset(int offset) throws BadLocationException : This method returns the line number that contains the given character offset .
GIVE EXAMPLE
4. JEditorPane Control: This control as its term defines appears to be like a window which can be placed on a document editor. It works very much like the JTextArea control and can be used to display text with better fonts and colours.
GIVE EXAMPLE
5.JTextPane Control: This control is a subclass of JEditorPane which helps us to create our own documents with the combinations of font, colour, and other attributes related to text. We can also add the images in this control with the text.
GIVE EXAMPLE
Common Text Component Features:
As we have mentioned earlier also that all these components are basically derived directly or indirectly from the JTextComponent, they all have few things very common in all. We will take a brief overview of few of its featutres which are found in all the five controls:
1. Editable and Enabled States: As the user we always have the opportunity to change or edit our text but at times we need to create text in Read only form and this is done by the following methods:
• public void setEditable(boolean editable);
This method makes the text editable if the value is passed as “True” and makes it only read only if value is passed as “False”.
2. Focus Accelerator Keys: JTextComponent provides an added feature of providing an accelerator key with each text component. When we press this key with the ALT key, that field gets the keyboard input focus. This facility serves of great help when we create a form with many input fields that could be filled in any order or sequence.
This can be done by setFocusAccelerator method. The syntax is:
public void setFocusAccelerator(char key);
We can also retrieve the accelerator key by getFocusAccelerator method. The syntax is:
public char getFocusAccelerator();
This method will return "\0" if there is none.
3. Selection and Data Transfer : As in any other language, we can have copy, paste and cut operations easily in swing too.The first step is to create a selection, which is done by clicking where the selection is to begin and then dragging it left or right to the other end of the selection. As the mouse is dragged, the text that will be part of the selection is highlighted.
Text Component Architecture
The basic architecture of any text Components has the following attributes which are :
1. Highlighter
2. Document
3. Caret
4. Editor Kit
5. Views
Now, let’s take a look on each one of them one by one.
1. Highlighter: Whenever we select any text, it is highlighted with the help of an object which is called as Highlighter. It is denoted by the grey shading over the selection.
2. Document: It stores the data and all its attributes which are displayed by the text component. All the methods which are used to maintain the document contents are defined by Document interface.
3. Caret: It is also known as cursor. It is used to point where the text will be inserted when entered at that point of time. It is represented by a small black bob. We can also change the representation of the caret or cursor by the setcaret method.
4. Editor Kit: This is used to handle a document with a particular content type. Swing has four types of editor kits. They are DefaultEditorKit, StyledEditorKit, HTMLEditorKit and RTFEditorKit.
5. Views: Text components are drawn with the help of objects which are derived from the abstract View class. The views that will be used to draw a particular component depend on the component type and on its actual content. For the simple text components, only one View type is required to manage the whole document and the component itself knows how to create its own specific kind of View.
11. Tables: It is the component which displays the the data in rows and columns. We can resize the size of the rown and columns with the help of cursors. The tables are implemented by the JTable class, which extends JComponent. .In almost all the cases we can implement renderers and editors that are generic enough and can be considered as separate building blocks that can be used to apply a particular effect to an existing table.
The constructor of Jtable is :
JTable(Object data[ ][ ], Object colHeads[ ])
Where, data is a two-dimensional array and colHeads is a one-dimensional array with the column headings.
We can create the table using the applet bu the following steps::
1. JTable object is created.
2. Then JScrollPane object is created.
3. Add the table to the scroll pane.
4. And finally we need to add the scroll pane to the content pane of the applet.
Table Rendering
The controls available in Swing controls do the task of painting themselves to look-and-feel specific UI classes. The JTable UI classes use the helper classes which are called as “renderers” to draw the contents of the table cells and the column headers. Renderers provide the major drawing functionality which is implemented by table's UI classes.
Rendering Basics
In Swings we need to follow the following steps to create the tables:
1. First, we need to select and fill the table with its background colour.
2. Whether the table will have gridlines or not. If they are there then it is drawn.
3. One by one, the cells of the table are made.
The Swing package includes renderers that can be used to draw cells that contain various types of data. The table can display data of the following types:
• Numbers. These are the objects which are derived from java.lang.Number, which includes all the Java numeric types like integer, Float, and Double.
• Booleans. A Boolean is represented by a check box, which is selected if the cell contains the value true and deselected if it contains false.
• Dates. This object is derived from a DateFormat object in a format suitable for the system's default locale.
• Icons. If a cell contains an object that is derived from ImageIcon, the icon will be displayed, cantered, in the cell.
Jtable provides us with two different ways to choose the renderer:
1. We can override the table's getCellRenderer method to choose a specific renderer for an individual cell.
2. Use the default mechanism, which chooses the renderer based on the cell's column or the class of the object in that column.
Selecting the Renderer for a Column
When we have to choose a renderer for a column, the table first looks at the TableColumn object which represents that column in the table's TableColumnModel. This object contains a renderer attribute which selects a particular renderer for that column. This is implemented with the help of following methods:
public void setCellRenderer(TableCellRenderer renderer);
public TableCellRenderer getCellRenderer();
Every renderer implements TableCellRenderer which works as an interface.
How to Edit a Table: We can edit a table in the following two steps:
1. We need to decide the column which needs to be edited.
2. We need to choose and install the editors that will control the editing of information in the e Model
We have two TableModel methods that work together to control cell editability:
1.public boolean isCellEditable (int row, int column);
2.public void setValueAt (Object value, int row, int column);
Give example for drawing table, editing table.
|