import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class LevelEditor implements ActionListener {
File input;
File output;
FileReader in;
JFileChooser fc = new JFileChooser();
String newline = "/n";
JTextArea log;
int c;
public JMenuBar createMenuBar() {
//Where the GUI is created:
JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
//Create the menu bar.
menuBar = new JMenuBar();
//Build the first menu.
menu = new JMenu("File");
menu.setMnemonic(KeyEvent.VK_A);
menuBar.add(menu);
//a group of JMenuItems
menuItem = new JMenuItem("Open", KeyEvent.VK_O);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.ALT_MASK));
menuItem.addActionListener(this);
menu.add(menuItem);
menuItem = new JMenuItem("Save", KeyEvent.VK_S);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.ALT_MASK));
menuItem.addActionListener(this);
menu.add(menuItem);
menuItem = new JMenuItem("Close", KeyEvent.VK_F4);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, ActionEvent.ALT_MASK));
menuItem.addActionListener(this);
menu.add(menuItem);
return menuBar;
}
public Container createContentPane() {
JPanel contentPane = new JPanel(new GridLayout(0,1));
contentPane.setBackground(Color.white);
contentPane.setPreferredSize(new Dimension(500, 500));
contentPane.setOpaque(true);
log = new JTextArea(5,50);
log.setEditable(false);
contentPane.add(log);
return contentPane;
}
public void actionPerformed(ActionEvent e) {
String event = e.getActionCommand();
if (event.equals("Close")){
log.append("Close");
}else if (event.equals("Open")) {
log.append("Open");
int returnVal = fc.showOpenDialog(createContentPane());
if (returnVal == JFileChooser.APPROVE_OPTION) {
input = fc.getSelectedFile();
try{
in = new FileReader(input);
while ((c = in.read()) != -1){
log.append(String.valueOf(c));
System.out.print((char) c);
}
}
catch(IOException io){
System.out.println(io);
}
} else {
System.out.println("Open command cancelled by user.");
}
}else if (event.equals("Save")) {
log.append("Save");
fc.showSaveDialog(createContentPane());
}
}
private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
JFrame frame = new JFrame("Level Editor");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set the menu bar
LevelEditor editor = new LevelEditor();
frame.setJMenuBar(editor.createMenuBar());
frame.setContentPane(editor.createContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class LevelEditor implements ActionListener {
File input;
File output;
FileReader in;
JFileChooser fc = new JFileChooser();
String newline = "/n";
JTextArea log;
int c;
public JMenuBar createMenuBar() {
//Where the GUI is created:
JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
//Create the menu bar.
menuBar = new JMenuBar();
//Build the first menu.
menu = new JMenu("File");
menu.setMnemonic(KeyEvent.VK_A);
menuBar.add(menu);
//a group of JMenuItems
menuItem = new JMenuItem("Open", KeyEvent.VK_O);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.ALT_MASK));
menuItem.addActionListener(this);
menu.add(menuItem);
menuItem = new JMenuItem("Save", KeyEvent.VK_S);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.ALT_MASK));
menuItem.addActionListener(this);
menu.add(menuItem);
menuItem = new JMenuItem("Close", KeyEvent.VK_F4);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, ActionEvent.ALT_MASK));
menuItem.addActionListener(this);
menu.add(menuItem);
return menuBar;
}
public Container createContentPane() {
JPanel contentPane = new JPanel(new GridLayout(0,1));
contentPane.setBackground(Color.white);
contentPane.setPreferredSize(new Dimension(500, 500));
contentPane.setOpaque(true);
log = new JTextArea(5,50);
log.setEditable(false);
contentPane.add(log);
return contentPane;
}
public void actionPerformed(ActionEvent e) {
String event = e.getActionCommand();
if (event.equals("Close")){
log.append("Close");
}else if (event.equals("Open")) {
log.append("Open");
int returnVal = fc.showOpenDialog(createContentPane());
if (returnVal == JFileChooser.APPROVE_OPTION) {
input = fc.getSelectedFile();
try{
in = new FileReader(input);
while ((c = in.read()) != -1){
log.append(String.valueOf(c));
System.out.print((char) c);
}
}
catch(IOException io){
System.out.println(io);
}
} else {
System.out.println("Open command cancelled by user.");
}
}else if (event.equals("Save")) {
log.append("Save");
fc.showSaveDialog(createContentPane());
}
}
private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
JFrame frame = new JFrame("Level Editor");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set the menu bar
LevelEditor editor = new LevelEditor();
frame.setJMenuBar(editor.createMenuBar());
frame.setContentPane(editor.createContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Now I've been trying to make it able to save the text. I wrote some code but it doesn't seem to work. It saves a file but it's empty. If I load a file and save it without changing anything it works.
Here is the code:
view plainprint?
Note: Text content in the code blocks is automatically word-wrapped
else if (event.equals("Save")) {
int returnVal = fc.showSaveDialog(container);
if (returnVal == JFileChooser.APPROVE_OPTION) {
output = fc.getSelectedFile();
try{
out = new FileWriter(output);
for (int i = 0; i < log.getText().length(); i++){
out.write(log.getText().charAt(i));
}
}
catch(IOException io){
System.out.println(io);
}
} else {
System.out.println("Save command cancelled by user.");
}
}
else if (event.equals("Save")) {
int returnVal = fc.showSaveDialog(container);
if (returnVal == JFileChooser.APPROVE_OPTION) {
output = fc.getSelectedFile();
try{
out = new FileWriter(output);
for (int i = 0; i < log.getText().length(); i++){
out.write(log.getText().charAt(i));
}
}
catch(IOException io){
System.out.println(io);
}
} else {
System.out.println("Save command cancelled by user.");
}
}
I also tried using PrintWriters methods print(string) and write(string) but they didn't work either.
You didn't close the writer after you finished using it. Since writing to disk files happens in chunks of several K bytes at a time (because that's how the operating system deals with files), you're losing the last chunk. Which is probably the whole thing. Close the writer in a finally block like this:
view plainprint?
Note: Text content in the code blocks is automatically word-wrapped
out = null;
try{
out = new FileWriter(output);
for (int i = 0; i < log.getText().length(); i++){
out.write(log.getText().charAt(i));
}
}
catch(IOException io){
// System.out.println(io); // Not enough information...
io.printStackTrace(); // More useful information
}
finally{
if (out != null){
out.close();
}
}
out = null;
try{
out = new FileWriter(output);
for (int i = 0; i < log.getText().length(); i++){
out.write(log.getText().charAt(i));
}
}
catch(IOException io){
// System.out.println(io); // Not enough information...
io.printStackTrace(); // More useful information
}
finally{
if (out != null){
out.close();
}
}
Copyright © 2011 - All Rights Reserved - Softron.in
Template by Softron Technology