Creating the User Interface for the Printer Management Application


Creating the User Interface for the Printer Management Application

You can use the PrintFile.java file to create a user interface that has a menu and a tabbed pane. You can click the Print Text tab from the tabbed pane to open and print a text document. To open and print an image, click the Print Image tab from the tabbed pane. The user interface provides a set of menu items to open a document, specify the page setup, and print that document.

Listing 5-1 shows the contents of the PrintFile.java file:

Listing 5-1: The PrintFile.java File
start example
 /* Imports required swing classes. */ import javax.swing.ImageIcon; import javax.swing.JFileChooser; import javax.swing.JLabel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTabbedPane; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JLabel; import javax.swing.JFrame; import javax.swing.JMenuItem; import javax.swing.UIManager; /* Imports required io classes. */ import java.io.File; import java.io.FileInputStream; /* Imports required nio classes. */ import java.nio.ByteBuffer; import java.nio.channels.FileChannel; /* Imports required awt classes. */ import java.awt.Container; import java.awt.BorderLayout; 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; /* class PrintFile - This class is the main class of the application. This class creates the user interface to open and print a text document or an image. Methods: actionPerformed() - This method is invoked when an end user clicks any button. main() - This method creates the main window of the application and displays it. */ class PrintFile extends JFrame implements ActionListener, Runnable  { /* Declares object of JTabbedPane class. */  JTabbedPane tabbedpane; /* Declares object of Container class. */  Container container; /* Declares object of JMenuBar class. */  JMenuBar menubar; /* Declares object of JMenu class. */  JMenu file_menu;  JMenu print_menuitem; /* Declares object of JMenuItem class. */ JMenuItem setup_menuitem, exit_menuitem, open_menuitem,prin_default,prin_custom; /* Declares object of JLabel class. */  JLabel label; /* Declares objects of JScrollPane class. */  JScrollPane image_scrollpane;  JScrollPane text_scrollpane; /* Declares object of JTextArea class. */  JTextArea textarea; /* Declares objects of JFileChooser class. */  JFileChooser textfilechooser, imgfilechooser; /* Declares object of CreateBookInterface class. */  CreateBookInterface bIn;  Thread thread1, thread2;  int count = 0; /* Implements the constructor of PrintFile class. */  public PrintFile(String title)  {  super(title); /*  Sets the look and feel of the application to windows. */  try  { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");  }  catch(Exception e)  { System.out.println("Unknown Look and Feel." + e);   } /*Sets the size of user interface. */  setSize(600,400); /* Sets the resizable property of Jframe to false. */  setResizable(false);  bIn = new CreateBookInterface(); /* Adds window listener to the JFrame. */  addWindowListener(new WindowAdapter()  { public void windowClosing(WindowEvent we)  {  System.exit(0);  }  });  container = getContentPane(); /*Sets the layout of the container to BorderLayout. */  container.setLayout(new BorderLayout()); /* Creates objects of JFileChooser class. */  textfilechooser = new JFileChooser();  imgfilechooser = new JFileChooser(); /*  Creates and sets the new file filter for selecting the text files. */ textfilechooser.setFileFilter(new javax.swing.filechooser.FileFilter()  {  public boolean accept(File f)  {  if(f.isDirectory())  {  return true;  }  String name=f.getName(); if ((name.endsWith(".jpg")) (name.endsWith(".gif"))  (name.endsWith(".GIF"))  (name.endsWith(".JPG"))  (name.endsWith(".tif"))  (name.endsWith(".TIF"))  (name.endsWith(".bmp")) (name.endsWith(".BMP")))  {  return false;  }  return true;  }  public String getDescription(){  return "Text Files";  }  }); /*  Creates and set the new file filter for selecting the image files. */ imgfilechooser.setFileFilter(new javax.swing.filechooser.FileFilter ()   { /*  This method lets the end user select only JPG and GIF files from the file chooser.  */  public boolean accept(File f)   {  if (f.isDirectory())   {  return true;  }  String name = f.getName(); if (name.endsWith(".jpg")  name.endsWith(".gif")  name.endsWith(".GIF")  name.endsWith(".JPG"))   {  return true;  }  return false;  }  public String getDescription() {  return ".jpg, .gif";  }  }); /*  Initializes the menu bar. Sets menu bar to the frame.  */  menubar = new JMenuBar();  setJMenuBar(menubar); /* Initializes the menu. Adds menus to the menu bar. */  file_menu = new JMenu("File");  print_menuitem = new JMenu("Print");  menubar.add(file_menu); /* Initializes the menu items and adds the menu item to the particular menu. */  open_menuitem = new JMenuItem("Open"); setup_menuitem = new JMenuItem("Page Setup & Print");  exit_menuitem = new JMenuItem("Exit"); prin_default=new JMenuItem("Print Default"); prin_custom=new JMenuItem("Print Custom");  file_menu.add(open_menuitem);  file_menu.add(setup_menuitem);  print_menuitem.add(prin_default);  print_menuitem.add(prin_custom);  file_menu.add(print_menuitem);  file_menu.add(exit_menuitem); /*Adds the action listener with each menu item.*/  open_menuitem.addActionListener(this);  setup_menuitem.addActionListener(this);  prin_default.addActionListener(this);  prin_custom.addActionListener(this);  exit_menuitem.addActionListener(this); /* Creates an object of JTabbedPane class.*/  tabbedpane = new JTabbedPane(); /* Creates a new JScrollPane for the text file.*/ text_scrollpane = new JScrollPane(textarea = new JTextArea()); /* Sets the text wrap style of the JTextArea class.*/  textarea.setLineWrap(true);  textarea.setWrapStyleWord(true); /* Adds the JScrollPane to JTabbedPane.*/ tabbedpane.add("Print Text", text_scrollpane ); /* Creates an object of JScrollPane class for the image file.*/ image_scrollpane = new JScrollPane(label =new JLabel()); /* Adds the JScrollPane to JTabbedPane*/ tabbedpane.add("Print Image", image_scrollpane); /* Adds the JTabbedPane to container.*/ container.add(tabbedpane, BorderLayout.CENTER);  setVisible(true);  } /* actionPerformed() - This method is called when the end user clicks any button. Parameters: ae - an ActionEvent object containing the details of the event. Return Value: NA */  public void actionPerformed(ActionEvent ae)  { /*  This section is executed when the end user selects the File-> Page Setup & Print menu item. */  if (ae.getSource()==setup_menuitem)  {  count =1;  thread1 = new Thread(this);  thread1.start();  } /*  This section is executed when the end user selects the File -> Print Default menu item. */  if (ae.getSource()==prin_default)  {  count =2;  thread2 = new Thread(this);  thread2.start();  } /*  This section is executed when the end user selects the File -> Print Custom menu item. */  if(ae.getSource()==prin_custom)  {  if (tabbedpane.getSelectedIndex()==0)  bIn.setComponent(textarea);  else  bIn.setComponent(label);  bIn.setVisible(true);  } /*  This section is executed when the end user selects the File->Exit menu item. */  if (ae.getSource()==exit_menuitem)  { /* Terminates the application. */  System.exit(0);  } /*  This section is executed when the end user selects the File -> Open menu item. */  if (ae.getSource()==open_menuitem)  { /* Checks whether the text's tab is selected in the tabbed pane. */  if (tabbedpane.getSelectedIndex()==0)  { int returnVal = textfilechooser.showOpenDialog(this); if(returnVal == JFileChooser.APPROVE_OPTION)   {  try  { /*  Reads the contents of opened file and sets the contents of the opened file in the text area. */ FileInputStream inputStream = new FileInputStream(textfilechooser.getSelectedFile().getAbsolutePath()); FileChannel fileIn=inputStream.getChannel(); long fsize=fileIn.size(); ByteBuffer buffin=ByteBuffer.allocate((int)fsize); fileIn.read(buffin); buffin.rewind(); String readLine=new String(buffin.array()); textarea.setText(readLine); fileIn.close(); inputStream.close();  }  catch(Exception ex)  {} bIn=new CreateBookInterface();  }  } /* Checks whether the image's tab is selected in the tabbed pane. */ else if (tabbedpane.getSelectedIndex()==1)  { int returnVal = imgfilechooser.showOpenDialog(this); if(returnVal == JFileChooser.APPROVE_OPTION)   {  try  { label.setIcon(new ImageIcon(imgfilechooser.getSelectedFile().getAbsolutePath()));  }  catch(Exception ex)  {}  }  }  }  } public void run()  {  if(count == 1)  {  if (tabbedpane.getSelectedIndex()==0) new PrintComp(textarea).pageSetupAndPrint();  else new PrintComp(label).pageSetupAndPrint();  }  else if(count == 2)  { if (tabbedpane.getSelectedIndex()==0)  PrintComp.printComponent(textarea);  else  PrintComp.printComponent(label);  this.setVisible(true);  }  } /* main() - This method creates the main window of the user interface and displays it. Parameters: args[] - Contains any command line arguments passed. Return Value: NA */  public static void main(String[] args)   { PrintFile frame = new PrintFile("Printer Management Application");  } } 
end example
 

Download this Listing .

In the above code, the main() method creates an instance of the PrintFile.java class. This class generates the main window of the Printer Management application, as shown in Figure 5-2:

click to expand: this figure shows the user interface of the printer management application. it contains a file menu and a jtabbedpane.
Figure 5-2: The Printer Management Application User Interface

When the end user selects the File menu, the options associated with it appear, as shown in Figure 5-3:

click to expand: this figure shows the options that are available in the file menu of the printer management application.
Figure 5-3: The File Menu of the Printer Management Application

To open a file, end users need to select the File-> Open menu option. The Open dialog box appears. When an end user selects the file and clicks the Open button of Open dialog box, the content of the selected file is displayed in the text area.

If end users select the File -> Page Setup & Print menu option, the actionPerformed() method sets the count value to 1 and calls the start() method of the Thread class to start the thread, thread1.

If the File->Print->Print Default menu option is selected, the actionPerformed() method sets the count value to 2 and calls the start() method of the Thread class to start the thread, thread2.

When the end user selects the File->Print->Print Custom menu option, the actionPerformed() method calls the setComponent() method of the CreateBookInterface class and opens a Book Interface dialog box.

The application terminates if the end user selects the File->Exit menu option.

The methods defined in the above listing are:

  • actionPerformed() : Acts as an event listener and activates an appropriate method, based on the menu option the end user selects. This method is invoked when an end user selects any option from the File menu.

  • run() : Checks the count value. If the count value is 1, this method creates an instance of the PrintComp class and calls the pageSetupAndPrint() method of the PrintComp class. If the count value is 2, the run() method creates an instance of PrintComp class.




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