Java InstantCode. Developing Applications Using Java NIO
Creating the User Interface for the Network Information Application
The NetCompFrame.java file helps create a user interface that allows you to retrieve a list of network computers. You can use this list to retrieve the echo time or the current system date and time of a network computer.
Listing 7-1 shows the contents of the NetCompFrame.java file:
Listing 7-1: The NetCompFrame.java File
|
/*Imports javax.swing package classes. */ import javax.swing.*; /*Imports javax.swing.event package classes. */ import javax.swing.event.*; /*Imports java.awt package classes. */ import java.awt.*; import java.awt.event.*; /*Imports java.awt.event package classes. */ import java.net.*; /*Imports java.util package classes. */ import java.util.*; /* Class NetCompFrame - This class creates the user interface of the Network Information application. This user interface displays the list of all the computers connected to the network. Fields: cont - Creates the container for the application. findComp_lb1 - Creates the label of Service/Protocol. findComp_cmb - Creates the combo box to select the service or protocol. netMask_lbl - Creates the label of the Host IP Address. netMask_txt - Creates the text box where the end user can specify the IP address. netComp_lbl - Creates the label of the network computers. netComp_lst - Creates a list box that displays the computer name. netComp_sp - Creates the scrollpane for the list box. showComp_btn -Creates the Display List button. clear_btn - Creates the Clear button. close_btn - Creates the Close button. listDataVec - Stores the list of computers in vector form. compInfoVec - Stores the computer's information in the vector form. selectedItem - Contains the selected item from the list. Methods: actionPerformed() - This method is invoked when end user clicks any of the button of the network management application. getMachinesList() - This method is invoked to display the list of computers connected in the network. valueChanged() - This method is invoked end user selects the computer from the computer list. itemStateChanged() - This method is invoked when end user selects the service/ protocol from the combo box. main() This method creates the main window of the application. */ public class NetCompFrame extends JFrame implements ActionListener, ListSelectionListener, ItemListener { /* Declares the object of the Container class. */ Container cont = null; /* Declares the objects of the JLabel class. */ JLabel findComp_lbl; JLabel netMask_lbl; JLabel netComp_lbl; /* Declares the objects of the JComboBox class. */ JComboBox findComp_cmb; /* Declares the object of the JTextField class. */ JTextField netMask_txt; /* Declares the object of the JList class. */ JList netComp_lst; /* Declares the object of the JScrollPane class. */ JScrollPane netComp_sp; /* Declares the objects of the JButton class. */ JButton showComp_btn; JButton clear_btn; JButton close_btn; /* Declares and initializes the objects of the Vector class. */ Vector listDataVec = new Vector(); Vector compInfoVec = new Vector(); /* Declares the object of String class. */ String selectedItem = ""; /* Declares the object of the NetCompConnect class. */ NetCompConnect compConnect = null; /* Declares the object of the CompInfoDialog class. */ CompInfoDialog dialogBox = null; /* Defines the default constructor. */ public NetCompFrame(String title) { super(title); try { /* Initializes and sets the look and feel of the application to Windows look and feel. */ UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch(Exception e){} /* Sets the size of the application. */ setSize(350, 400); /* addWindowListener - It contains a windowClosing() method. windowClosing: It is called when the end user clicks the close button of the Window. It closes the main window. Parameter: we Represents the object of the WindowEvent class. Return Value: NA */ addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { System.exit(0); } }); /* Initializes the object of the Container class. */ cont = getContentPane(); /* Sets the layout of the container to NULL layout. */ cont.setLayout(null); /* Initializes the objects of the JLabel class. */ findComp_lbl = new JLabel("Service/Protocol: "); netMask_lbl = new JLabel("Host IP Address: "); netComp_lbl = new JLabel("Network Computers: "); /* Initializes the object of the JTextField class. */ netMask_txt = new JTextField(); netMask_txt.setFont(new Font("Verdana", Font.PLAIN, 12)); /* Initializes the object of the JComboBox class. */ findComp_cmb = new JComboBox(); findComp_cmb.setFont(new Font("Verdana", Font.PLAIN, 12)); /* Adds the items in the combo box. */ findComp_cmb.addItem("Echo"); findComp_cmb.addItem("Date Time"); /* Adds the itemListener event to the combo box. */ findComp_cmb.addItemListener(this); /* Initializes the object of the JList class. */ netComp_lst = new JList(); netComp_lst.setFont(new Font("Verdana", Font.PLAIN, 12)); netComp_lst.addListSelectionListener(this); /* Initializes the object of the JScrollPane class. */ netComp_sp = new JScrollPane(netComp_lst); /* Initializes the objects of the JButton class. */ showComp_btn = new JButton("Display List"); clear_btn = new JButton("Clear"); close_btn = new JButton("Close"); /* Adds actionListener events to the JButton objects. */ showComp_btn.addActionListener(this); clear_btn.addActionListener(this); close_btn.addActionListener(this); /* Sets the position of the components on the application window. */ findComp_lbl.setBounds(10, 10, 140, 20); findComp_cmb.setBounds(160, 10, 100, 20); netMask_lbl.setBounds(10, 40, 110, 20); netMask_txt.setBounds(160, 40, 170, 20); showComp_btn.setBounds(230, 70, 100, 25); netComp_lbl.setBounds(10, 100, 300, 20); netComp_sp.setBounds(10, 125, 320, 200); clear_btn.setBounds(120, 335, 100, 25); close_btn.setBounds(230, 335, 100, 25); /* Adds the swing components on the container. */ cont.add(findComp_lbl); cont.add(findComp_cmb); cont.add(netMask_lbl); cont.add(netMask_txt); cont.add(netComp_lbl); cont.add(netComp_sp); cont.add(showComp_btn); cont.add(clear_btn); cont.add(close_btn); /* Sets the visibility of the frame to true. */ setVisible(true); /* Sets the reliability of the frame to false. */ setResizable(false); } /* actionPerformed() - This method is called when the end user clicks any of the button from the window. Parameters: ae Represents an object of the ActionEvent class that contains the details of the event. Return Value: NA */ public void actionPerformed(ActionEvent ae) { /* This section is executed when an end user clicks the Display List button. */ if (ae.getSource() == showComp_btn) { /* Gets the computer name from the selected item. */ String cmb_selection = (String)findComp_cmb.getSelectedItem(); /* This section is executed when the end user selects the "Echo" option from the combo box. */ if (cmb_selection.trim().equals("Echo")) { /* Gets the machine IP address from the Host IP Address text field. */ String machineIPAddress = netMask_txt.getText(); if (machineIPAddress==null machineIPAddress.trim().equals("")) { /* Displays an Error message dialog box. */ JOptionPane.showMessageDialog(this, "Please Enter the IP address of your machine."); /* Clears the text of the Host IP Address text field. */ netMask_txt.setText(""); return; } try { /* Creates and initializes the object of the InetAddress class. */ InetAddress myMachineAddress = InetAddress.getByName(machineIPAddress.trim()); } catch (Exception ex) { /* Displays an Error message dialog box. */ JOptionPane.showMessageDialog(this, "Please enter a valid IP address."); /* Clears the text of the Host IP Address text field. */ netMask_txt.setText(""); return; } /* Calls the getMachinesList() method. */ getMachinesList(machineIPAddress.trim(), 7); } /* This section is executed when end user selects the "Date Time" option from the combo box. */ else if (cmb_selection.trim().equals("Date Time")) { /* Gets the machine IP address from the Host IP Address text field. */ String machineIPAddress = netMask_txt.getText(); if (machineIPAddress==null machineIPAddress.trim().equals("")) { /* Displays an Error message dialog box. */ JOptionPane.showMessageDialog(this, "Please Enter the IP address of your machine."); /* Clears the text of the Host IP Address text field. */ netMask_txt.setText(""); return; } try { /* Creates and initializes the object of the InetAddress class. */ InetAddress myMachineAddress = InetAddress.getByName(machineIPAddress.trim()); } catch (Exception ex) { /* Displays an Error message dialog box. */ JOptionPane.showMessageDialog(this, "Please enter a valid IP address."); /* Clears the text of the Host IP Address text field. */ netMask_txt.setText(""); return; } /* Calls the getMachinesList() method. */ getMachinesList(machineIPAddress.trim(), 13); } } /* This section is executed when an end user clicks the Clear button. */ else if (ae.getSource() == clear_btn) { /* Calls the setListData() method of the JList class. */ netComp_lst.setListData(listDataVec=new Vector()); } /* This section is executed when an end user clicks the Close button. */ else if (ae.getSource() == close_btn) { System.exit(0); } } /* getMachinesList() - This method is invoked when the end user clicks the Display List button of the Network Management Application window. Parameters: myAddress - Contains the IP address of the user machine. port - Contains the port number of the user machine. Return Value: NA */ public void getMachinesList(String myAddress, int port) { /* Initializes the object of the Vector class. */ compInfoVec = new Vector(); /* Calls the setListData() method of the JList class. */ netComp_lst.setListData(listDataVec=new Vector()); /* Creates and initializes the object of the StringTokenizer class. */ StringTokenizer st = new StringTokenizer(myAddress, "."); /* Gets the IP address from the tokenize string. */ String rawIPAddress = (String)st.nextToken() + "." + (String)st.nextToken() + "." + (String)st.nextToken() + "."; /* Initializes an object of the String class to store the IP address. */ String testIPAddress = null; for (int i=0; i<256; i++) { /* Gets the IP address of the computers that are connected to the network. */ testIPAddress = rawIPAddress + i; /* Creates and initializes the object of the NetCompConnect class. */ compConnect = new NetCompConnect(testIPAddress, this, port); /* Calls the start() method of the NetCompConnect class. */ compConnect.start(); } try { Thread.sleep(5000); } catch (InterruptedException iex){} /* Evaluates the size of the vector. */ int size = compInfoVec.size(); for (int j=0; j<size; j++) { /* Creates and initializes the object of the CompInfo class. */ CompInfo tempObj = (CompInfo)compInfoVec.elementAt(j); /* Inserts the element at the end of the vector. */ listDataVec.addElement(tempObj.getAddress()); } /* Calls the setListData() method of the JList class. */ netComp_lst.setListData(listDataVec); } /* valueChanged() - This method is called when the end user selects the computer name from the list. Parameters: e Represents an object of the ListSelectionEvent class that contains the details of the event. Return Value: NA */ public void valueChanged(ListSelectionEvent e) { /* This section is executed when an end user changes the selection value of the computer from the list. */ if (e.getSource() == netComp_lst) { /* Gets the computer name from the selected item. */ String selectedItem2 = (String)netComp_lst.getSelectedValue(); if (selectedItem2 == null) { return; } if (selectedItem.equals(selectedItem2)) { /* Clears the item selection in the list. */ netComp_lst.clearSelection(); } /* This section is executed when the previously selected and currently selected computer names are different. */ if (!selectedItem.equals(selectedItem2)) { selectedItem = selectedItem2; /* Evaluates the size of the vector. */ int size = compInfoVec.size(); for (int j=0; j<size; j++) { /* Creates and initializes the object of the CompInfo class. */ CompInfo tempObj = (CompInfo)compInfoVec.elementAt(j); /* Gets the IP address using the getAddress() method of the CompInfo class. */ String compAddress = tempObj.getAddress(); if (compAddress.equals(selectedItem)) { /* Gets the computer name from the selected item. */ String cmb_selection = (String)findComp_cmb.getSelectedItem(); /* Gets the computer information of the selected item using the getCompInfo() method of the CompInfo class. */ String compInfo = tempObj.getCompInfo(); /* This section is executed when the end user selects "Echo" from the combo box. */ if (cmb_selection.trim().equals("Echo")) { /* Creates and initializes the object of the CompInfoDialog class. */ dialogBox = new CompInfoDialog(this, "Echo Response", true, 7, compInfo); /* Clears the item selection in the list. */ netComp_lst.clearSelection(); } /* This section is executed when the end user selects "Date Time" from the combo box. */ else if (cmb_selection.trim().equals("Date Time")) { /* Creates and initializes the object of the CompInfoDialog class. */ dialogBox = new CompInfoDialog(this, "System Date & Time", true, 13, compInfo); /* Clears the item selection in the list. */ netComp_lst.clearSelection(); } break; } } } } } /* itemStateChanged - This method is called when the end user selects the Service/Protocol from the combo box. Parameters: ie - a ListSelectionEvent object containing details of the event. Return Value: NA */ public void itemStateChanged(ItemEvent ie) { /* This section is executed when the end user selects any value from the combo box. */ if (ie.getSource() == findComp_cmb) { /* Calls the setListData() method of the JList class. */ netComp_lst.setListData(listDataVec=new Vector()); } } /* main() - This method creates the main window of the user interface and displays it. Parameters: args[] - Contains any command line arguments passed. Return Value: NA */ public static void main(String[] args) { /* Creates and initializes the object of the NetCompFrame class. */ NetCompFrame frame = new NetCompFrame("Network Information Application"); } }
|
Download this Listing .
In the above code, the main() method creates an instance of the NetCompFrame class. This class generates the main window of the Network Information application, as shown in Figure 7-2:
When the end user specifies the required service or protocol in the combo box and the host IP address in the Host IP Address text field, a list of network computers is displayed in the Network Computers list box.
The methods defined in the above code are:
-
actionPerformed() : Acts as an event listener and activates an appropriate class or method based on the button the end user clicks. If the end user clicks the Display List button, the actionPerformed() method retrieves the computer name from the selected item and checks the combo box value. If the end user selects Echo in the combo box, the actionPerformed() method retrieves the host IP from the Host IP Address text field and calls the getMachinesList() method with port number 7. If the end user selects the Date/Time value, the actionPerformed() method retrieves the host IP from the Host IP Address text field and calls the getMachinesList() method with port number 13. When an end user clicks the Clear button, the actionPerformed() method calls the setListData() method of the JList class. The setListData() method clears the list box. When an end user clicks the Close button, the actionPerformed() method calls the System.exit(0) method to close the application.
-
getMachinesList() : Retrieves the IP address of the network computers and initializes the object of the NetCompConnect class. This method then calls the start() method of the NetCompConnect class and initializes the object of the CompInfo class. Next , the getMachinesList() method inserts the element at the end of the vector and calls the setListData() method of the JList class to add the list in the list box.
-
valueChanged() : Acts as an event listener and activates an appropriate class or method based on the list value the end user selects. When the end user changes the selection of the item in the list, this method checks the selected item with the previously selected item. If both the item names are same, this method calls a break; otherwise it creates and initializes the object of the CompInfo class. This method then gets the IP address using the getAddress() method of the CompInfo class and gets the computer information of the selected item using the getCompInfo() method of the CompInfo class. Next, the valueChanged() method checks the combo box value and creates the object of the CompInfoDialog class based to the value specified in the combo box.
-
itemStateChanged() : Acts as an event listener and activates an appropriate class or method based on the option the end user changes in the combo box. After the end user modifies the option in the combo box, this method calls the setListData() method of the JList class. The setListData() method clears the list box.
Категории