• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

AdviceClient - GUI Version

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,
Well, just want to share my experience with my fellow ranchers.
During my studies, i found it bored having to swap btw windows to watch
both my AdviceClient and the j2ee output window. So, I decided to create
a GUI version of the same application. It's a very simple application,
all you have to do is just click the Advice Button and you get a dialog box
box with the response from the server.Sorry this is a long page, but i hope it worth the effort.So the code is bellow :

/**
To run this application just follow the following steps.
1.Copy the the file to the advice directory just as the AdviceClient.
2. Window users:
a. To compile, do this

C:\>cd projects\advice

C:\projects\advice>javac -classpath ./classes;C:\j2sdkee1.3.1\lib\j2ee.jar;Advic
eAppClient.jar;. GuiClient.java

b. To run the application, do this

Start the j2ee -verbose

C:\projects\advice>java -classpath ./classes;C:\j2sdkee1.3.1\lib\j2ee.jar;Advic
eAppClient.jar;. GuiClient
NOTE for windows users:
Change the getAdice to the method that returns the advice from your bean.
some jvm are known to have bugs, check posts at ranch for solution or post
the problems and assure your what you already know.

3.Linux users:
a. To compile:

[mimoh@localhost advice]$ javac -classpath ./classes:AdviceAppClient.jar:/home/mimoh/j2sdkee1.3.1/lib/j2ee.jar: GuiClient.java

b. To Run:
[mimoh@localhost advice]$ java -classpath ./classes:AdviceAppClient.jar:/home/mimoh/j2sdkee1.3.1/lib/j2ee.jar: GuiClient


4. A GUI should appear left corner of your screen.
Click the Get Advice button and monitor the response of the server.
Try shutting down the server and run the app, what you see is what you expect on exception
part of the exams
**/

//import EJB packages
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;
import javax.ejb.*;

// import headfirst packages
import headfirst.*;

// Swing and awt packages for constructing the GUI
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GuiClient extends JFrame {

// getAdviceButton triggers the action that call AdviceBean messages
private JButton getAdviceButton;
// panel to display both the getAdvicButton
private JPanel panel;
// String to hold the message from adviceBean
private String message;

// Constructor builds the GUI, not the best place to do this.
// Please don't ask me why ? Just think of exception handling
public GuiClient() {

//passing the title of the GUI to the JFrame constructor
super("Advice Client - GUI Version");
// setting the layout manager.
getContentPane().setLayout( new FlowLayout() );
// making the JPanel object
panel = new JPanel();
// creating the JButton object
getAdviceButton = new JButton("Get Advice");
//adding the getAdviceButton to the panel
panel.add( getAdviceButton);
//adding the panel to the buttom of the BorderLayout
getContentPane().add(panel);
// Instantiating our event handler inner class
AdviceHandler handler = new AdviceHandler();
//adding the event listener to the getAdviceButton
getAdviceButton.addActionListener( handler);
}// end constructor

// method that does all the EJB lookup and so on. (like the AdviceClient)
// but it return a String that represent the message from the bean
public String getAdvice () {

try {
Context ic = new InitialContext();
Object o = ic.lookup("Advisor");
AdviceHome home = (AdviceHome) PortableRemoteObject.narrow(o, AdviceHome.class);
Advice advisor = home.create();
message = advisor.getMessage();

} catch(CommunicationException ex) {

JOptionPane.showMessageDialog(null,ex.getMessage(),"Message From Advice Bean",JOptionPane.ERROR_MESSAGE);
System.exit(1);
} catch(NamingException ex) {

JOptionPane.showMessageDialog(null, ex.getMessage(),"Message From Advice Bean",JOptionPane.ERROR_MESSAGE);
System.exit(1);
} catch(EJBException ex) {

JOptionPane.showMessageDialog(null, ex.getMessage(),"Message From Advice Bean",JOptionPane.ERROR_MESSAGE);
System.exit(1);
} catch(CreateException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(),"Message From Advice Bean",JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
catch(RemoteException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(),"Message From Advice Bean",JOptionPane.ERROR_MESSAGE);
System.exit(1);

}
return message;
}// end getAdvice
//listening for events from the getAdviceButton
private class AdviceHandler implements ActionListener {
//handling the event
public void actionPerformed( ActionEvent event) {

try {
// Handling changed event
// a JOptionPane to display the message
JOptionPane.showMessageDialog(null, getAdvice(),"Message From Advice Bean",JOptionPane.PLAIN_MESSAGE);
} catch(Exception ex) {

ex.printStackTrace();
}
}// end actionPerformed
}// end AdviceHandler

// main
public static void main(String[] args) {
//instantiating our application
GuiClient gui = new GuiClient();
//setting the window close operation
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setting the default size of the window
gui.setSize(300,100);
//showing the window
gui.setVisible(true);


} // end main


}// end GuiClient
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic