Objective
This topic will help you to program an application of RMI (Remote Method Invocation) technology. The case study is always an edit customer application.
Assume that we would like create an application as following description:
– A remote server RMI which provides a method enabling to search and edit customer information
– The RMI server also manages a database which contains a table of customer (id, name, idcard, address, email, tel, note).
– At the client side, a GUI is provided which enables user to search customer by name, and then, edit a customer.
– The client will call the remote method from server RMI to manipulate the related functions
Application Design
At the third side, there are entity classes (They could be implemented in a separated project and then, exported to a .jar file to be imported into the client project and the server project):
- Customer: this is an model class containing all necessary customer attributes. This class is commonly used on both client and server side.
- CustomerInterface: this is an interface which enables the client side could refer/bind the remote object via this interface. This interface has two abstract methods: searchCustomer() and editCustomer().
- IPAddress: the entity containing the information about the host and the port of a server

At the client side, there are following classes (some relationship among classes are omitted, these classes could also be implemented in a separated RMI client project) ):
- ClientMainFrm: the main client interface to manipulate the connection to the server: connect, disconnect, choose the function…
- SearchCustomerFrm: a graphical view class enabling user to search and choose the customer to update.
- EditCustomerFrm: the form to edit information of the chosen customer
- ClientCtr: a control class to lookup the remote object and call the methods of that object.
At the server side, there are some other classes (these classes could also be implemented in a separated RMI server project) :
- At the DAO level, there are two classes of DAO and CustomerDAO which has two method: searchCustomer() to search all customer whose name contains the input string, and editCustomer() to update the data of the given input customer.
- At the business control level, there is a class of ServerCtr which implements the CustomerInterface to implement and provide two methods: searchCustomer() and editCustomer().
- At the view level, there is the ServerMainFrm to manipulate the RMI service: bin/rebind, unbind, and display information a bout it.
The sequence of activities is represented as following sequence diagram:
- Step 1-8 : the server registries its service to the Registry at the server side
- Step 9-16: a client looks up the remote objects to use remote methods.
- Step 17-36: client user choose the search customer by name (17-22), the client calls the remote method from the server (24-26), the server searches in the DB (27-33), and returns the results to the client (34), the client show it to the user (35-36).
- Step 37-56: client user choose a customer to update (37-43), the client wraps the entity object (44-46) and then, call the remote method (49), the server updates it into the DB (50-53) and then, returns the results to the client (54). The client show the success message to the user (55-56).

