how to open .drw or .prt file using swing?
Hi Everyone,
I am new to java programming n I m facing a problem in swing.I want to open a perticular drawing file using Java program but the condition is,it should open in the same window.Now,I have managed to open normal txt files n .doc files in the same window using JFileChooser and IO programming.But I dont know what it takes to open a .prt or a .drw file using the same program.
Can you guys plz help me out in this?I think,it is not compatible to open such files...but how to make it compatible then?
I m posting the code here...plz check it n help me out as soon as possible...Thank you very much......
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class FileChooseApp1 extends JFrame
implements ActionListener
{
JMenuItem fMenuOpen = null;
JTextArea fTextArea;
File fFile = new File ("default.java");
FileChooseApp1 (String title) {
super (title);
Container cp = getContentPane ();
// Create a user interface.
cp.setLayout ( new BorderLayout () );
fTextArea = new JTextArea ("");
cp.add ( fTextArea, "Center");
JMenu m = new JMenu ("File");
m.add (fMenuOpen = makeMenuItem ("Open"));
JMenuBar mb = new JMenuBar ();
mb.add (m);
setJMenuBar (mb);
setSize (400,400);
}
public void actionPerformed ( ActionEvent e ){
boolean status = false;
String command = e.getActionCommand ();
if (command.equals ("Open")) {
// Open a file
status = openFile ();
}
}
private JMenuItem makeMenuItem (String name) {
JMenuItem m = new JMenuItem (name);
m.addActionListener (this);
return m;
}
boolean openFile () {
JFileChooser fc = new JFileChooser ();
fc.setDialogTitle ("Open File");
// Choose only files, not directories
fc.setFileSelectionMode ( JFileChooser.FILES_ONLY);
// Start in current directory
fc.setCurrentDirectory (new File ("."));
// Set filter for Java source files.
// fc.setFileFilter (fJavaFilter);
// Now open chooser
int result = fc.showOpenDialog (this);
if (result == JFileChooser.CANCEL_OPTION) {
return true;
} else if (result == JFileChooser.APPROVE_OPTION) {
fFile = fc.getSelectedFile ();
// Invoke the readFile method in this class
String file_string = readFile (fFile);
if (file_string != null)
fTextArea.setText (file_string);
else
return false;
} else {
return false;
}
return true;
} // openFile
public String readFile (File file) {
StringBuffer fileBuffer;
String fileString=null;
String line;
try {
FileReader in = new FileReader (file);
BufferedReader dis = new BufferedReader (in);
fileBuffer = new StringBuffer () ;
while ((line = dis.readLine ()) != null) {
fileBuffer.append (line + "\n");
}
in.close ();
fileString = fileBuffer.toString ();
}
catch (IOException e ) {
return null;
}
return fileString;
} // readFile
public static void main (String [] args) {
// Can pass frame title in command line arguments
String title="Frame Test";
if (args.length != 0) title = args[0];
FileChooseApp1 f = new FileChooseApp1 (title);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setVisible (true);
} // main
}

