wim DC said:
- Yes, that should work, it will only display 1 line tho. But shouldn't cause errors.
- What's the exception you get?
- Better give the view a method setText, instead of getting the textArea. The model should know as less as possible about the view.
Thanks for helping... still dont get this tho. Ill paste MVC-classes and try to explain.
I have similarly "almost" solved the save()-to-file function. I can write to selected file whatever i put into " "; but I cant use v.getTextArea().setText() as an reference instead of it to actually write whats in my textarea to the file. Maybe this is having to do with that i am getting the textarea as u said. But i already created getters and setter so i can call it from Controller too, how would i change for the best?
UPDATE Wow, i found out that i have two Constructors in Model, one which i havent noticed, must have accidently added it sometime. Thing is when i remove it which is completely blank the constructors in View and Controller are affected and vice versa. If i remove the one "i thought i was using" and letting the blank be left its still the same. Can you explain whats happening?
I bold out all the essential parts. What is the best to do here?
Also, the exception that happens if i dont use System.out.print(st) or System.out.print("Whatever I like but not area content") is a nullpointerexception that traces back to the lines where i either if so tries to write: v.getTextArea.setText()
VIEW
import java.awt.*;
@SuppressWarnings("serial")
public class View extends javax.swing.JFrame {
// Variables declaration
private Controller c;
private Model m;
private javax.swing.JMenuItem copy;
private javax.swing.JMenuItem cut;
private javax.swing.JMenu edit;
private javax.swing.JMenuItem exit;
private javax.swing.JMenu file;
private javax.swing.JMenuBar menuBar;
private javax.swing.JMenuItem newFile;
private javax.swing.JMenuItem open;
private javax.swing.JMenuItem paste;
private javax.swing.JMenuItem save;
private javax.swing.JMenuItem saveAs;
private javax.swing.JScrollPane scrollPane;
private javax.swing.JTextArea textArea;
// End of variables declaration
/** Creates new form View */
[B]public View(Controller con) {
c = con;
m = new Model();[/B]
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
*/
private void initComponents() {
scrollPane = new javax.swing.JScrollPane();
setTextArea(new javax.swing.JTextArea());
menuBar = new javax.swing.JMenuBar();
setFile(new javax.swing.JMenu());
newFile = new javax.swing.JMenuItem();
open = new javax.swing.JMenuItem();
save = new javax.swing.JMenuItem();
saveAs = new javax.swing.JMenuItem();
exit = new javax.swing.JMenuItem();
edit = new javax.swing.JMenu();
copy = new javax.swing.JMenuItem();
cut = new javax.swing.JMenuItem();
paste = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Notepad");
getTextArea().setColumns(28);
getTextArea().setRows(22);
scrollPane.setViewportView(getTextArea());
this.add(scrollPane, BorderLayout.CENTER);
getFile().setText("File");
newFile.setMnemonic('N');
newFile.setText("New");
newFile.addActionListener(new NewFileListener(this));
getFile().add(newFile);
open.setMnemonic('O');
open.setText("Open");
open.setActionCommand("Open");
open.addActionListener(new OpenListener(this));
getFile().add(open);
save.setMnemonic('S');
save.setText("Save");
getFile().add(save);
saveAs.setMnemonic('A');
saveAs.setText("Save As...");
getFile().add(saveAs);
exit.setMnemonic('E');
exit.setText("Exit");
exit.addActionListener(new ExitListener(this));
getFile().add(exit);
menuBar.add(getFile());
edit.setText("Edit");
copy.setMnemonic('C');
copy.setText("Copy");
edit.add(copy);
cut.setMnemonic('U');
cut.setText("Cut");
edit.add(cut);
paste.setMnemonic('P');
paste.setText("Paste");
edit.add(paste);
menuBar.add(edit);
setJMenuBar(menuBar);
// Set the application in the middle of the screen
int width = 350;
int height = 500;
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x = (screen.width - width) / 2;
int y = (screen.height - height) / 2;
setBounds(x, y, width, height);
this.pack();
this.setVisible(true);
}
public void handleNewFile() {
m.newFile();
}
public void handleOpen() {
m.open();
}
public void handleSave() {
m.save();
}
public void handleSaveAs() {
m.saveAs();
}
public void handleExit() {
c.exit();
}
[B]public void setTextArea(javax.swing.JTextArea textArea) {
this.textArea = textArea;
}
public javax.swing.JTextArea getTextArea() {
return textArea;
}[/B]
public void setFile(javax.swing.JMenu file) {
this.file = file;
}
public javax.swing.JMenu getFile() {
return file;
}
}
MODEL
import java.io.*;
import javax.swing.JFileChooser;
public class Model {
@SuppressWarnings("unused")
private Controller c;
@SuppressWarnings("unused")
private View v;
private String currentFile = null;
private JFileChooser fc = new JFileChooser();
[B]public Model(View vi) {
c = new Controller();
v = vi;
}[/B]
public void open() {
fc = new JFileChooser();
fc.setDialogTitle("Open File");
int value = fc.showOpenDialog(null);
if(value == JFileChooser.APPROVE_OPTION) {
File f = fc.getSelectedFile();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String st = "";
try {
while((st = br.readLine()) != null){
//String fileString = st;
//v.getTextArea().setText(fileString);
[B]System.out.print(st);[/B]
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void save() {
}
public void saveAs() {
fc = new JFileChooser();
int value = fc.showSaveDialog(null);
if(value == JFileChooser.APPROVE_OPTION) {
File f = fc.getSelectedFile().getAbsoluteFile();
try {
FileWriter fw = new FileWriter(f);
BufferedWriter out = new BufferedWriter(fw);
[B]out.write("Whatever I want to file inside these but not my textarea");[/B]
//Close the output stream
out.close();
}catch (Exception e)
{ //Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
public void saveCurrentFile() {
}
[B]public Model() {
//WTF?
}[/B]
public void newFile() {
}
public void setCurrentFile(String currentFile) {
this.currentFile = currentFile;
}
public String getCurrentFile() {
return currentFile;
}
}
import javax.swing.JOptionPane;
@SuppressWarnings("serial")
public class Controller {
private View v;
private Model m;
private String name = null;
private int option;
[B]public Controller() {
v = new View(this);
m = new Model();
}[/B]
public void exit() {
// if the output area is empty just quit, otherwise prompt before leaving
if (!v.getTextArea().getText().equals("") && !v.getTextArea().getText().equals(m.getCurrentFile()))
{
if(name == null)
/* Shows a Confirm dialog box. */
option = JOptionPane.showConfirmDialog(null,"Do you want to save the file?");
if(option == 0)
{
/* Calls the saveAs() method. */
m.saveAs();
/* Closes the main application. */
System.exit(0);
}
if(option == 1)
{
System.exit(0);
}
else
{
/* Shows a Confirm dialog box. */
option = JOptionPane.showConfirmDialog(null,"Do you want to save the file?");
if(option == 0)
{
/* Calls the save() method. */
m.save();
System.exit(0);
}
if(option == 1)
{
System.exit(0);
}
}
}
else
{
System.exit(0);
}
}
public static void main(String args[]) {
@SuppressWarnings("unused")
Controller c = new Controller();
}
}