Skip to main content

List and Textfield of java.awt class

SOURCE & COPIED FROM:


AWT List Class

Introduction

The List represents a list of text items. The list can be configured to that user can choose either one item or multiple items.

Class declaration

Following is the declaration for java.awt.List class:
public class List
   extends Component
      implements ItemSelectable, Accessible

Class constructors

S.N.Constructor & Description
1List() 
Creates a new scrolling list.
2List(int rows) 
Creates a new scrolling list initialized with the specified number of visible lines.
3List(int rows, boolean multipleMode) 
Creates a new scrolling list initialized to display the specified number of rows.

Class methods

<T extends EventListener> T[] getListeners(Class<T> listenerType) 
Returns an array of all the objects currently registered as FooListeners upon this List.
S.N.Method & Description
1void add(String item) 
Adds the specified item to the end of scrolling list.
2void add(String item, int index) 
Adds the specified item to the the scrolling list at the position indicated by the index.
3void addActionListener(ActionListener l) 
Adds the specified action listener to receive action events from this list.
4void addItem(String item) 
Deprecated. replaced by add(String).
5void addItem(String item, int index) 
Deprecated. replaced by add(String, int).
6void addItemListener(ItemListener l) 
Adds the specified item listener to receive item events from this list.
7void addNotify() 
Creates the peer for the list.
8boolean allowsMultipleSelections() 
Deprecated. As of JDK version 1.1, replaced by isMultipleMode().
9void clear() 
Deprecated. As of JDK version 1.1, replaced by removeAll().
10int countItems() 
Deprecated. As of JDK version 1.1, replaced by getItemCount().
11void delItem(int position)
Deprecated. replaced by remove(String) and remove(int).
12void delItems(int start, int end) 
Deprecated. As of JDK version 1.1, Not for public use in the future. This method is expected to be retained only as a package private method.
13void deselect(int index) 
Deselects the item at the specified index.
14AccessibleContext getAccessibleContext() 
Gets the AccessibleContext associated with this List.
15ActionListener[] getActionListeners() 
Returns an array of all the action listeners registered on this list.
16String getItem(int index) 
Gets the item associated with the specified index.
17int getItemCount() 
Gets the number of items in the list.
18ItemListener[] getItemListeners() 
Returns an array of all the item listeners registered on this list.
19String[] getItems() 
Gets the items in the list.
20Dimension getMinimumSize() 
Determines the minimum size of this scrolling list.
21Dimension getMinimumSize(int rows) 
Gets the minumum dimensions for a list with the specified number of rows.
22Dimension getPreferredSize() 
Gets the preferred size of this scrolling list.
23Dimension getPreferredSize(int rows) 
Gets the preferred dimensions for a list with the specified number of rows.
24int getRows() 
Gets the number of visible lines in this list.
25int getSelectedIndex() 
Gets the index of the selected item on the list,
26int[] getSelectedIndexes() 
Gets the selected indexes on the list.
27String getSelectedItem() 
Gets the selected item on this scrolling list.
28String[] getSelectedItems() 
Gets the selected items on this scrolling list.
29Object[] getSelectedObjects() 
Gets the selected items on this scrolling list in an array of Objects.
30int getVisibleIndex() 
Gets the index of the item that was last made visible by the method makeVisible.
31boolean isIndexSelected(int index) 
Determines if the specified item in this scrolling list is selected.
32boolean isMultipleMode() 
Determines whether this list allows multiple selections.
33boolean isSelected(int index) 
Deprecated. As of JDK version 1.1, replaced by isIndexSelected(int).
34void makeVisible(int index) 
Makes the item at the specified index visible.
35Dimension minimumSize() 
Deprecated. As of JDK version 1.1, replaced by getMinimumSize().
36Dimension minimumSize(int rows) 
Deprecated. As of JDK version 1.1, replaced by getMinimumSize(int).
37protected String paramString() 
Returns the parameter string representing the state of this scrolling list.
38Dimension preferredSize() 
Deprecated. As of JDK version 1.1, replaced by getPreferredSize().
39Dimension preferredSize(int rows) 
Deprecated. As of JDK version 1.1, replaced by getPreferredSize(int).
40protected void processActionEvent(ActionEvent e) 
Processes action events occurring on this component by dispatching them to any registered ActionListener objects.
41protected void processEvent(AWTEvent e) 
Processes events on this scrolling list.
42protected void processItemEvent(ItemEvent e) 
Processes item events occurring on this list by dispatching them to any registered ItemListener objects.
43void remove(int position) 
Removes the item at the specified position from this scrolling list.
44void remove(String item) 
Removes the first occurrence of an item from the list.
45void removeActionListener(ActionListener l) 
Removes the specified action listener so that it no longer receives action events from this list.
46void removeAll() 
Removes all items from this list.
47void removeItemListener(ItemListener l) 
Removes the specified item listener so that it no longer receives item events from this list.
48void removeNotify() 
Removes the peer for this list.
49void replaceItem(String newValue, int index) 
Replaces the item at the specified index in the scrolling list with the new string.
50void select(int index) 
Selects the item at the specified index in the scrolling list.
51void setMultipleMode(boolean b) 
Sets the flag that determines whether this list allows multiple selections.
52void setMultipleSelections(boolean b) 
Deprecated. As of JDK version 1.1, replaced by setMultipleMode(boolean).

