RMI client application; copying a class
Code:
//Client gui java application for assignment 3
//Matthew Faria-Seerattan
//Last updated: July 26 2011
//GUI;
//clicking connect and disconnect pops up a dialog box
//clicking a operation pops up a dialog box
//clicking ok button displays the address and coordinates entered
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.rmi.Naming;
public class client extends JFrame{
private JRadioButton jrC, jrD; //connect and disconnect
private JTextField jtAddress, jtX, jtY; //Text field for coordinates and address
private JComboBox jcOperation; //add, delete and retrieve
private JButton jbOK;
private String Address, x, y, displayAddressInfo;
private JTextArea jtaLocations;
private DataAccess da2;
//private String[] resetInfo = {"Connection:", "Operation:"};
//private String[] info;
//scrollable text area needed
//used for JComboBox values
private String[] operations = {"add","delete", "report generation"};
public client()
{
//name of client window
super("Address Database");
//rmi connection stuff
String url = "rmi://localhost:6666/";
try
{
System.out.println("Running a client application....");
System.out.println("These services have been registered with the RMI registry");
String names[] = Naming.list("rmi://localhost:6666");
for(int k=0;k<names.length;k++)
{
System.out.println("....." + names[k]);
}
System.out.println("Bind object variables to remote objects.....");
DataAccess da1 = (DataAccess) Naming.lookup(url + "address");
[B]da2 = da1;[/B]
}
catch(Exception e)
{
System.out.println("Error: " + e);
}
//values for the database connect/disconnect buttons
jrC = new JRadioButton("Connect", false);
jrD = new JRadioButton("Disconnect", false);
//button group for the above buttons
ButtonGroup bgroup = new ButtonGroup();
bgroup.add(jrC); bgroup.add(jrD);
JPanel jpCorD = new JPanel(); //panel for the radio buttons
jpCorD.add(jrC);
jpCorD.add(jrD);
JPanel jpCombo = new JPanel(); //combo box
JPanel jpTextAddress = new JPanel(); //text fields
JPanel jpTextCoor = new JPanel();
//the list of operations is made
jcOperation = new JComboBox(operations);
jcOperation.setEditable(false);
jcOperation.setMaximumRowCount(3);
jcOperation.insertItemAt("Select an operation", 0);
jcOperation.setSelectedIndex(0);
JLabel Address = new JLabel("Enter Address: ");
JLabel Yor = new JLabel("Y coordinate: ");
JLabel Xor = new JLabel("X coordinate: ");
JLabel Oper = new JLabel("Operations");
jtAddress = new JTextField(15);
jtX = new JTextField(15);
jtY = new JTextField(15);
jpCombo.add(Oper);
jpCombo.add(jcOperation);
jpTextAddress.add(Address);
jpTextAddress.add(jtAddress);
jpTextCoor.add(Xor);
jpTextCoor.add(jtX);
jpTextCoor.add(Yor);
jpTextCoor.add(jtY);
JPanel displayLocations = new JPanel();
jtaLocations = new JTextArea("--Locations--", 7,25);
jtaLocations.setEditable(false);
displayLocations.add(new JScrollPane(jtaLocations));
Container c = getContentPane();
c.setLayout(new FlowLayout());
jbOK = new JButton("OK");
c.add(jpCorD);
c.add(jpCombo);
c.add(jpTextAddress);
c.add(jpTextCoor);
c.add(jbOK);
c.add(displayLocations);
setSize(550,300);
setVisible(true);
registerListeners();
}
/*private void resetRadioButtons()
{
jrD.setSelected(true);
info[0] = resetInfo[0];
info[0] += "Disconnected";
}*/
private void registerListeners()
{
//info = new String[resetInfo.length];
//System.arraycopy(resetInfo, 0, info, 0, info.length);
//register a listener for the radio buttons
RadioHandler rh = new RadioHandler();
jrC.addItemListener(rh);
jrD.addItemListener(rh);
//register combo box
jcOperation.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
if(e.getStateChange() == ItemEvent.SELECTED && jcOperation.getSelectedIndex() > 0)
{
JOptionPane.showMessageDialog(null, "You selected: " + (String)jcOperation.getSelectedItem());
if((String)jcOperation.getSelectedItem() == "report generation")
{
jtaLocations.append("\nTest Data; 70 Avenue Rd. 87 90");
}
}
}
});
//register text field
/*jtAddress.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Address = jtAddress.getText();
}
});*/
//register ok button
jbOK.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Address = jtAddress.getText();
x = jtX.getText();
y = jtY.getText();
displayAddressInfo = "Address: " + Address + " Coordinates: " + x + y;
JOptionPane.showMessageDialog(null, displayAddressInfo, "Summary", JOptionPane.INFORMATION_MESSAGE);
//System.arraycopy(resetInfo, 1, info, 1, info.length);
//resetRadioButtons();
}
});
}
class RadioHandler implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
//info[0] = resetInfo[0];
if((e.getSource() == jrC) && (e.getStateChange() == ItemEvent.SELECTED))
{
//info[0] += "Connected";
JOptionPane.showMessageDialog(null, "Connected to database");
da2.connectToDatabase();
}
else if((e.getSource() == jrD) && (e.getStateChange() == ItemEvent.SELECTED))
{
//info[0] += "Disconnected";
JOptionPane.showMessageDialog(null, "Disconnected from database");
}
}
}
public static void main(String[] args)
{
client c = new client();
c.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
I am just wondering if the line of code i bolded is safe to do. I want to be able to use the connectToDatabase() method found in the DataAccess class and this was the only way i could think of doing it. The whole program works and compiles and does what i need it to do. But im just checking if this is a logical way of accomplishing what i need.