The MDI Class
This class is used to create the main window of our application.
Listing
16.2 MDI.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.rmi.*;
//Main form MDI
public class MDI extends JFrame{
//{{DECLARE CONTROLS}}//
Container cp=null;
JDesktopPane dp=null;
public static MDI mdi=null;
private static DatabaseInterface server;
//Constructor
public MDI(){
super("Student Information System");
//get a content pane object.
cp = getContentPane();
//Create a new JDesktopPane object.
//A JDesktopPane object is used
//to create a multiple-document
//interface or a virtual desktop.
dp = new JDesktopPane();
//add the JDesktopPane object
//to the content pane.
cp.add(dp);
//set the background color
//of the JDesktopPane object.
dp.setBackground(cp.getBackground());
//Set the operation that will
//happen by default when
//the user initiates a "close"
//on this frame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setup();
}
//method used to get a reference to the remote object.
private void setup(){
//method used to create various components
createMenuComponents();
//method used to set the size of the MDI form
setMDISize();
//Make the Window visible.
show();
try{
//Get a reference to the remote object
//associated with the specified name.
server = (DatabaseInterface)Naming.lookup("DatabaseServer");
}catch(Exception ex){
JOptionPane.showMessageDialog(mdi,ex.getMessage()
,"ERROR",JOptionPane.ERROR_MESSAGE);
}
}
//This method returns a reference to the remote object
public static DatabaseInterface getServerObject(){
return server;
}
//Method used to create menu bar, menus, and menu items.
private void createMenuComponents(){
//create a menu bar object.
JMenuBar mBar = new JMenuBar();
//create a menu object
JMenu mnuForms = SwingComp.createMenu(mBar,"Forms");
//create an object to handle action events
MenuHandler mHandler = new MenuHandler();
//create a menu item object
JMenuItem
itmStudent =
SwingComp.createMenuItems(mnuForms,mHandler,"Student");
//Set the menubar for this frame.
this.setJMenuBar(mBar);
}
//method used to the set size of MDI
private void setMDISize(){
//get the default toolkit
//to interact with operating system
Toolkit tk=Toolkit.getDefaultToolkit();
//Get the size of the screen.
Dimension d = tk.getScreenSize();
int screenHeight = d.height;
int screenWidth = d.width;
//set the size of window
this.setSize(screenWidth,screenHeight);
}
//main method
static public void main(String args[]){
mdi= new MDI();
}
//{{Listener class
private class MenuHandler
implements ActionListener{
//Invoked when an action occurs.
public void actionPerformed(ActionEvent ae){
//create a StudentForm object.
StudentForm sf = new StudentForm();
//method used to show an internal frame
showForms(sf);
}
}
//Method used to show an internal frame
private void showForms(JInternalFrame jif){
dp.add(jif,JLayeredPane.DEFAULT_LAYER);
try{
//show the internal frame window
jif.show();
//Select the JInternalFrame.
jif.setSelected(true);
}catch(java.beans.PropertyVetoException ex){
JOptionPane.showMessageDialog(
mdi,ex.getLocalizedMessage());
}
}
}
|