Methods inherited

This class inherits methods from the following classes:
  • java.awt.Component
  • java.lang.Object

List Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >
AwtControlDemo.java
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;

public class AwtControlDemo {

   private Frame mainFrame;
   private Label headerLabel;
   private Label statusLabel;
   private Panel controlPanel;

   public AwtControlDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      AwtControlDemo  awtControlDemo = new AwtControlDemo();
      awtControlDemo.showListDemo();
   }

   private void prepareGUI(){
      mainFrame = new Frame("Java AWT Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new Label();
      headerLabel.setAlignment(Label.CENTER);
      statusLabel = new Label();        
      statusLabel.setAlignment(Label.CENTER);
      statusLabel.setSize(350,100);

      controlPanel = new Panel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }

   private void showListDemo(){                                       

      headerLabel.setText("Control in action: List"); 
      final List fruitList = new List(4,false);

      fruitList.add("Apple");
      fruitList.add("Grapes");
      fruitList.add("Mango");
      fruitList.add("Peer");

      final List vegetableList = new List(4,true);
   
      vegetableList.add("Lady Finger");
      vegetableList.add("Onion");
      vegetableList.add("Potato");
      vegetableList.add("Tomato");

      Button showButton = new Button("Show");

      showButton.addActionListener(new ActionListener() {

         public void actionPerformed(ActionEvent e) {     
            String data = "Fruits Selected: " 
               + fruitList.getItem(fruitList.getSelectedIndex());
            data += ", Vegetables selected: ";
            for(String vegetable:vegetableList.getSelectedItems()){
               data += vegetable + " ";
            }
            statusLabel.setText(data);
         }
      }); 

      controlPanel.add(fruitList);
      controlPanel.add(vegetableList);
      controlPanel.add(showButton);

      mainFrame.setVisible(true);  
   }
}
Compile the program using command prompt. Go to D:/ > AWT and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AwtControlDemo.java
If no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AwtControlDemo
Verify the following output
AWT List

AWT TextField Class

Introduction

The textField component allows the user to edit single line of text.When the user types a key in the text field the event is sent to the TextField. The key event may be key pressed, Key released or key typed. The key event is passed to the registered KeyListener. It is also possible to for an ActionEvent if the ActionEvent is enabled on the textfield then ActionEvent may be fired by pressing the return key.

Class declaration

Following is the declaration for java.awt.TextField class:
public class TextField
   extends TextComponent

Class constructors

S.N.Constructor & Description
1TextField() 
Constructs a new text field.
2TextField(int columns) 
Constructs a new empty text field with the specified number of columns.
3TextField(String text) 
Constructs a new text field initialized with the specified text.
4TextField(String text, int columns) 
Constructs a new text field initialized with the specified text to be displayed, and wide enough to hold the specified number of columns.

Class methods

