Question:
How to make a simple text editor with java?
Nighthawk0973
2011-04-12 12:05:05 UTC
I'm a beginning java programmer and am just learning GUIs and I thought it would be cool to make a java text editor. You don't have to walk me through it or anything just give me the code and try to make it as short as possible so I can learn by example providing you keep these features in mind:

I want to create files that are either *.txt, or if you can a custom file type for the editor like *.wwf (WordWriter files, I know not very original but I'm terrible at coming up with names) that have a custom icon with them and automatically open up my program when double clicked (I can make executables provided there is a .zip or in this case a .jar file to work with and I have a plugin for paint.NET so I can make the icons myself)

Also I want basic text editor features including:

a basic font,
a "new" button
a "save" button
a "save as" button
a "open" button

Finally I need custom window stuff so if someone has a file called "Untitled" that their editing than the window border at the top and in the bottom of the screen says:

"Untitled - WordWriter"

Plus it has to have something besides the java icon like in minecraft how it has the dirt block.

Thanks so much for you're help I'll give a link to the download when I'm done.

Again thanks in advance,
Nighthawk0973
Five answers:
anonymous
2011-04-12 13:02:42 UTC
I can't give you code because last time I did that yahoo would not accept my answer because it was too long and once my answer was truncated leaving the asker confused. But what I can tell you is how you will create your own file type with your own icon and extension that will open with your program when double clicked. This is called file association and is matter of setting a few registry keys.



The first key/entry you have to create is in HKEY_CLASSES_ROOT



This key must be called .ext where ext is your file extension (make sure it does not already exist)

in the default value of that key you have to set it a unique identifier of your program. This identifier identifies another key which you have to create that will contain information about file type eg MyCustApp



The second key you have to create is the identifier you wrote (HAS TO BE UNIQUE).

The default value of this key is a description of the file eg "Word Writer Files"



You have to create a sub key of that key which is called DefaultIcon. The default value has to be of type REG_EXPAND_SZ and contains either the path to your custom icon or a path to a executable with icon in resource. Be sure to use " quotes if your path contains spaces.



A New sub key "shell" has to be create with sub key "open" with yet another sub key "command"

which in the default value has the path to your program to open the files in " quotes. then your close quotes leave a space open quotes again and put %1 and close quotes again. When a file is doubled clicked %1 will be replaced with the file name and will be passed as a command line argument. I presume any format can be used.
?
2016-12-12 14:45:20 UTC
Text Editor Program In Java
anonymous
2011-04-12 12:35:20 UTC
A program like that is pretty simple.



First, you'll need your main class to be a JFrame. Add a WindowListener to it

When created, you will be adding the GUI first. Create a JMenuBar, and add all the JMenus and JMenuItems you want to it. Remember to add ActionListeners to the JMenuItems so things happen when they are selected.

After everything has been added to the JMenuBar, set it as the JFrame's menubar. You can also set the icon for the frame now as well. I will explain the various options you requested after we get the textbox set up.



Now, we'll add the main typing area. Just create a JScrollPane, using a new JTextArea, and add it to the JFrame. This allows the user to type all they want, and scroll around if they need to.



Now, as for the JMenuItems, this is what you'll need:



"new": When selected, have it ask if you want to save your current work first. Use JOptionPane for this. Then, set the text in the JTextArea to an empty string.

"save": You can save files easily using the JFileChooser to have them select a file, or choose an existing one to overwrite. Then, once the file is selected, just get the contents of the JTextArea, call split("\n") to split the contents into separate lines as a String[], and use a BufferedWriter to write one line at a time to the selected file. Be sure to call the BufferedWriter's newLine() function between each line you write.

"save as": Same as the "save" choice above.

"open": Similar to "save", first ask them if they want to save the current file. Then, use a JFileChooser to have them select a file from somewhere, and use a BufferedReader to read all the lines in it. Combine them into one String, adding "\n" between each line. Then, just set the text of the JTextArea to that one String which contains all the lines of text.



Lastly, in the windowClosing method which is used by the WindowListener you added, you need to have it ask them if they want to save before closing it.







I know these aren't the most detailed instructions, but you should be able to get it from this. Just search google for any class names you need to learn how to use, and the first link will usually have all the methods and field of that class, with descriptions of them. It should resemble this page: http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/JTextArea.html
anonymous
2011-04-12 12:10:51 UTC
to give you code for something so big on such little space is damn near impossible.



if you know how to make java windows then you want to aim for something smaller.



but if you dont, dont even try to make a text editor program because thats oretty advance and to much coding to put into an answer. but look up some guides, a simple googling/binging will work for sure.
?
2016-03-02 10:27:29 UTC
Make sure you are setting up the right enviroments. And using the right class/package. ----I haven't really heard of it but i'd che the Java API


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...