Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java RMI question

Options
  • 26-10-2009 3:36pm
    #1
    Registered Users Posts: 4,037 ✭✭✭


    I am creating an RMI application which creates a popup GUI with a text box and buttons. I have the method that creates the GUI in the same class as the RMI client (it has to be as the button clicks on the GUI call methods which do the RMI stuff.
    The compiler has a problem with two lines in my program :
    1. setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);:
    it says "the method setdefaultcloseoperation() is undefined for the type DVDClient"
    and also
    [2. B]this.getContentPane().add(JName, BorderLayout.CENTER);[/B]
    it says "the method getContentPane() is undefined for the type DVDClient"

    I know if I changed the class to implement ActionListener it would then work but I have an interface that I need the class to inherit from (ServerChanged).
    My class is below.Can anyone help?
    
    
    import java.awt.*;
    import java.awt.event.*;
    import java.rmi.*;
    import java.rmi.server.*;
    import java.net.*;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.WindowConstants;
    import javax.swing.JOptionPane;
    import java.awt.BorderLayout;
    
    
    public class DvdClient extends Frame implements ServerChanged{
       private Dvd remote;
       private Button dep, wdraw;
       private Label bal;
       private TextField amt;
       private float openBal;
       private Title dvdTitle;
       
       
    
    	private JLabel JName;
    
    	private JLabel jLabel1;
    
    	private JLabel jLabel2;
    
    	private JButton jSave;
    
    	private JButton jSearch;
    
    	private JButton jAdd;
    
    	private JButton jNext;
    
    	private JButton jPrevious;
    
    	private JTextField jTextNumber;
    
    	private JTextField jTextName;
    
    
       public DvdClient( String s ) {
          super( "Remote Bank Teller" );
          connectServer( s );
          //getDVDList();
         initGUI();
          
       }
    
       
       public void initGUI() {
    
    		try {
    			setSize(400, 250);
    			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    			{
    				JName = new JLabel();
    				this.getContentPane().add(JName, BorderLayout.CENTER);
    				{
    					jLabel1 = new JLabel();
    					JName.add(jLabel1);
    					jLabel1.setText("Name");
    					jLabel1.setBounds(44, -2, 60, 30);
    				}
    				{
    					jTextName = new JTextField();
    					JName.add(jTextName);
    					jTextName.setBounds(84, 5, 100, 18);
    					
    				}
    				{
    					jLabel2 = new JLabel();
    					JName.add(jLabel2);
    					jLabel2.setText("Number");
    					jLabel2.setBounds(202, 0, 60, 30);
    				}
    				{
    					jTextNumber = new JTextField();
    					JName.add(jTextNumber);
    					jTextNumber.setBounds(253, 5, 100, 19);
    				}
    				{
    					jPrevious = new JButton();
    					JName.add(jPrevious);
    					jPrevious.setText("Previous");
    					jPrevious.setBounds(114, 27, 84, 30);
    					jPrevious.addActionListener((ActionListener) this);
    				}
    				{
    					jNext = new JButton();
    					JName.add(jNext);
    					jNext.setText("Next");
    					jNext.setBounds(212, 27, 60, 30);
    					jNext.addActionListener((ActionListener) this);
    				}
    				{
    					jAdd = new JButton();
    					JName.add(jAdd);
    					jAdd.setText("Add");
    					jAdd.setBounds(67, 170, 60, 30);
    					jAdd.addActionListener((ActionListener) this);
    					{
    						jSearch = new JButton();
    						JName.add(jSearch);
    						jSearch.setText("Search");
    						jSearch.setBounds(130, 170, 77, 30);
    						jSearch.addActionListener((ActionListener) this);
    					}
    					{
    						jSave = new JButton();
    						JName.add(jSave);
    						jSave.setText("Save Directory");
    						jSave.setBounds(212, 170, 135, 31);
    						jSave.addActionListener((ActionListener) this);
    					}
    				}
    
    				//readFile();
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    
    	}
       public void setBal( float balance ) throws RemoteException {
          bal.setText( "Balance: $ " + balance ); 
       }
       public static void main( String argv[ ] ) {
          System.setSecurityManager( new RMISecurityManager() );
          DVDgui myGUI =new DVDgui();
    		//PDGUI.readFile();
          myGUI.show();
       }
    
       public void connectServer( String s ) { 
          try {
             remote = (Dvd)Naming.lookup( "rmi://" + s + "/DvdServer" );
             UnicastRemoteObject.exportObject( this,0 );
             remote.register( this );
          }catch (Exception e ) {
             System.out.println( "Exception: " + e.getMessage() );
             e.printStackTrace();
          }
       }
    	
       public void getDVDList() {
          try {
             openBal = remote.getBal();
          }catch ( RemoteException e ) {
             System.out.println( "Exception: " + e.getMessage() );
             e.printStackTrace();
          }
       }
    
           class windowHandler extends WindowAdapter {
          public void windowClosing( WindowEvent event ) {
             System.exit( 0 );   
          }
       }
    
       class actionHandler implements ActionListener {
          public void actionPerformed( ActionEvent event ) {
             float balance, amount;
             try {
                amount = new Float( amt.getText() ). floatValue();
             } catch ( NumberFormatException e ) {
                amount = 0;
             }		 		
             
             Object source = event.getSource();
             try {
                if ( source == dep ) {
                   remote.deposit( amount );
                   return;
                } 
                else 
                   if ( source == wdraw ) {
                      remote.withdraw( amount );
                      return;
                   }
             }catch ( Exception e ) { 
                System.out.println( "Exception: " + e.getMessage() );
                e.printStackTrace();
             }
          }
       }
    }
    


Comments

  • Registered Users Posts: 1,998 ✭✭✭lynchie


    If you check the javadocs for the declaration of those two method you will see where your error is. Both are declared in JFrame yet you are not extending JFrame in your DVDClient class.


  • Registered Users Posts: 4,037 ✭✭✭lukin


    lynchie wrote: »
    If you check the javadocs for the declaration of those two method you will see where your error is. Both are declared in JFrame yet you are not extending JFrame in your DVDClient class.

    Changed it to this and it seems to work now
    
    
    import java.awt.*;
    import java.awt.event.*;
    import java.rmi.*;
    import java.rmi.server.*;
    import java.net.*;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.WindowConstants;
    import javax.swing.JOptionPane;
    
    import java.awt.BorderLayout;
    
    
    public class DvdClient extends JFrame implements ServerChanged{
       private Dvd remote;
       private Button dep, wdraw;
       private Label bal;
       private TextField amt;
       private float openBal;
       private Title dvdTitle;
       
       
    
    	private JLabel JName;
    
    	private JLabel jLabel1;
    
    	private JLabel jLabel2;
    
    	private JButton jSave;
    
    	private JButton jSearch;
    
    	private JButton jAdd;
    
    	private JButton jNext;
    
    	private JButton jPrevious;
    
    	private JTextField jTextNumber;
    
    	private JTextField jTextName;
    
    
       public DvdClient( String s ) {
          super( "Remote Bank Teller" );
          connectServer( s );
          getDVDList();
         initGUI();
         this.addWindowListener( new windowHandler() );
          
       }
    
       
       public void initGUI() {
    
    		try {
    			setSize(400, 250);
    			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    			{
    				JName = new JLabel();
    				this.getContentPane().add(JName, BorderLayout.CENTER);
    				{
    					jLabel1 = new JLabel();
    					JName.add(jLabel1);
    					jLabel1.setText("Name");
    					jLabel1.setBounds(44, -2, 60, 30);
    				}
    				{
    					jTextName = new JTextField();
    					JName.add(jTextName);
    					jTextName.setBounds(84, 5, 100, 18);
    					
    				}
    				{
    					jLabel2 = new JLabel();
    					JName.add(jLabel2);
    					jLabel2.setText("Number");
    					jLabel2.setBounds(202, 0, 60, 30);
    				}
    				{
    					jTextNumber = new JTextField();
    					JName.add(jTextNumber);
    					jTextNumber.setBounds(253, 5, 100, 19);
    				}
    				{
    					jPrevious = new JButton();
    					JName.add(jPrevious);
    					jPrevious.setText("Previous");
    					jPrevious.setBounds(114, 27, 84, 30);
    					jPrevious.addActionListener((ActionListener) this);
    				}
    				{
    					jNext = new JButton();
    					JName.add(jNext);
    					jNext.setText("Next");
    					jNext.setBounds(212, 27, 60, 30);
    					jNext.addActionListener((ActionListener) this);
    				}
    				{
    					jAdd = new JButton();
    					JName.add(jAdd);
    					jAdd.setText("Add");
    					jAdd.setBounds(67, 170, 60, 30);
    					jAdd.addActionListener((ActionListener) this);
    					{
    						jSearch = new JButton();
    						JName.add(jSearch);
    						jSearch.setText("Search");
    						jSearch.setBounds(130, 170, 77, 30);
    						jSearch.addActionListener((ActionListener) this);
    					}
    					{
    						jSave = new JButton();
    						JName.add(jSave);
    						jSave.setText("Save Directory");
    						jSave.setBounds(212, 170, 135, 31);
    						jSave.addActionListener((ActionListener) this);
    					}
    				}
    
    				//readFile();
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    
    	}
       public void setBal( float balance ) throws RemoteException {
          bal.setText( "Balance: $ " + balance ); 
       }
       public static void main( String argv[ ] ) {
          System.setSecurityManager( new RMISecurityManager() );
          try {
              Frame frame = new DvdClient( argv[0] );
              frame.setBackground( Color.lightGray );
              frame.pack();
              frame.setVisible(true);
              frame.setSize( 220, 150 );
           }catch (Exception e ) {
              System.out.println( "Exception: " + e.getMessage() );
              e.printStackTrace();
           }
         
       }
    
       public void connectServer( String s ) { 
          try {
             remote = (Dvd)Naming.lookup( "rmi://" + s + "/DvdServer" );
             UnicastRemoteObject.exportObject( this,0 );
             remote.register( this );
          }catch (Exception e ) {
             System.out.println( "Exception: " + e.getMessage() );
             e.printStackTrace();
          }
       }
    	
       public void getDVDList() {
          try {
             openBal = remote.getBal();
          }catch ( RemoteException e ) {
             System.out.println( "Exception: " + e.getMessage() );
             e.printStackTrace();
          }
       }
    
           class windowHandler extends WindowAdapter {
          public void windowClosing( WindowEvent event ) {
             System.exit( 0 );   
          }
       }
    
       class actionHandler implements ActionListener {
          public void actionPerformed( ActionEvent event ) {
             float balance, amount;
             try {
                amount = new Float( amt.getText() ). floatValue();
             } catch ( NumberFormatException e ) {
                amount = 0;
             }		 		
             
             Object source = event.getSource();
             try {
                if ( source == dep ) {
                   remote.deposit( amount );
                   return;
                } 
                else 
                   if ( source == wdraw ) {
                      remote.withdraw( amount );
                      return;
                   }
             }catch ( Exception e ) { 
                System.out.println( "Exception: " + e.getMessage() );
                e.printStackTrace();
             }
          }
       }
    }
    
    
    


  • Registered Users Posts: 4,037 ✭✭✭lukin


    Sorry admins, I never marked it as "question". You can mark it "answered" as I can't.


Advertisement