S.N.Method & Description
1void addActionListener(ActionListener l) 
Adds the specified action listener to receive action events from this text field.
2void addNotify() 
Creates the TextField's peer.
3boolean echoCharIsSet() 
Indicates whether or not this text field has a character set for echoing.
4AccessibleContext getAccessibleContext() 
Gets the AccessibleContext associated with this TextField.
5ActionListener[] getActionListeners() 
Returns an array of all the action listeners registered on this textfield.
6int getColumns() 
Gets the number of columns in this text field.
7char getEchoChar() 
Gets the character that is to be used for echoing.
8<T extends EventListener> T[] getListeners(Class<T> listenerType) 
Returns an array of all the objects currently registered as FooListeners upon this TextField.
9Dimension getMinimumSize() 
Gets the minumum dimensions for this text field.
10Dimension getMinimumSize(int columns) Gets the minumum dimensions for a text field with the specified number of columns.
11Dimension getPreferredSize() 
Gets the preferred size of this text field.
12Dimension getPreferredSize(int columns) 
Gets the preferred size of this text field with the specified number of columns.
13Dimension minimumSize() 
Deprecated. As of JDK version 1.1, replaced by getMinimumSize().
14Dimension minimumSize(int columns) 
Deprecated. As of JDK version 1.1, replaced by getMinimumSize(int).
15protected String paramString() 
Returns a string representing the state of this TextField.
16Dimension preferredSize() 
Deprecated. As of JDK version 1.1, replaced by getPreferredSize().
17Dimension preferredSize(int columns) 
Deprecated. As of JDK version 1.1, replaced by getPreferredSize(int).
18protected void processActionEvent(ActionEvent e) 
Processes action events occurring on this text field by dispatching them to any registered ActionListener objects.
19protected void processEvent(AWTEvent e) 
Processes events on this text field.
20void removeActionListener(ActionListener l) 
Removes the specified action listener so that it no longer receives action events from this text field.
21void setColumns(int columns) 
Sets the number of columns in this text field.
22void setEchoChar(char c) 
Sets the echo character for this text field.
23void setEchoCharacter(char c) 
Deprecated. As of JDK version 1.1, replaced by setEchoChar(char).
24void setText(String t) 
Sets the text that is presented by this text component to be the specified text.

Methods inherited

This class inherits methods from the following classes:
  • java.awt.TextComponent
  • java.awt.Component
  • java.lang.Object

TextField Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >
AwtControlDemo.java
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;

public class AwtControlDemo {

   private Frame mainFrame;
   private Label headerLabel;
   private Label statusLabel;
   private Panel controlPanel;

   public AwtControlDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      AwtControlDemo  awtControlDemo = new AwtControlDemo();
      awtControlDemo.showTextFieldDemo();
   }

   private void prepareGUI(){
      mainFrame = new Frame("Java AWT Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new Label();
      headerLabel.setAlignment(Label.CENTER);
      statusLabel = new Label();        
      statusLabel.setAlignment(Label.CENTER);
      statusLabel.setSize(350,100);

      controlPanel = new Panel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }

   private void showTextFieldDemo(){
      headerLabel.setText("Control in action: TextField"); 

      Label  namelabel= new Label("User ID: ", Label.RIGHT);
      Label  passwordLabel = new Label("Password: ", Label.CENTER);
      final TextField userText = new TextField(6);
      final TextField passwordText = new TextField(6);
      passwordText.setEchoChar('*');

      Button loginButton = new Button("Login");
   
      loginButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {     
            String data = "Username: " + userText.getText();
            data += ", Password: " + passwordText.getText();
            statusLabel.setText(data);        
         }
      }); 

      controlPanel.add(namelabel);
      controlPanel.add(userText);
      controlPanel.add(passwordLabel);       
      controlPanel.add(passwordText);
      controlPanel.add(loginButton);
      mainFrame.setVisible(true);  
   }
}
Compile the program using command prompt. Go to D:/ > AWT and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AwtControlDemo.java
If no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AwtControlDemo
Verify the following output
AWT TextField

Comments

Popular posts from this blog

unix commands