Solved

Displaying file content.

Posted on 2004-08-02
6
277 Views
Last Modified: 2010-08-05
Hi ,

I am very new to Java swings. No I am stuck in one prob.

After chooseing a file from showOpenDialog() I need to display the content of the file to user. How we can display the file content in swings ?

Please help me out.

Thanks
Raghava
0
Comment
Question by:raghava_dg
  • 3
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 20 total points
ID: 11698773
Use a JEditorPane and add it to your JFrame:


            JEditorPane pane = new JEditorPane();
            try {
                  pane.getEditorKit().read(new java.io.FileReader("yourFile.txt"), pane.getDocument(), 0);
            }
            catch (Exception e) {
                  e.printStackTrace();
            }
            getContentPane().add(pane, BorderLayout.CENTER);
0
 

Author Comment

by:raghava_dg
ID: 11699216
Thank you for the quick help . but my main window in JPanel (inside that i have tabs) .
but getContentPane() is a method on JFrame .  how can I use this. my problem is a new window should pop up displating the file content . I am very new to swings . please help me out .
0
 
LVL 12

Expert Comment

by:Giant2
ID: 11700931
you could use JOptionPane:
JOptionPane.showInternalMessageDialog(frame, "information","information", JOptionPane.INFORMATION_MESSAGE);

or:
JDialog dialog = new JDialog();
//use what CEHJ suggest. Replace the last instruction with the following
dialog.getContentPane().add(pane,BorderLayout.CENTER);
dialog.show();


Bye, Giant.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:raghava_dg
ID: 11704107
thanks to both CEHJ and Giant2 . It worked out . I don't know how to share the points to both of you i will try it ..
0
 

Author Comment

by:raghava_dg
ID: 11727672
hey , one more thing ....

is it possible to open the file depending on the file type ?
for example if its a xls the it should open in excell . can it be done?
0
 
LVL 12

Expert Comment

by:Giant2
ID: 11733089
sure, it could be done.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSumClump challenge 9 115
arguments to jar 5 26
tomcat startup error 5 59
type mismatch (Object[] to double[] 4 24
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question