• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextArea get file name  RSS feed

 
Stribor Kab
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a file loaded in JtextArea and on a click of a button I would like to show a file chooser with name of current loaded file already filled in "File Name" field of file chose. How would I solve this?

Thanks
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Added to Swing / AWT / SWT forum.
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See JFileChooser
https://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html
 
Stribor Kab
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am aware of that. But let's say I have my file.txt currently open in textsrea.  I want to click on a button and have file chooser dialog box file.txt already filled in file name. 
I guess my question is how do I determine what is the name of currently opened file in Jtextarea?
 
Mohammed Sardar.
Ranch Hand
Posts: 193
1
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stribor Kab wrote:

I am aware of that. But let's say I have my file.txt currently open in textsrea. 


As you have specified file.txt is the file opened, somehow before filling JTextArea file would have been opened by File System with a file name. The filename was  there before the content read to JTextArea. Is the file name randomly picked up ?

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do I determine what is the name of currently opened file in Jtextarea?

Where in the program is the file opened and its contents read?  At that point the program needs to save the name of the file that is being read so that it can be used later as needed.
 
Stribor Kab
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I run my program.....I click on a button which gets file choser presented and then I chose the file to open in jtextarea.
Now I want to click another button and implement a sort of "save as" and want to have name of the currently opened file pre-filled in file name
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I chose the file to open

Have the code that opens the file save the name of the file it is opening.
 
Stribor Kab
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok and how do I pre-fill dialog box with that name?
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read the API doc for the JFileChooser class?  If it is possible, the method to use will be documented there.
The API doc link: http://docs.oracle.com/javase/8/docs/api/index.html
 
Stribor Kab
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
I chose the file to open

Have the code that opens the file save the name of the file it is opening.

The only problem with that is that i am opening the file with different button and therefore opened file name can be saved only in that Action Listener.
If I go to save as Action Listener I wouldnt be able to get the name of opened file....unless I declare that file globally so it accessible from anywhere?
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
declare that file globally so it accessible from anywhere

Yes, that is a common way to do it.  Define a variable at the class level so all methods can see it.
 
Stribor Kab
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
setSelectedFile(..) seems to do what I need.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!