Creating the Book Interface


The CreateBookInterface.java file helps you to create a book interface for the document that is to be printed.

Listing 5-3 shows the contents of the CreateBookInterface.java file:

Listing 5-3: The CreateBookInterface.java File
start example
 /* Imports java.awt package classes. */ import java.awt.Graphics; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.WindowEvent; import java.awt.event.ActionListener; import java.awt.event.WindowListener; import java.awt.event.WindowAdapter; /* Imports awt.print package classes. */ import java.awt.print.Book; import java.awt.print.PrinterJob; import java.awt.print.PageFormat; import java.awt.print.Printable; import java.awt.print.PrinterException; /* Imports javax.swing package classes. */ import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JCheckBox; import javax.swing.JTextField; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.DefaultListModel; import javax.swing.ButtonGroup; /* Imports java.util package classes. */ import java.util.Enumeration; import java.util.Vector; /* class  CreateBookInterface - This class is the subclass of the application. This class creates a book for the document to be printed. Methods: actionPerformed() - This method is invoked when an end user clicks any button. setComponent() - This method sets the component to the currently opened document. createBook() - This method is called when the end user clicks the Print Custom menu option. addPagesInBook()- This method is called when the end user clicks the Add Pages In Book button. printBook()- This method is called when the end user clicks the Print button. */ public class CreateBookInterface extends JFrame implements ActionListener { /* Declares object of JComponent class. */  JComponent component; /* Declares object of Book class. */  Book book = null; /* Declares object of DefaultListModel class. */  DefaultListModel dlm; /* Declares object of PrinterJob class. */  PrinterJob printJob = null; /* Declares object of ButtonGroup class.*/  ButtonGroup bg; /* Declares object of JCheckBox class.*/  JCheckBox lscape = null;  JCheckBox potrait=null; /* Declares object of JTextField class. */  JTextField pageNo = null; /* Declares object of JList class. */  JList bookItemList = null; /* Declares object of JButton class. */  JButton add, print, reset;  String format;  int pageNum; /* Declares object of BookPrintable class. */  BookPrintable bookPrintable; /* Implements the constructor of the CreateBookInterface class. */  CreateBookInterface()  { /*Sets the title of the CreateBookInterface class. */  setTitle("Book Interface"); /* Sets the resizable property to false. */  setResizable(false); /* Adds the window closing event to the CreateBookInterface class. */  addWindowListener(new WindowAdapter()  { public void windowClosing(WindowEvent we)  {  setVisible(false);   }  }); /* Sets the size of the CreateBookInterface class. */  setSize(400,250);  setLocation(50,50); /* Initializes the object of the BookPrintable class. */  bookPrintable = new BookPrintable(); /* Creates a new ButtonGroup object. */  bg = new ButtonGroup(); /* Creates a new object of the DefaultListModel class. */  dlm = new DefaultListModel(); /* Creates a new object of the JPanel class. */  JPanel selectPanel = new JPanel(); /* Sets the layout of the Jpanel class. */ selectPanel.setLayout(new BorderLayout()); /* Creates a new object of the JPanel class. */  JPanel formatPanel = new JPanel(); /* Sets the layout of the Jpanel class. */ formatPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); /* Adds a new JLabel to the panel. */ formatPanel.add(new JLabel("Page Format : ")); /* Creates new JCheckBox objects. */  lscape=new JCheckBox("Landscape",true);  potrait=new JCheckBox("Potrait",false); /* Adds the JCheckBox objects to ButtonGroup. */  bg.add(lscape);  bg.add(potrait); /* Adds the objects of JCheckBox to the JPanel, formatPanel. */  formatPanel.add(lscape);  formatPanel.add(potrait); /* Adds formatPanel to the panel. */  selectPanel.add("North", formatPanel); /* Creates a new object of the JPanel class. */  JPanel pageNoPanel = new JPanel(); /* Sets the layout of the JPanel class. */ pageNoPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); /* Adds a new JLabel to the JPanel,pageNoPanel. */ pageNoPanel.add(new JLabel("Number of Pages : ")); /* Creates a new object of the JTextField class. */  pageNo = new JTextField(5); /* Adds JTextBox to the JPanel,pageNoPanel. */  pageNoPanel.add(pageNo); /* Adds the pageNoPanel to the selectPanel panel. */  selectPanel.add("Center", pageNoPanel);  add = new JButton("Add in the Book"); /* Adds the action listener to the add button. */  add.addActionListener(this); /* Adds the add button to the selectPanel panel. */  selectPanel.add("South", add); getContentPane().add("North", selectPanel); /* Creates an object of the JList class. */  bookItemList = new JList(dlm); /* Sets the background color of the JList object to gray. */  bookItemList.setBackground(Color.gray); getContentPane().add("Center", new JScrollPane(bookItemList)); /* Creates a new JPanel, ctrlPanel. */  JPanel ctrlPanel = new JPanel(); /*  Sets the layout of the ctrlPanel JPanel to GridLayout. */  ctrlPanel.setLayout(new GridLayout(1,2));  print = new JButton("Print");  print.setEnabled(false); /* Adds action listener to the print JButton. */  print.addActionListener(this);  ctrlPanel.add(print);   reset = new JButton("Reset");  reset.setEnabled(false); /* Adds action listener to the reset JButton. */  reset.addActionListener(this);  ctrlPanel.add(reset);   getContentPane().add("South", ctrlPanel); /* Invokes the createBook() method*/  createBook();  setVisible(false);  } /* actionPerformed() - This method is called when the end user clicks any button. Parameters: ae - An ActionEvent object containing details of the event. Return Value: NA */  public void actionPerformed(ActionEvent ae)  { /*  This section is executed when the end user clicks the Add in the Book button. */ if(ae.getActionCommand().equals("Add in the Book"))  { /*  Gets all the elements added to the ButtonGroup, bg in an object of the Enumeration class. */  Enumeration enum=bg.getElements(); /*  The while loop runs until there is any element in the enumeration. */  while(enum.hasMoreElements())  { /*  Retrieves the next element of the enumeration in an object of the JCheckBox class. */ JCheckBox button=(JCheckBox)enum.nextElement(); /* Checks if the check box is selected.*/  if (button.isSelected())  { /* Gets the text of the check box in a string.*/ format=button.getText();  }  }  try  { pageNum = Integer.parseInt(pageNo.getText().trim());  }  catch (Exception ex)  { System.out.println("Error : Invalid entry of page number.");  return;  }  print.setEnabled(true); /* Adds the currently open document to the book.*/  addPagesInBook(format, pageNum);  } /*  This section is executed when the end user clicks the Print button. */ else if(ae.getActionCommand().equals("Print"))  {  reset.setEnabled(true);  print.setEnabled(false);  setComponent(null);  /* Invokes the printBook() method.*/  printBook();  } /*  This section is executed when the end user clicks the Reset button. */ else if(ae.getActionCommand().equals("Reset"))  {  print.setEnabled(true);  reset.setEnabled(false); /* Clears the JList.*/  dlm.clear(); /* Clears the book. */  book = null; /* Invokes the createBook() method*/  createBook();  }  } /* setComponent() - This method sets the value of the object of the JComponent class. Parameters: component - An object of the JComponent class. Return Value: NA */  void setComponent(JComponent component)  {  this.component= component;  } /* createBook() - This method creates a book to add the document to be printed. Parameters: NA Return Value: NA */  void createBook()  {  book = new Book();  printJob = PrinterJob.getPrinterJob();  } /* createBook() - This method is called when the end user clicks the Add in the Book button. Parameters:  format- A string that represents the format in which the document to be printed is added to the book. pageNumber- An integer representing the number of pages to be added to the book. Return Value: NA */ void addPagesInBook(String format, int pageNumber)  { /* Creates an object of PageFormat class. */ PageFormat pageFormat = printJob.defaultPage(); /* Checks if the Potrait check box is checked. */  if(format.equals("Potrait"))  { pageFormat.setOrientation (PageFormat.PORTRAIT);  } /* Checks if the Landscape check box is checked. */  else if(format.equals("Landscape"))  { pageFormat.setOrientation (PageFormat.LANDSCAPE);   } /* Appends the document to be printed to the book. */ book.append(bookPrintable,pageFormat, pageNumber); String temporarySt=new String("Book Item Added : Page Format : "+format+" : "+" Number of Pages : "+pageNumber); /*  Adds the details of the document to be printed to a JList. */  dlm.addElement(temporarySt);  } /* printBook() - This method is called when the end user clicks the Print button. Parameters: NA Return Value: NA */  void printBook()  {  printJob.setPageable(book);  try   { /* Calls the print() method to print a book. */  printJob.print();  }  catch(PrinterException pe)   { System.out.println("Error in printing !!! " + pe);  }  } /* class BookPrintable - This class is the subclass of the application. This class implements the Printable interface and creates a component to be printed. Methods: print()-This method is called when the end user prints a document. */  class BookPrintable implements Printable   {  /* print() - This method defines the Printable interface. Parameters:  g - Represents the object of the Graphics class. pf - Represents the object of the PageFormat class. index - Represents an index of a page. Return Value: int PAGE_EXIST */  public int print(Graphics g, PageFormat pf, int pageIndex)   { /* Implements print code here. */  return Printable.PAGE_EXISTS;  }  } } 
