If supplied me the code I would be grate you of it .
Thanks
Palmis
Okay, here is the code. Remember, it was a beginner programming project of a
friend of mine; he had no real experience in programming in any language,
let alone Java. The style is not very professional and the program is not
complete. But I think you will find that it can select and display a file
well enough; perhaps you can use it as a starting point for your own
program.
The program has one minor dependency, the icon that appears to represent
"Open". You can find this file, whose name is Open24.gif, on this page:
http://java.sun.com/developer/techDocs/hi/repository/TBG_General.html. It is
the larger of the two Open icons. Just save it to your computer and put it
in a package named "images" below your source code. For example, if you put
the source code in a package called viewer02, put Open24.gif in a package
called viewer02.images.
===============================================================
/*
* Created on May 25, 2003
*
*/
package viewer02;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
public class Fileviewer03 extends JFrame implements ActionListener {
/* Setup the classes and declarations of variables that will have a class
wide
* scope.
*/
final String CLASS_NAME = getClass().getName(); // Used for error messages
in the displayFile method.
JFrame frame; // main content pane
JTextArea textArea;
/* Added Font on May 29, 2003 to allow the font that is used to
* display the file to be changed. In Future it may be set in
* the preferences.
*/
Font textFont; // Font used in the textArea
JScrollPane scrollPane;
/* file - the name of the variable used to pass the filename
* from the pickFile method to the displayFile method
*/
File file=null; //file name variable
/* File pathName Added May 27, 2003 this variable is used to
* Store the path of the last file. So that the filechooser
* opens in the same directory, rather than the default directory
*/
File pathName=null; //file path variable
String fileName = new String();
/* Variables used to set up the information that is passed
* to the ActionListeners when a Menu Item is Pressed.
*/
static final String OpenText = "Open";
static final String PreferencesText = "Preferences";
static final String ExitText = "Exit";
static final String HelpText = "Help";
static final String ProdInfoText = "About";
/* Setup Menu Bar and Items
*
*/
JMenuBar menuBar;
JMenuItem OpenMenuItem = new JMenuItem(OpenText);
JMenuItem PreferencesMenuItem = new JMenuItem(PreferencesText);
JMenuItem ExitMenuItem = new JMenuItem(ExitText);
JMenuItem HelpMenuItem = new JMenuItem(HelpText);
JMenuItem ProdInfoMenuItem = new JMenuItem(ProdInfoText);
public static void main(String[] args) {
Fileviewer03 myProg = new Fileviewer03();
myProg.pack();
myProg.setLocationRelativeTo(null);
myProg.setVisible(true);
} // end main()
public Fileviewer03 () {
super("Fileviewer"); // Give the Frame a name
/* TODO: Set up the Name so that it displays the file Name
*
*/
/* Setup the Layout in the GUI
* Starting with the look and feel and then
* Setting up the Jframe for everything else to go into.
*/
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("FrameDemo");
// Setup - What happens when the frame closes?
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.setLocationRelativeTo(null) ; // should place the window in the
center of the screen if using 1.4.1
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
System.out.println(dim);
frame.setLocation(dim.width / 2 - frame.getSize().width / 2,
dim.height / 2 - frame.getSize().height / 2);
/* Get the open icon. */
String openIconFileName = "images/Open24.gif";
URL openIconUrl = this.getClass().getResource(openIconFileName);
ImageIcon openIcon = new ImageIcon(openIconUrl);
/* Add toolbar. */
JToolBar toolBar = new JToolBar();
toolBar.setFloatable(false);
/* Create open button. */
JButton openButton = new JButton(openIcon);
openButton.setToolTipText("Use this button to open a new file.");
openButton.addActionListener(this);
openButton.setActionCommand(OpenText);
toolBar.add(openButton);
/* Put toolbar on GUI. */
getContentPane().add(toolBar, BorderLayout.NORTH);
textFont = new Font("Courier", Font.PLAIN, 14);
/* Setup a textarea to display the file in. Make it read only,
* with a border on the sides and wrap the lines on the space
* between words. The background is White and the text is
* Black. Also set the font.
*/
textArea = new JTextArea(35, 60);
textArea.setEnabled(false);
textArea.setBorder(BorderFactory.createEmptyBorder(0,10,0,10));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setBackground(Color.white);
textArea.setDisabledTextColor(Color.black);
textArea.setFont(textFont);
/* Put the textarea into a scrollpane so that the whole file
* can be viewed. Added May 29, 2003 the vertical Scroll bar
* is always on.
*/
scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);/* Put the JScrollPane and the JTextArea into the Jframe */ getContentPane().add(scrollPane,BorderLayout.CENTER);/* Setup a menu bar. */ JMenuBar menuBar = new JMenuBar(); //Create the Menu Bar and instanciate it setJMenuBar(menuBar); // Associate the menubar with the frame JMenu FileMenu = new JMenu("File"); // create a main menu item called"File" menuBar.add(FileMenu); // add the File menu to the menu bar FileMenu.add(OpenMenuItem); // add the item to the File menu FileMenu.add(PreferencesMenuItem); // add the item to the File menu FileMenu.add(ExitMenuItem); // add the item to the File menu JMenu HelpMenu = new JMenu("Help"); // create a main menu item called"Help" menuBar.add(HelpMenu); // add the Help menu to the menu bar HelpMenu.add(HelpMenuItem); // add the item to the Help menu HelpMenu.add(ProdInfoMenuItem); // add the item to the Help menu/* Set action commands for the menu items. */ OpenMenuItem.setActionCommand(OpenText); PreferencesMenuItem.setActionCommand(PreferencesText); ExitMenuItem.setActionCommand(ExitText); HelpMenuItem.setActionCommand(HelpText); ProdInfoMenuItem.setActionCommand(ProdInfoText);/* Add listeners for the menu items. */ OpenMenuItem.addActionListener(this); ExitMenuItem.addActionListener(this); PreferencesMenuItem.addActionListener(this); HelpMenuItem.addActionListener(this); ProdInfoMenuItem.addActionListener(this);/* Setup a button at the bottom of the Jframe * that brings up the file chooser */// JButton OKButton = new JButton("Select A New File");// OKButton.setActionCommand(OpenText);// OKButton.addActionListener(this);// getContentPane().add(OKButton, BorderLayout.SOUTH);} // end constructor/method Filechooserpublic File pickFile() {// Create a JFileChooser JFileChooser filetree = new JFileChooser(pathName); filetree.setFileHidingEnabled(false); int returnVal = filetree.showOpenDialog(null);// Determine which file the user selected or close the application if userpressed cancel. if (returnVal == JFileChooser.APPROVE_OPTION) { textArea.setText(""); // Clears the textarea file = filetree.getSelectedFile(); fileName = file.getName(); pathName = file.getParentFile(); setTitle(pathName + File.separator + fileName);// System.out.println(file); // only used to during testing// System.out.println(fileName);// System.out.println(pathName); return file; } else { return null; }} //end method pickFile()public void displayFile(File myFile) { String METHOD_NAME = "displayFile()"; String strLine = null; BufferedReader viewfile = null;// Put the file into the textarea. try { viewfile = new BufferedReader(new FileReader(myFile)); while ((strLine = viewfile.readLine()) != null) { textArea.append(strLine+"\n"); //added "\n", which causes line wrap textArea.setCaretPosition(0); // Sets the position of the curser to thetop of the file textArea.grabFocus(); // Gives the focus to the textarea so that themovement arrows work. } // end while viewfile.close(); // close file } // end try catch(FileNotFoundException IO_excp) { System.out.println(CLASS_NAME + "." + METHOD_NAME + " - The file, " +viewfile + ", could not be found. Error: " + IO_excp); System.exit(1); } catch(IOException IO_excp) { System.out.println(CLASS_NAME + "." + METHOD_NAME + " - I/O Error readingthe file, " + viewfile + ". Error: " + IO_excp); System.exit(1); } return;} //End method displayFile()public void actionPerformed (ActionEvent evt){ String command = evt.getActionCommand(); if (command.equals(ExitText)) { System.exit(0); } // end if command = ExitText else if (command.equals(OpenText)){ file = pickFile();// System.out.println ("Open was pressed"); // used for testing// System.out.println ("File returned was " + file); displayFile(file); } //end if command = OpenText else if (command.equals(PreferencesText)){ System.out.println ("Preferences was pressed"); //SetPreferences(); } //end if command = PreferencesText else if (command.equals(HelpText)){ System.out.println ("Help was pressed"); //new Help(); } //end if command = PreferencesText else if (command.equals(ProdInfoText)){ System.out.println ("About was pressed"); //new About(); } //end if command = ProdInfoText} //end method ActionListeners} //End Class Fileviewer03===============================================================Rhino