Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: java noob needs help with text editor code :)

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java noob needs help with text editor code :)

    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import javax.swing.*;
    import javax.swing.text.*;
    import java.util.Hashtable;
     
    class TextEdit extends Jframe {
     
    	private JTextArea area = new JTextArea(10,60);
    	private JFileChooser dialog =
    		new JfileChooser(System.getProperty("user.dir"));
     
    	private String aktuellFil = "namlös";
    	private boolean ändrad = false;
     
    	public TextEdit() {   //layout
     
    		area.setFont(new Font("Monospaces", Font.PLAIN, 12));
    		JscrollPane scroll = new JScrollPane(area, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    		add(scroll, BorderLayout.CENTER);
     
    		JMenuBar mb = new JMenuBar();
    		setJMenuBar(mb);
    		JMenu arkiv = new JMenu("Arikiv");
    		Jmenu redi = new Jmenu("Redigera");
    		mb.add(arkiv); mb.add(redi);
     
    		arkiv.add(ny); arkiv.add(öppna);
    		arkiv.add(spara); arkiv.add(sparaSom);
    		arkiv.addSeparator(); arkiv.add(avsluta);
    		for (int i = 0; i<4; i++)
    			arkiv.getItem(i).setIcon(null);
     
     
    		redi.add(klipp); redi.add(kopierar); redi.add(klistra);
    		redi.getItem(0).setText("Klipp ut");
    		redi.getItem(1).setText("Kopiera");
    		redi.getItem(2).setText("Klistra in");
     
    		JToolBar verktyg = new JToolBar();
    		add(verktyg, BorderLayout.NORTH);
    		verktyg.add(ny); verktyg.add(öppna);
    		verktyg.add(spara); verktyg.addSeparator();
    		JButton cut=verktyg.add(klipp), cop=verktyg.add(kopiera), pas=verktyg.add(klistra);
    		cut.setText(null); cut.setIcon(new ImageIcon("cut.gif"));
    		cop.setText(null); cop.setIcon(new ImageIcon("copy.gif"));
    		pas.setText(null); pas.setIcon(new ImageIcon("paste.gif"));
     
    		spara.setEableEnable(false);
    		sparaSom.setEnable(false);
     
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		pack();
    		area.addKeyListener(kl);
    		setTitle(aktuellFil);
    		setVisible(true);	
    	}
     
    	private KeyListener kl = new KeyAdapter() {
    		public void keyPressed(KeyEvent e) {
    			ändrad = true;
    			spara.setEnabled(true);
    			sparaSom.setEnabled(true);
    		}
    	};
     
     
    	Action ny = new AbstractAction("Ny", new ImageIcon("new.gif")) {
    		public void actionPerformed(actionEvent e) {
    			sparaGammal();
    			area.setText("");
    			aktuellFil = "namnlös";
    			setTitle(aktuellFil);
    			ändrad = false;
    			spara.setEnabled(false);
    			sparaSom.setEnabled(false);
    		}6
    	};
     
    	Action öppna = new AbstractAction("öppna ...", new ImageIcon("Open.gif")){
    		public void actionPerformed(ActionEvent e) {
    			sparaGammal();
    			if (dialog.showOpenDialog(null)==JFileChooser.APPROVe_OPTION) {
    				läsInFil(dialog.getSelectedFile().getAbsolutePath());
    			}
    			sparaSom.setEnabled(true);
    		}
    	};
     
     
    	Action spara = new AbstractAction("Spara", new ImageIcon("save.gif)) {
    		public void actionPerformed(ActionEvent e) {
    			if (!aktuellFil.equals("namnlös"))
    				sparaFil(aktuellFil);
    				else
    				sparaFilSom();
    		}
    	};
     
    	Action spara som = new AbstractAction("Spara som.."){
    		public void actionPerformed(ActionEvent e){
    			sparaFilsom();
    		}
    	};
     
    	Action sparaSom = new AbstractAction("Avsluta") {
    		public void actionPerformed(ActionEvent e) {
    			sparaGammal();
    			System.exit(0);
    		}
    	};
     
    	ActionMap m = area.getActionMap();
    	Action klipp =m.get(DefaultEditiorKit.cutAction);
    	Action kopiera =m.get(DefaultEditiorKit.copyAction);
    	Action klistra =m.get(DefaultEditiorKit.pasteAction);
     
    	private void sparaFilSom() {
    		if (dialog.showSaveDialog(null)==JFileChooser.APPROVE_OPTION)
    			sparaFil(dialog.getSelectedFile().getAbsolutePath());
    	}
     
    	private void sparaFil(String filnamn) {
    		try {
    			FileWriter w = mew FileWriter(filnamn);
    			area.write(w);
    			w.close();
    			aktuellFil = filnamn;
    			setTitle(aktuellFil);
    			ändrad = false;
    			spara.setEnabled(false);
     
    		}
    		catch (IOException e) {}
    	}
    	public void sparaGammal() {
    		if (ändrad) {
    			if (JOptionPane.showConfirmDialog
    				(this,  "Skall filen " ? aktuellFil ? " sparas?", "",
    				JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
    			sparaFil(aktuellFil);
    		}
    	}
     
    	private void läsInFil(String filnamn) {
    		try {
    			FileReader r = new FileReader(filnamn);
    			area.read(r, null);
    			r.close();
    			aktuellFil = filnamn;
    			setTitle(aktuellFil);
    			ändrad = false;
    	}
    	catch (IOException e) {
    		Toolkit.getDefaulttOOLKIT().Beep();
    		JOptionPane.showMessageDialog
    			(this, "Det går inte att hitta filen " + filnamn);
    	}
    }

    I get 47 "error: class, interface, or enum expected" errors.
    would very much like some help. thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: java noob needs help with text editor code :)

    class, interface, or enum expected
    Check that all the { and } are properly paired.
    Copy and paste here the full text of the first few error messages that show where they occur in the source code.


    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java noob needs help with text editor code :)

    I have managed to fix some things, so i only have 16 errors now. The code in the first post in also updated. I think there still might be some very simple errors like a missing ")" or something. But am I missing any import packages, or something of that sort?

    here are all the errors:

    --------------------Configuration: <Default>--------------------
    C:\Users\Proxxy\workspace\Prg ALLT\TextEdit.java:78: error: illegal start of type
    }6
    ^
    C:\Users\Proxxy\workspace\Prg ALLT\TextEdit.java:78: error: ';' expected
    }6
    ^
    C:\Users\Proxxy\workspace\Prg ALLT\TextEdit.java:92: error: unclosed string literal
    Action spara = new AbstractAction("Spara", new ImageIcon("save.gif)) {
    ^
    C:\Users\Proxxy\workspace\Prg ALLT\TextEdit.java:92: error: ')' expected
    Action spara = new AbstractAction("Spara", new ImageIcon("save.gif)) {
    ^
    C:\Users\Proxxy\workspace\Prg ALLT\TextEdit.java:93: error: ';' expected
    public void actionPerformed(ActionEvent e) {
    ^
    C:\Users\Proxxy\workspace\Prg ALLT\TextEdit.java:93: error: invalid method declaration; return type required
    public void actionPerformed(ActionEvent e) {
    ^
    C:\Users\Proxxy\workspace\Prg ALLT\TextEdit.java:101: error: ';' expected
    Action spara som = new AbstractAction("Spara som.."){
    ^
    C:\Users\Proxxy\workspace\Prg ALLT\TextEdit.java:101: error: <identifier> expected
    Action spara som = new AbstractAction("Spara som.."){
    ^
    C:\Users\Proxxy\workspace\Prg ALLT\TextEdit.java:126: error: ';' expected
    FileWriter w = mew FileWriter(filnamn);
    ^
    C:\Users\Proxxy\workspace\Prg ALLT\TextEdit.java:140: error: : expected
    (this, "Skall filen " ? aktuellFil ? " sparas?", "",
    ^
    C:\Users\Proxxy\workspace\Prg ALLT\TextEdit.java:140: error: : expected
    (this, "Skall filen " ? aktuellFil ? " sparas?", "",
    ^
    C:\Users\Proxxy\workspace\Prg ALLT\TextEdit.java:140: error: illegal start of expression
    (this, "Skall filen " ? aktuellFil ? " sparas?", "",
    ^
    C:\Users\Proxxy\workspace\Prg ALLT\TextEdit.java:140: error: ')' expected
    (this, "Skall filen " ? aktuellFil ? " sparas?", "",
    ^
    C:\Users\Proxxy\workspace\Prg ALLT\TextEdit.java:141: error: illegal start of expression
    JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
    ^
    C:\Users\Proxxy\workspace\Prg ALLT\TextEdit.java:141: error: ';' expected
    JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
    ^
    C:\Users\Proxxy\workspace\Prg ALLT\TextEdit.java:160: error: reached end of file while parsing
    }
    ^
    16 errors

    Process completed.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: java noob needs help with text editor code :)

    What is the 6 doing there?

    Check that all "s are in pairs. One before and one after the String literal: "The literal"


    Suggestion for future programs. Compile more often. Don't wait so long before compiling that you get so many errors.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java noob needs help with text editor code :)

    nvm, the code is just too messed up. but thank you very much for the help.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: java noob needs help with text editor code :)

    reached end of file while parsing
    Check that all the {s have a matching }
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java noob needs help with text editor code :)

    Hello!
    I am sitting with proxxy and working with this code, trying too get it work.
    Now we are down to 10 errors =) and they all seems to be at the same place...

    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import javax.swing.*;
    import javax.swing.text.*;
     
    class TextEdit extends Jframe {
     
    	private JTextArea area = new JTextArea(10,60);
    	private JFileChooser dialog =
    		new JfileChooser(System.getProperty("user.dir"));
     
    	private String aktuellFil = "namlös";
    	private boolean ändrad = false;
     
    	public TextEdit() {   //layout
     
    		area.setFont(new Font("Monospaces", Font.PLAIN, 12));
    		JscrollPane scroll = new JScrollPane(area,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    		add(scroll, BorderLayout.CENTER);
     
    		JMenuBar mb = new JMenuBar();
    		setJMenuBar(mb);
    		JMenu arkiv = new JMenu("Arikiv");
    		Jmenu redi = new Jmenu("Redigera");
    		mb.add(arkiv); mb.add(redi);
     
    		arkiv.add(ny); arkiv.add(öppna);
    		arkiv.add(spara); arkiv.add(sparaSom);
    		arkiv.addSeparator(); arkiv.add(avsluta);
    		for (int i = 0; i<4; i++)
    			arkiv.getItem(i).setIcon(null);
     
     
    		redi.add(klipp); redi.add(kopiera); redi.add(klistra);
    		redi.getItem(0).setText("Klipp ut");
    		redi.getItem(1).setText("Kopiera");
    		redi.getItem(2).setText("Klistra in");
     
    		JToolBar verktyg = new JToolBar();
    		add(verktyg, BorderLayout.NORTH);
    		verktyg.add(ny); verktyg.add(öppna);
    		verktyg.add(spara); verktyg.addSeparator();
    		JButton cut=verktyg.add(klipp), cop=verktyg.add(kopiera), pas=verktyg.add(klistra);
    		cut.setText(null); cut.setIcon(new ImageIcon("cut.gif"));
    		cop.setText(null); cop.setIcon(new ImageIcon("copy.gif"));
    		pas.setText(null); pas.setIcon(new ImageIcon("paste.gif"));
     
    		spara.setEable(false);
    		sparaSom.setEnable(false);
     
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		pack();
    		area.addKeyListener(kl);
    		setTitle(aktuellFil);
    		setVisible(true);	
    	}
     
    	private KeyListener kl = new KeyAdapter() {
    		public void keyPressed(KeyEvent e) {
    			ändrad = true;
    			spara.setEnabled(true);
    			sparaSom.setEnabled(true);
    		}
    	};
     
     
    	Action ny = new AbstractAction("Ny", new ImageIcon("new.gif")) {
    		public void actionPerformed(ActionEvent e) {
    			sparaGammal();
    			area.setText("");
    			aktuellFil = "namnlös";
    			setTitle(aktuellFil);
    			ändrad = false;
    			spara.setEnabled(false);
    			sparaSom.setEnabled(false);
    		}
    	};
     
    	Action öppna = new AbstractAction("öppna ...", new ImageIcon("Open.gif")){
    		public void actionPerformed(ActionEvent e) {
    			sparaGammal();
    			if (dialog.showOpenDialog(null)==JFileChooser.APPROVE_OPTION) {
    				läsInFil(dialog.getSelectedFile().getAbsolutePath());
    			}
    			sparaSom.setEnabled(true);
    		}
    	};
     
     
    	Action spara = new AbstractAction("Spara", new ImageIcon("save.gif")) {
    		public void actionPerformed(ActionEvent e) {
    			if (!aktuellFil.equals("namnlös"))
    				sparaFil(aktuellFil);
    				else
    				sparaFilSom();
    		}
    	};
     
    	Action sparaSom = new AbstractAction("Spara som.."){
    		public void actionPerformed(ActionEvent e){
    			sparaFilsom();
    		}
    	};
     
    	Action avsluta = new AbstractAction("Avsluta") {
    		public void actionPerformed(ActionEvent e) {
    			sparaGammal();
    			System.exit(0);
    		}
    	};
     
    	ActionMap m = area.getActionMap();
    	Action klipp = m.get(DefaultEditorKit.cutAction);
    	Action kopiera = m.get(DefaultEditorKit.copyAction);
    	Action klistra = m.get(DefaultEditorKit.pasteAction);
     
    	private void sparaFilSom() {
    		if (dialog.showSaveDialog(null)==JFileChooser.APPROVE_OPTION)
    			sparaFil(dialog.getSelectedFile().getAbsolutePath());
    	}
     
    	private void sparaFil(String filnamn) {
    		try {
    			FileWriter w = new FileWriter(filnamn);
    			area.write(w);
    			w.close();
    			aktuellFil = filnamn;
    			setTitle(aktuellFil);
    			ändrad = false;
    			spara.setEnabled(false);
     
    		}
    		catch (IOException e) {}
    	}
    	public static void main (String[] arg) {
      new TextEdit();
    	}
    }
    	private void sparaGammal() {  //error
    		if (ändrad) {
    			if (JOptionPane.showConfirmDialog
    				(this,  "Skall filen " + aktuellFil + " sparas?", "",
    				JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
    			sparaFil(aktuellFil);
    		}  //error
    	}
     
    	private void läsInFil(String filnamn) {
    		try {
    			FileReader r = new FileReader(filnamn);
    			area.read(r, null);  //error
    			r.close();  //error
    			aktuellFil = filnamn;  //error
    			setTitle(aktuellFil);  //error
    			ändrad = false;  //error
    	}  //error
    	catch (IOException e) {
    		Toolkit.getDefaultToolkit().beep();
    		JOptionPane.showMessageDialog  //error
    			(this, "Det går inte att hitta filen " + filnamn);
    	}  //error
    }

    We have marked where the errors seems to occur, they are all located at the ending of the code.

    We are desperate and BIG NOOBS! =P Can someone give us the answer to what is wrong? All help is highly appreciated =D

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: java noob needs help with text editor code :)

    Please copy and post the full text of the error messages.

    We have marked where the errors seems to occur
    How did you mark it?
    Something like: //<<<<<<<<<<<<<<<<<< HERE IS ERROR<<<<<
    that is very easy to see and find.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java noob needs help with text editor code :)

    You have to excuse my stupidity XD as i have stated we ARE noobs.

    here is the eror messages...
    --------------------Configuration: <Default>--------------------
    C:\Users\treny\Desktop\Prg C\TextEdit.java:140: error: class, interface, or enum expected
    private void sparaGammal() { //error
    ^
    C:\Users\treny\Desktop\Prg C\TextEdit.java:146: error: class, interface, or enum expected
    } //error
    ^
    C:\Users\treny\Desktop\Prg C\TextEdit.java:152: error: class, interface, or enum expected
    area.read(r, null); //error
    ^
    C:\Users\treny\Desktop\Prg C\TextEdit.java:153: error: class, interface, or enum expected
    r.close(); //error
    ^
    C:\Users\treny\Desktop\Prg C\TextEdit.java:154: error: class, interface, or enum expected
    aktuellFil = filnamn; //error
    ^
    C:\Users\treny\Desktop\Prg C\TextEdit.java:155: error: class, interface, or enum expected
    setTitle(aktuellFil); //error
    ^
    C:\Users\treny\Desktop\Prg C\TextEdit.java:156: error: class, interface, or enum expected
    ändrad = false; //error
    ^
    C:\Users\treny\Desktop\Prg C\TextEdit.java:157: error: class, interface, or enum expected
    } //error
    ^
    C:\Users\treny\Desktop\Prg C\TextEdit.java:160: error: class, interface, or enum expected
    JOptionPane.showMessageDialog //error
    ^
    C:\Users\treny\Desktop\Prg C\TextEdit.java:162: error: class, interface, or enum expected
    } //error
    ^
    10 errors

    Process completed.


    And yes, the error lines are marked with //error

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: java noob needs help with text editor code :)

    The compiler has found code past the end of the class definition (past its ending }) that is not one of these:
    class, interface, or enum

    Check that the code after the } ending the class's definition is correctly located. That code should probably be inside the one and only class definition you have. If there is only one class, move the } that ends the class's definition to the end of the source.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. I find code Editor CSV file with jtable
    By mazaoa in forum AWT / Java Swing
    Replies: 1
    Last Post: November 27th, 2012, 08:27 PM
  2. Primitive text editor
    By jclaxton in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: October 15th, 2011, 12:01 PM
  3. Replies: 2
    Last Post: June 27th, 2011, 11:14 AM
  4. Text editor and command line
    By cl2606 in forum Java IDEs
    Replies: 2
    Last Post: June 8th, 2011, 08:20 AM
  5. making text editor using string class
    By petadeer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 14th, 2011, 03:28 AM