Java text editor help

Discussion in 'Software' started by red death68, Jan 4, 2013.

  1. red death68

    red death68 Command Sergeant Major

    So I am making a text editor in java that will be used for taking math notes. The main goal of this is to have special math characters be inserted into the text which I have figured out after very much googling but now there are a few things I am trying to get working and some things I have questions on.

    First I am trying to disable the delete menu button unless there is an active selection in the text but my if statement doesn't seem to work properly.

    Second I need to know how to add open and close functions to the program hopefully for .txt and .rtf as a minimum.

    my code is as follows(I will also post the archived versions of my code)

    Code:
    import javax.swing.*;
    import java.awt.event.*;
    
    @SuppressWarnings("serial")
    /**
     * class designed to make taking notes in math class easier
     * @author reddeath68
     */
    public class Notes extends JFrame 
    {
    	JMenuBar menuBar; //creates manuBar variable for later use
    	JMenu file, characters, edit; //creates file and characters variables for later use
    	JMenuItem exit, square, copy, paste, delete, selectAll, cut; //creates exit and square variables for later use
    	private final static String sqroot = "\u221A"; //creates sqroot variable for later use 
    	Text text = new Text();
    	int position = 0;
    	
    	
    	/**
    	 * Constructor sets menu bar and other important aspects of the program
    	 */
    	public Notes()
    	{
    		add(text.getTextArea()); //adds the text area to the window
    		//menu for program operations.
    		menuBar = new JMenuBar(); //Creates menu bar
    		setJMenuBar(menuBar); //sets menu bar to appear in window
    		
    		file = new JMenu("File"); //creates file button
    		menuBar.add(file); //adds file button to menu bar
    		exit = new JMenuItem("Exit"); //creates exit button
    		file.add(exit); //creates exit button under file buttons drop down
    		exitBottonPress exitEvent = new exitBottonPress(); //creates event
    		exit.addActionListener(exitEvent); //defines event for when exit is clicked (see class)
    		
    		//menu for edit functions
    		edit = new JMenu("Edit");
    		menuBar.add(edit);
    		//menu for copy function
    		copy = new JMenuItem("Copy");
    		edit.add(copy);
    		editMenuAction copyEvent = new editMenuAction();
    		copy.addActionListener(copyEvent);
    		//menu for cut function
    		cut = new JMenuItem("Cut");
    		edit.add(cut);
    		editMenuAction cutEvent = new editMenuAction();
    		cut.addActionListener(cutEvent);
    		//menu for paste function
    		paste = new JMenuItem("Paste");
    		edit.add(paste);
    		editMenuAction pasteEvent = new editMenuAction();
    		paste.addActionListener(pasteEvent);
    		//menu for select all function
    		selectAll = new JMenuItem("Select All");
    		edit.add(selectAll);
    		editMenuAction selectAllEvent = new editMenuAction();
    		selectAll.addActionListener(selectAllEvent);
    		//menu for delete function
    		delete = new JMenuItem("Delete");
    		edit.add(delete);
    		editMenuAction deleteEvent = new editMenuAction();
    		delete.addActionListener(deleteEvent);
    		delete.setEnabled(false);
    		if (text.textarea.getSelectionStart() == text.textarea.getSelectionEnd())
    		{
    			delete.setEnabled(false);
    		} else
    		{
    			delete.setEnabled(true);
    		}
    		
    		//menu for special characters
    		characters = new JMenu("Special Characters"); //creates special character button
    		menuBar.add(characters); //adds special character button to menu bar
    		square = new JMenuItem("Square Root \u221A"); //creates square root button 
    		characters.add(square); //adds square root button under special characters drop down
    		squareRootButtonPress sqevent = new squareRootButtonPress(); //creates event
    		square.addActionListener(sqevent); //defines event when square root is clicked( see class)
    	}
    
    		/**
    		 * Defines the actions to take place upon clicking square root button in menu
    		 * @author reddeath68
    		 */
    		public class squareRootButtonPress implements ActionListener 
    		{
    			public void actionPerformed(ActionEvent sqevent) 
    			{
    				int pos = text.textarea.getCaretPosition();
    				text.getTextArea().insert(sqroot, pos); //adds square root symbol to text area
    			}
    		}	
    		
    		/**
    		 * Defines action to take place upon clicking the exit button
    		 * @author reddeath68
    		 */
    		public class exitBottonPress implements ActionListener 
    		{
    			public void actionPerformed(ActionEvent exitevent) 
    			{
    				System.exit(0); //closes the program
    			}
    		}
    		
    		public class editMenuAction implements ActionListener
    		{
    			public void actionPerformed(ActionEvent editEvent)
    			{
    				if (editEvent.getSource().equals(copy))
    				{
    					text.textarea.copy();
    				}  
    					
    				if (editEvent.getSource().equals(cut))
    				{
    					text.textarea.cut();
    				}
    				
    				if (editEvent.getSource().equals(paste))
    				{
    					text.textarea.paste();
    				}
    				
    				if (editEvent.getSource().equals(selectAll))
    				{
    					text.textarea.selectAll();
    					//delete.setEnabled(true);
    				}
    				
    				if (editEvent.getSource().equals(delete))
    				{
    					text.textarea.setText(text.textarea.getText().replace(text.textarea.getSelectedText(),""));
    				}
    					
    					
    			}
    		}
    		
    		/**
    		 * main class defines gui
    		 * @param args
    		 */
    		public static void main (String args[]) 
    		{
    			Notes gui = new Notes();
    			gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //closes program with the window
    			gui.setSize(800, 600); //sets initial window size
    			gui.setVisible(true); //makes window visible when program runs
    			gui.setTitle("Mathamatical Note Taker"); //sets title of the window
    		}
    }
    

    and this is the text class I am still debating whether it is a good idea to have this as a separate class

    Code:
    import java.awt.Color;
    import java.awt.Insets;
    import javax.swing.event.*;
    
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class Text extends JFrame
    {
    	JTextArea textarea; //creates and declares text area variable for later use
    	
    	public Text()
    	{
    		setTextArea();
    	}
    	
    	
    	
    	public void setTextArea()
    	{
    		textarea = new JTextArea("hi");
    		textarea.setWrapStyleWord(true);
    		textarea.setLineWrap(true);
    		textarea.setSize(800, 600);
    		textarea.setMargin(new Insets(0, 5, 0, 0));
    		textarea.setEditable(true);
    		textarea.setBackground(Color.BLACK);
    		textarea.setForeground(Color.GREEN);
    		textarea.setCaretColor(Color.GREEN);
    		textarea.addCaretListener(new CaretListener() 
    		{
    			public void caretUpdate(CaretEvent e) 
    			{
    				//JTextArea editArea = (JTextArea)e.getSource();
    			}
    		});
    	}
    	
    	public JTextArea getTextArea()
    	{
    		return textarea;
    	}
    }
    
    All advise and help is much appreciated. I am still very new to java and am doing this as I learn. I would be forever lost if not for google.

    Note: not letting me add zip files I will try with next post.
     
  2. red death68

    red death68 Command Sergeant Major

    Here is the project archive I am using eclipse to write this code.

    popup blocker wouldn't display the dialogue to upload in the first post
     

    Attached Files:

  3. PC-XT

    PC-XT Master Sergeant

    "First I am trying to disable the delete menu button unless there is an active selection in the text but my if statement doesn't seem to work properly."

    The constructor only checks just after it's created, so there probably won't be selected text. You should hook the textarea selection routines to enable or disable it, or it might work to just add an onmouseover event to the button that makes the check, as you already have that in the constructor. It would be less annoying if you do it the first way, though. :p (so you would know before moving the mouse there)

    "Second I need to know how to add open and close functions to the program hopefully for .txt and .rtf as a minimum."

    A Unicode stream would probably be the best bet for human-readable .txt. I think your textarea may have a getUnicode method that can just >> to a Unicode stream, but not positive...

    RTF would need some parts escaped, but may be able to make other parts look better. I don't remember if Java has special RTF writers, or what they're capabilities are as far as using RTF's math formula formatting abilities. If I see something, I'll post it. ;)
     
  4. red death68

    red death68 Command Sergeant Major

    ok with the structure of my classes I can't see a way to implement your first suggest would you suggest refactoring my class and if so how would you go about it?
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds