The ReservationResultTable.java file shows the available airline schedules in the user interface of the Airline Reservation application. Listing 6-6 shows the contents of the ReservationResultTable.java file:
Listing 6-6: The ReservationResultTable.java File
/* Imports required classes from javax.swing package */ import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; /* Imports required classes from javax.swing.table package. */ import javax.swing.table.*; /* Imports required classes from javax.swing.event package. */ import javax.swing.event.*; /* Imports required classes from javax.swing package. */ import javax.swing.*; /* Imports required classes from java.awt package. */ import java.awt.*; /* Imports required classes from java.awt.event package. */ import java.awt.event.*; /* class SocketClass - This class is used for reading the input messages and writing output messages. Constructor: SocketClass - This constructor calls a method of SocketOpener class to open a client socket. Methods: */ public class ReservationResultTable extends JPanel { /* Declare the object of the ReservationTableModel class. */ ReservationTableModel tablemodel; /* Declare the object of the JTable class.*/ JTable table; public ReservationResultTable() { super(new GridLayout(1,0)); /* Initializes the object of ReservationTableModel class. */ tablemodel=new ReservationTableModel(); /* Initializes the object of JTable class. */ table = new JTable(tablemodel); table.setPreferredScrollableViewportSize(new Dimension(600, 100)); /* Declare and initialize the object of JScrollPane class.*/ JScrollPane scrollPane = new JScrollPane(table); add(scrollPane); setPreferredSize(new Dimension(600, 100)); } /* class ReservationTableModel - This class is used to insert data into table. Methods: getColumnCount - Returns number of column in the table. setData - Set data into table cell. getRowCount -gets number of rows in the table. isCellEditable - Set cell edit property. setValueAt - Set data into table cell. */ class ReservationTableModel extends AbstractTableModel { private String[] columnNames = {"Choice","Flight No.", "Aircraft type", "Origin", "No. of Stops", "Destination", "Dep.Date-time", "Arr.Date-time", "Executive Class", "Economy Class", }; private Object[][] data = {}; /* getColumnCount - Returns number of column in the table. Parameter: N/A Return Value: int */ public int getColumnCount() { return columnNames.length; } /* setData - Set data into table celles. Parameter: dataarr - array of Object Type. Return Value: int */ public void setData(Object[][] dataarr) { data=dataarr; } /* getRowCount -gets number of rows in the table. Parameter: dataarr - array of Object Type. Return Value: int */ public int getRowCount() { return data.length; } /* getColumnName - Returns Column name Parameter: col Return Value: String */ public String getColumnName(int col) { return columnNames[col]; } /* getValueAt - Returns value from a table cell. Parameter: row, col Return Value: Object */ public Object getValueAt(int row, int col) { return data[row][col]; } /* getColumnClass - Returns the Class of a table cell. Parameter: col Return Value: Class */ public Class getColumnClass(int col) { return getValueAt(0, col).getClass(); } /* isCellEditable - Set cell edit property. Parameter: row, col Return Value: boolean */ public boolean isCellEditable(int row, int col) { if (col >=1&&col<=7) { return false; } else { return true; } } /* setValueAt - Set data into table cell. Parameter: value - Object of Object type, row, col Return Value: N/A */ public void setValueAt(Object value, int row, int col) { data[row][col] = value; fireTableCellUpdated(row, col); } } }
Download this listing.
In the above code, the constructor of the ReservationResultTable class creates a new instance of the ReservationTableModel class. The ReservationTableModel class shows the matched airline schedules received from the Jabber server in a tabular format.
The methods defined in Listing 6-6 are:
getRowCount(): Returns the total number of records present in the search result.
isCellEditable(): Checks whether or not the information received from the Jabber server is editable.
setValueAt(): Shows the matched airline schedules in the user interface of the Airline Reservation application.
The ReservationResultTable.java file shows the airline schedules in the main window of the Airline Reservation application, as shown in Figure 6-4:
Figure 6-4: Showing the Airline Schedules
A confirmation message appears when an end user clicks the Submit button, as shown in Figure 6-5:
Figure 6-5: Confirmation Message