Complete code
Class: Customer.java
package model;
import java.io.Serializable;
public class Customer implements Serializable{
private static final long serialVersionUID = 20210811004L;
private int id;
private String name;
private String idCard;
private String address;
private String tel;
private String email;
private String note;
public Customer() {
super();
}
public Customer(String name, String idCard, String address, String tel, String email, String note) {
super();
this.name = name;
this.idCard = idCard;
this.address = address;
this.tel = tel;
this.email = email;
this.note = note;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIdCard() {
return idCard;
}
public void setIdCard(String idCard) {
this.idCard = idCard;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
}
import java.io.Serializable;
public class User implements Serializable{
private String userName;
private String password;
public User(){
}
public User(String username, String password){
this.userName = username;
this.password = password;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
Class: IPAddress.java
package model;
import java.io.Serializable;
public class IPAddress implements Serializable{
private static final long serialVersionUID = 20210811012L;
private String host;
private int port;
public IPAddress() {
super();
}
public IPAddress(String host, int port) {
super();
this.host = host;
this.port = port;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}
CustomerInterface.java
package rmi.general;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.ArrayList;
import model.Customer;
public interface CustomerInterface extends Remote{
public ArrayList<Customer> searchCustomer(String key) throws RemoteException;
public boolean editCustomer(Customer cus) throws RemoteException;
}
RMILoginInterface.java
package rmi.server;
import java.rmi.Remote;
import java.rmi.RemoteException;
import rmi.server.User;
public interface RMILoginInterface extends Remote{
public String checkLogin(User user) throws RemoteException;
}
Class: DAO.java
package jdbc.dao;
import java.sql.Connection;
import java.sql.DriverManager;
public class DAO {
public static Connection con;
public DAO(){
if(con == null){
String dbUrl = "jdbc:mysql://localhost:3307/hotel?autoReconnect=true&useSSL=false";
String dbClass = "com.mysql.jdbc.Driver";
try {
Class.forName(dbClass);
con = DriverManager.getConnection (dbUrl, "root", "xxx");
}catch(Exception e) {
e.printStackTrace();
}
}
}
}
Class: CustomerDAO.java
package jdbc.dao;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import model.Customer;
public class CustomerDAO extends DAO{
/**
* search all clients in the tblClient whose name contains the @key
* using PreparedStatement - recommended for professional coding
* @param key
* @return list of client whose name contains the @key
*/
public ArrayList<Customer> searchCustomer(String key){
ArrayList<Customer> result = new ArrayList<Customer>();
String sql = "SELECT * FROM tblclient WHERE name LIKE ?";
try{
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, "%" + key + "%");
ResultSet rs = ps.executeQuery();
while(rs.next()){
Customer client = new Customer();
client.setId(rs.getInt("id"));
client.setName(rs.getString("name"));
client.setIdCard(rs.getString("idcard"));
client.setAddress(rs.getString("address"));
client.setTel(rs.getString("tel"));
client.setEmail(rs.getString("email"));
client.setNote(rs.getString("note"));
result.add(client);
}
}catch(Exception e){
e.printStackTrace();
}
return result;
}
/**
* update the @client
* @param client
*/
public boolean editCustomer(Customer client){
String sql = "UPDATE tblclient SET name=?, idcard =?, address=?, tel=?, email=?, note=? WHERE id=?";
try{
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, client.getName());
ps.setString(2, client.getIdCard());
ps.setString(3, client.getAddress());
ps.setString(4, client.getTel());
ps.setString(5, client.getEmail());
ps.setString(6, client.getNote());
ps.setInt(7, client.getId());
ps.executeUpdate();
}catch(Exception e){
e.printStackTrace();
return false;
}
return true;
}
}
ServerCtr.java
package rmi.server.control;
import java.net.InetAddress;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.ExportException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import jdbc.dao.CustomerDAO;
import jdbc.dao.UserDAO;
import model.Customer;
import model.IPAddress;
import model.User;
import rmi.general.CustomerInterface;
import rmi.general.UserInterface;
import rmi.server.view.ServerMainFrm;
public class ServerCtr extends UnicastRemoteObject implements CustomerInterface{
private IPAddress myAddress = new IPAddress("localhost", 9999); // default server host/port
private Registry registry;
private ServerMainFrm view;
private String rmiService = "rmiServer"; // default rmi service key
public ServerCtr(ServerMainFrm view) throws RemoteException{
this.view = view;
}
public ServerCtr(ServerMainFrm view, int port, String service) throws RemoteException{
this.view = view;
myAddress.setPort(port);
this.rmiService = service;
}
public void start() throws RemoteException{
// registry this to the localhost
try{
try {
//create new one
registry = LocateRegistry.createRegistry(myAddress.getPort());
}catch(ExportException e) {//the Registry exists, get it
registry = LocateRegistry.getRegistry(myAddress.getPort());
}
registry.rebind(rmiService, this);
myAddress.setHost(InetAddress.getLocalHost().getHostAddress());
view.showServerInfo(myAddress, rmiService);
view.showMessage("The RIM has registered the service key: " + rmiService + ", at the port: " + myAddress.getPort());
}catch(RemoteException e){
throw e;
}catch(Exception e) {
e.printStackTrace();
}
}
public void stop() throws RemoteException{
// unbind the service
try{
if(registry != null) {
registry.unbind(rmiService);
UnicastRemoteObject.unexportObject(this,true);
}
view.showMessage("The RIM has unbinded the service key: " + rmiService + ", at the port: " + myAddress.getPort());
}catch(RemoteException e){
throw e;
}catch(Exception e) {
e.printStackTrace();
}
}
@Override
public ArrayList<Customer> searchCustomer(String key) throws RemoteException {
return new CustomerDAO().searchCustomer(key);
}
@Override
public boolean editCustomer(Customer cus) throws RemoteException {
return new CustomerDAO().editCustomer(cus);
}
}
ServerMainFrm.java
package rmi.server.view;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import model.IPAddress;
import rmi.server.control.ServerCtr;
public class ServerMainFrm extends JFrame implements ActionListener{
private JTextField txtServerHost, txtServerPort, txtServerService;
private JButton btnStartServer, btnStopServer;
private JTextArea mainText;
private ServerCtr myServer;
public ServerMainFrm(){
super("RMI server view");
JPanel mainPanel = new JPanel();
mainPanel.setLayout(null);
JLabel lblTitle = new JLabel("Server RMI");
lblTitle.setFont(new java.awt.Font("Dialog", 1, 20));
lblTitle.setBounds(new Rectangle(150, 15, 200, 30));
mainPanel.add(lblTitle, null);
JLabel lblHost = new JLabel("Server host:");
lblHost.setBounds(new Rectangle(10, 70, 150, 25));
mainPanel.add(lblHost, null);
txtServerHost = new JTextField(50);
txtServerHost.setBounds(new Rectangle(170, 70, 150, 25));
txtServerHost.setText("localhost");
txtServerHost.setEditable(false);
mainPanel.add(txtServerHost,null);
JLabel lblPort = new JLabel("Server port:");
lblPort.setBounds(new Rectangle(10, 100, 150, 25));
mainPanel.add(lblPort, null);
txtServerPort = new JTextField(50);
txtServerPort.setBounds(new Rectangle(170, 100, 150, 25));
mainPanel.add(txtServerPort,null);
JLabel lblService = new JLabel("Service key:");
lblService.setBounds(new Rectangle(10, 150, 150, 25));
mainPanel.add(lblService, null);
txtServerService = new JTextField(50);
txtServerService.setBounds(new Rectangle(170, 150, 150, 25));
mainPanel.add(txtServerService,null);
btnStartServer = new JButton("Start server");
btnStartServer.setBounds(new Rectangle(10, 200, 150, 25));
btnStartServer.addActionListener(this);
mainPanel.add(btnStartServer,null);
btnStopServer = new JButton("Stop server");
btnStopServer.setBounds(new Rectangle(170, 200, 150, 25));
btnStopServer.addActionListener(this);
btnStopServer.setEnabled(false);
mainPanel.add(btnStopServer,null);
JScrollPane jScrollPane1 = new JScrollPane();
mainText = new JTextArea("");
jScrollPane1.setBounds(new Rectangle(10, 250, 610, 240));
mainPanel.add(jScrollPane1, BorderLayout.CENTER);
jScrollPane1.getViewport().add(mainText, null);
this.setContentPane(mainPanel);
this.pack();
this.setSize(new Dimension(640, 480));
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.addWindowListener( new WindowAdapter(){
public void windowClosing(WindowEvent e) {
if(myServer != null)
try {
myServer.stop();
}catch(Exception ex) {
ex.printStackTrace();
}
System.exit(0);
}
});
}
public void showServerInfo(IPAddress serverAddr, String service) {
txtServerHost.setText(serverAddr.getHost());
txtServerPort.setText(serverAddr.getPort()+"");
txtServerService.setText(service);
}
@Override
public void actionPerformed(ActionEvent ae) {
// TODO Auto-generated method stub
if(ae.getSource() instanceof JButton) {
JButton clicked = (JButton)ae.getSource();
if(clicked.equals(btnStartServer)) {// start button
try{
if(!txtServerPort.getText().isEmpty() && (txtServerPort.getText().trim().length() > 0) &&
!txtServerService.getText().isEmpty() && (txtServerService.getText().trim().length() > 0)) {//custom port
int port = Integer.parseInt(txtServerPort.getText().trim());
myServer = new ServerCtr(this, port, txtServerService.getText().trim());
}else {// default port
myServer = new ServerCtr(this);
}
myServer.start();
btnStopServer.setEnabled(true);
btnStartServer.setEnabled(false);
}catch(Exception e) {
mainText.append("\n Error in starting the RIM server");
e.printStackTrace();
}
}else if(clicked.equals(btnStopServer)) {// stop button
if(myServer != null) {
try {
myServer.stop();
myServer = null;
}catch(Exception e) {
mainText.append("\n Error in closing the RIM server");
e.printStackTrace();
}
}
btnStopServer.setEnabled(false);
btnStartServer.setEnabled(true);
txtServerHost.setText("localhost");
}
}
}
public void showMessage(String s){
mainText.append("\n"+s);
mainText.setCaretPosition(mainText.getDocument().getLength());
}
public static void main(String[] args) {
ServerMainFrm view = new ServerMainFrm();
view.setVisible(true);
}
}
ClientCtr.java
package rmi.client.control;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;
import model.Customer;
import model.IPAddress;
import model.User;
import rmi.client.view.ClientMainFrm;
import rmi.general.CustomerInterface;
import rmi.general.UserInterface;
public class ClientCtr {
private ClientMainFrm view;
private CustomerInterface customerRO;
private IPAddress serverAddress = new IPAddress("localhost", 9999); //default server address
private String rmiService = "rmiServer"; //default server service key
public ClientCtr(ClientMainFrm view){
this.view = view;
}
public ClientCtr(ClientMainFrm view, String serverHost, int serverPort, String service){
this.view = view;
serverAddress.setHost(serverHost);
serverAddress.setPort(serverPort);
rmiService = service;
}
public boolean init(){
try{
// get the registry
Registry registry = LocateRegistry.getRegistry(serverAddress.getHost(), serverAddress.getPort());
// lookup the remote objects
customerRO = (CustomerInterface)(registry.lookup(rmiService));
view.setServerHost(serverAddress.getHost(), serverAddress.getPort()+"", rmiService);
view.showMessage("Found the remote objects at the host: " + serverAddress.getHost() + ", port: " + serverAddress.getPort());
}catch(Exception e){
e.printStackTrace();
view.showMessage("Error to lookup the remote objects!");
return false;
}
return true;
}
public ArrayList<Customer> remoteSearchCustomer(String key){
try {
return customerRO.searchCustomer(key);
}catch(RemoteException e) {
e.printStackTrace();
return null;
}
}
public String remoteEditCustomer(Customer cus) {
try {
if(customerRO.editCustomer(cus))
return "ok";
else
return "failed";
}catch(RemoteException e) {
e.printStackTrace();
return null;
}
}
}
ClientMainFrm.java
package rmi.client.view;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import rmi.client.control.ClientCtr;
public class ClientMainFrm extends JFrame implements ActionListener{
private JMenuBar mnbMain;
private JMenu mnUser, mnClient;
private JMenuItem mniLogin, mniEditClient;
private JTextField txtServerHost, txtServerPort, txtService;
private JButton btnStart;
private JTextArea mainText;
private ClientCtr myControl;
public ClientMainFrm(){
super("RMI client view");
JPanel mainPanel = new JPanel();
mainPanel.setLayout(null);
mnbMain = new JMenuBar();
mnUser = new JMenu("User");
mniLogin = new JMenuItem("Login");
mniLogin.addActionListener(this);
mnUser.add(mniLogin);
mnbMain.add(mnUser);
mnClient = new JMenu("Customer");
mniEditClient = new JMenuItem("Edit customer");
mniEditClient.addActionListener(this);
mnClient.add(mniEditClient);
mnbMain.add(mnClient);
this.setJMenuBar(mnbMain);
mniLogin.setEnabled(false);
mniEditClient.setEnabled(false);
JLabel lblTitle = new JLabel("Client RMI");
lblTitle.setFont(new java.awt.Font("Dialog", 1, 20));
lblTitle.setBounds(new Rectangle(150, 20, 200, 30));
mainPanel.add(lblTitle, null);
JLabel lblHost = new JLabel("Server host:");
lblHost.setBounds(new Rectangle(10, 70, 150, 25));
mainPanel.add(lblHost, null);
txtServerHost = new JTextField(50);
txtServerHost.setBounds(new Rectangle(100, 70, 150, 25));
mainPanel.add(txtServerHost,null);
JLabel lblPort = new JLabel("Server port:");
lblPort.setBounds(new Rectangle(10, 100, 150, 25));
mainPanel.add(lblPort, null);
txtServerPort = new JTextField(50);
txtServerPort.setBounds(new Rectangle(100, 100, 150, 25));
mainPanel.add(txtServerPort,null);
JLabel lblClientHost = new JLabel("Server service key:");
lblClientHost.setBounds(new Rectangle(10, 130, 150, 25));
mainPanel.add(lblClientHost, null);
txtService = new JTextField(50);
txtService.setBounds(new Rectangle(100, 130, 150, 25));
mainPanel.add(txtService,null);
btnStart = new JButton("Lookup/Relookup");
btnStart.setBounds(new Rectangle(10, 180, 150, 25));
btnStart.addActionListener(this);
mainPanel.add(btnStart,null);
JScrollPane jScrollPane1 = new JScrollPane();
mainText = new JTextArea("");
jScrollPane1.setBounds(new Rectangle(10, 230, 610, 240));
mainPanel.add(jScrollPane1, BorderLayout.CENTER);
jScrollPane1.getViewport().add(mainText, null);
this.setContentPane(mainPanel);
this.pack();
this.setSize(new Dimension(640, 480));
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.addWindowListener( new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@Override
public void actionPerformed(ActionEvent ae) {
// TODO Auto-generated method stub
if(ae.getSource() instanceof JButton) {
JButton btn = (JButton)ae.getSource();
if(btn.equals(btnStart)) {// connect button
if(!txtServerHost.getText().isEmpty() && (txtServerHost.getText().trim().length() > 0) &&
!txtServerPort.getText().isEmpty() && (txtServerPort.getText().trim().length() > 0) &&
!txtService.getText().isEmpty() && (txtService.getText().trim().length() > 0)) {//custom server port
int serverPort = Integer.parseInt(txtServerPort.getText().trim());
myControl = new ClientCtr(this, txtServerHost.getText().trim(), serverPort, txtService.getText().trim());
}else {//default server host and port
myControl = new ClientCtr(this);
}
if(myControl.init()) {
mniLogin.setEnabled(true);
mniEditClient.setEnabled(true);
}else {
mniLogin.setEnabled(false);
mniEditClient.setEnabled(false);
}
}
}else if(ae.getSource() instanceof JMenuItem) {
JMenuItem mni = (JMenuItem)ae.getSource();
if(mni.equals(mniLogin)) {// login function
}else if(mni.equals(mniEditClient)) {// Search client to edit
SearchCustomerFrm scv = new SearchCustomerFrm(myControl);
scv.setVisible(true);
}
}
}
public void showMessage(String s){
mainText.append("\n"+s);
mainText.setCaretPosition(mainText.getDocument().getLength());
}
public void setServerHost(String serverHost, String serverPort, String service) {
txtServerHost.setText(serverHost);
txtServerPort.setText(serverPort);
txtService.setText(service);
}
public static void main(String[] args) {
ClientMainFrm view = new ClientMainFrm();
view.setVisible(true);
}
}
SearchCustomerFrm.java
package rmi.client.view;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import model.Customer;
import rmi.client.control.ClientCtr;
public class SearchCustomerFrm extends JFrame implements ActionListener{
private ArrayList<Customer> listCustomer;
private JTextField txtKey;
private JButton btnSearch;
private JTable tblResult;
private ClientCtr myRemoteObject;
public SearchCustomerFrm(ClientCtr ro){
super("Search customer to edit");
myRemoteObject = ro;
listCustomer = new ArrayList<Customer>();
JPanel pnMain = new JPanel();
pnMain.setSize(this.getSize().width-5, this.getSize().height-20);
pnMain.setLayout(new BoxLayout(pnMain,BoxLayout.Y_AXIS));
pnMain.add(Box.createRigidArea(new Dimension(0,10)));
JLabel lblHome = new JLabel("Search a customer to edit");
lblHome.setAlignmentX(Component.CENTER_ALIGNMENT);
lblHome.setFont (lblHome.getFont ().deriveFont (20.0f));
pnMain.add(lblHome);
pnMain.add(Box.createRigidArea(new Dimension(0,20)));
JPanel pn1 = new JPanel();
pn1.setLayout(new BoxLayout(pn1,BoxLayout.X_AXIS));
pn1.setSize(this.getSize().width-5, 20);
pn1.add(new JLabel("Client name: "));
txtKey = new JTextField();
pn1.add(txtKey);
btnSearch = new JButton("Search");
btnSearch.addActionListener(this);
pn1.add(btnSearch);
pnMain.add(pn1);
pnMain.add(Box.createRigidArea(new Dimension(0,10)));
JPanel pn2 = new JPanel();
pn2.setLayout(new BoxLayout(pn2,BoxLayout.Y_AXIS));
tblResult = new JTable();
JScrollPane scrollPane= new JScrollPane(tblResult);
tblResult.setFillsViewportHeight(false);
scrollPane.setPreferredSize(new Dimension(scrollPane.getPreferredSize().width, 250));
tblResult.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int column = tblResult.getColumnModel().getColumnIndexAtX(e.getX()); // get the coloum of the button
int row = e.getY() / tblResult.getRowHeight(); // get the row of the button
// *Checking the row or column is valid or not
if (row < tblResult.getRowCount() && row >= 0 && column < tblResult.getColumnCount() && column >= 0) {
(new EditCustomerFrm(myRemoteObject, listCustomer.get(row))).setVisible(true);
dispose();
}
}
});
pn2.add(scrollPane);
pnMain.add(pn2);
this.add(pnMain);
this.setSize(600,300);
this.setLocation(200,10);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
JButton btnClicked = (JButton)e.getSource();
if(btnClicked.equals(btnSearch)){
if((txtKey.getText() == null)||(txtKey.getText().length() == 0))
return;
listCustomer = (ArrayList<Customer>)myRemoteObject.remoteSearchCustomer(txtKey.getText());
if(listCustomer == null) {
JOptionPane.showMessageDialog(this, "Error connecting to RMI server!");
dispose();
}
String[] columnNames = {"Id", "Name", "idCard", "Address", "Email", "Tel", "Note"};
String[][] value = new String[listCustomer.size()][columnNames.length];
for(int i=0; i<listCustomer.size(); i++){
value[i][0] = listCustomer.get(i).getId() +"";
value[i][1] = listCustomer.get(i).getName();
value[i][2] = listCustomer.get(i).getIdCard();
value[i][3] = listCustomer.get(i).getAddress();
value[i][4] = listCustomer.get(i).getEmail();
value[i][5] = listCustomer.get(i).getTel();
value[i][6] = listCustomer.get(i).getNote();
}
DefaultTableModel tableModel = new DefaultTableModel(value, columnNames) {
@Override
public boolean isCellEditable(int row, int column) {
//unable to edit cells
return false;
}
};
tblResult.setModel(tableModel);
}else {
tblResult.setModel(null);
}
}
}
EditCustomerFrm.java
package rmi.client.view;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import model.Customer;
import rmi.client.control.ClientCtr;
public class EditCustomerFrm extends JFrame implements ActionListener{
private Customer customer;
private JTextField txtId, txtName, txtIdcard, txtAddress, txtEmail, txtTel, txtNote;
private JButton btnUpdate, btnReset;
private ClientCtr myRemoteObject;
public EditCustomerFrm(ClientCtr ro, Customer client){
super("Edit a customer");
myRemoteObject = ro;
this.customer = client;
JPanel pnMain = new JPanel();
pnMain.setSize(this.getSize().width-5, this.getSize().height-20);
pnMain.setLayout(new BoxLayout(pnMain,BoxLayout.Y_AXIS));
pnMain.add(Box.createRigidArea(new Dimension(0,10)));
JLabel lblHome = new JLabel("Edit a customer information");
lblHome.setAlignmentX(Component.CENTER_ALIGNMENT);
lblHome.setFont (lblHome.getFont ().deriveFont (20.0f));
pnMain.add(lblHome);
pnMain.add(Box.createRigidArea(new Dimension(0,20)));
txtId = new JTextField(15);
txtId.setEditable(false);
txtName = new JTextField(15);
txtIdcard = new JTextField(15);
txtAddress = new JTextField(15);
txtEmail = new JTextField(15);
txtTel = new JTextField(15);
txtNote = new JTextField(15);
btnUpdate = new JButton("Update");
btnReset = new JButton("Reset");
JPanel content = new JPanel();
content.setLayout(new GridLayout(8,2));
content.add(new JLabel("Customer ID:")); content.add(txtId);
content.add(new JLabel("Name:")); content.add(txtName);
content.add(new JLabel("Idcard:")); content.add(txtIdcard);
content.add(new JLabel("Address:")); content.add(txtAddress);
content.add(new JLabel("Email:")); content.add(txtEmail);
content.add(new JLabel("Tel:")); content.add(txtTel);
content.add(new JLabel("Note:")); content.add(txtNote);
content.add(btnUpdate); content.add(btnReset);
pnMain.add(content);
btnUpdate.addActionListener(this);
btnReset.addActionListener(this);
initForm();
this.setContentPane(pnMain);
this.setSize(600,300);
this.setLocation(200,10);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
private void initForm(){
if(customer != null){
txtId.setText(customer.getId()+"");
txtName.setText(customer.getName());
txtIdcard.setText(customer.getIdCard());
txtAddress.setText(customer.getAddress());
txtEmail.setText(customer.getEmail());
txtTel.setText(customer.getTel());
txtNote.setText(customer.getNote());
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JButton btnClicked = (JButton)e.getSource();
if(btnClicked.equals(btnReset)){
initForm();
return;
}
if(btnClicked.equals(btnUpdate)){
customer.setName(txtName.getText());
customer.setIdCard(txtIdcard.getText());
customer.setAddress(txtAddress.getText());
customer.setEmail(txtEmail.getText());
customer.setTel(txtTel.getText());
customer.setNote(txtNote.getText());
//call the remote function
if(myRemoteObject.remoteEditCustomer(customer) == null) {
JOptionPane.showMessageDialog(this, "Error in connecting to the RMI server!");
this.dispose();
}else if(myRemoteObject.remoteEditCustomer(customer).equals("ok")) {
JOptionPane.showMessageDialog(this, "Update succesfully!");
this.dispose();
}
else {
JOptionPane.showMessageDialog(this, "Error when updating!");
}
}
}
}