3 Replies - 291 Views - Last Post: 09 May 2012 - 11:57 AM Rate Topic: -----

#1 Nuko32  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 23
  • Joined: 08-May 12

Help with Java OpenFile?

Posted 08 May 2012 - 10:18 AM

Hi guys!
I need help with some code. I'm new here and I program in Java since 2 months. I'm trying to create an editor with Java GUI, actually, I've made the "Save" button and the "Close" button. But HOW CAN I CREATE THE OPEN BUTTON?? Here is the code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class SimplestEditor extends JFrame implements ActionListener {
	public static void main(String[] args) {
		new SimplestEditor().setVisible(true);
	}

	private JTextField textField;

	public SimplestEditor() {
		super("Simplest Editor");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setPreferredSize(new Dimension(400, 300));
		pack();
		setLocationRelativeTo(null);

		// creates the menu bar
		JMenuBar menuBar = new JMenuBar();

		// creates the file menu
		JMenu fileMenu = new JMenu("File");

		// creates a new menu item
		JMenuItem saveMenuItem = new JMenuItem("Save");
		saveMenuItem.addActionListener(this);
		fileMenu.add(saveMenuItem);

		// creates a new menu item
		JMenuItem exitMenuItem = new JMenuItem("Exit");
		exitMenuItem.addActionListener(this);
		fileMenu.add(exitMenuItem);

		// creates the edit menu
		JMenu editMenu = new JMenu("Edit");

		// creates a new submenu
		JMenu changeFontColorMenuItem = new JMenu("Font Color");

		// red menu item
		JMenuItem redMenuItem = new JMenuItem("Red");
		redMenuItem.addActionListener(this);
		changeFontColorMenuItem.add(redMenuItem);

		// blue menu item
		JMenuItem blueMenuItem = new JMenuItem("Blue");
		blueMenuItem.addActionListener(this);
		changeFontColorMenuItem.add(blueMenuItem);

		// green menu item
		JMenuItem greenMenuItem = new JMenuItem("Green");
		greenMenuItem.addActionListener(this);
		changeFontColorMenuItem.add(greenMenuItem);

		// adds the menu item to the edit menu
		editMenu.add(changeFontColorMenuItem);

		// creates a new menu item
		JMenuItem clearMenuItem = new JMenuItem("Clear");
		clearMenuItem.addActionListener(this);
		editMenu.add(clearMenuItem);

		// add the menus to the menu bar
		menuBar.add(fileMenu);
		menuBar.add(editMenu);

		// sets the menu bar
		setJMenuBar(menuBar);

		// creates the text field and adds it to the panel
		textField = new JTextField();
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(textField, BorderLayout.CENTER);
		// sets the content panel
		setContentPane(panel);
	}

	/**
	 * Saves the contents of the text field into a file.
	 */
	protected void save() throws IOException {
		// creates a file chooser to allow the user to select the file name and
		// folder.
		JFileChooser dialog = new JFileChooser();
		// default file name is "file.txt"
		File file = new File("file.txt");
		dialog.setSelectedFile(file);
		// displays the dialog
		if (dialog.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
			// gets the selected file
			file = dialog.getSelectedFile();
			// creates a file writer
			FileWriter writer = new FileWriter(file);
			// writes the contents of the text field into file
			writer.append(textField.getText());
			// closes the file writer
			writer.close();
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String action = e.getActionCommand();
		if ("Save".equals(action)) {

			try {
				save();
				JOptionPane.showMessageDialog(null, "File has been saved.");
			} catch (IOException e1) {
				JOptionPane.showMessageDialog(null, "Could not save the file.");
			}

		} else if ("Exit".equals(action)) {

			System.exit(0);

		} else if ("Red".equals(action)) {

			textField.setForeground(Color.red);

		} else if ("Blue".equals(action)) {

			textField.setForeground(Color.blue);

		} else if ("Green".equals(action)) {

			textField.setForeground(Color.green);

		} else if ("Clear".equals(action)) {

			textField.setText(null);

		}
	}
}

Please help me guys! Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Help with Java OpenFile?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Help with Java OpenFile?

Posted 08 May 2012 - 10:45 AM

Almost the same thing as the Save button with a FileWriter
just use a FileReader instead
Was This Post Helpful? 0
  • +
  • -

#3 Nuko32  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 23
  • Joined: 08-May 12

Re: Help with Java OpenFile?

Posted 09 May 2012 - 07:22 AM

View Postpbl, on 08 May 2012 - 10:45 AM, said:

Almost the same thing as the Save button with a FileWriter
just use a FileReader instead


Can you give me an example? Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Help with Java OpenFile?

Posted 09 May 2012 - 11:57 AM

http://www.java-samp...?tutorialid=392
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1