end example
 

Download this Listing .

In the above code, the CreateBookInterface class allows you to create a user interface to create and print a book, as shown in Figure 5-4:

click to expand: this figure shows the book interface dialog box for the printer management application. the dialog box displays a set of check boxes, labels, text fields, lists, and buttons.
Figure 5-4: The Book Interface Window

The methods defined in the above code are:

  • actionPerformed() : Acts as an event listener and activates an appropriate method, based on the button the end user clicks in the Book Interface dialog box. If the end user clicks the Add in the Book button, the actionPerformed() method calls the addPagesInBook() method. If the Print button is clicked, the actionPerformed() method calls the printBook() method. When the end user clicks the Reset button, the actionPerformed() method calls the clear() method of the JComponent class and then calls the createBook() method of Book class.

  • createBook() : Initializes the object of the Book class and gets the object of the PrinterJob class using the getPrinterJob() method.

  • addPagesInBook() : Uses format and paneNumber as input parameters. The addPagesInBook() method creates an object of the PageFormat class. This method then checks the page format style and sets the appropriate page orientation. Next, the addPagesInBook() method appends to the book the document that is to be printed. Finally, the addPageInBook() method adds the elements to the list box.

  • printBook() : Calls the setPageable() method of the PrinterJob class to set the page. The printBook() method calls the print() method to print the file.

The CreateBookInterface class contains a sub class called BookPrintable, which implements the Printable interface of the book. The BookPrintable class calls the print() method to print the file. You can define the print() method to print a customized book.




Java InstantCode. Developing Applications Using Java NIO
Java InstantCode. Developing Applications Using Java NIO
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 55

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net