Results 1 to 10 of 10
  1. #1
    stevenfriz is offline Member
    Join Date
    Feb 2012
    Posts
    28
    Rep Power
    0

    Default Stumped on unknown problem

    I've been working on a text editor, which mainly consisted of two classes. My main class that handled everything was getting large (almost 1000 lines). I created a separate class that would consist of my fileHandlers( new file, open file, save file, etc). These methods took up the majority of the space. I have the fileHandlers() class extending my textEditor class. The Frame, JTextArea, etc, will run; however, open file and the other methods will not read or write text to the JTextArea.

    I would provide code if I could, but I don't know how to go about doing this. Eclipse compiles with no errors and the program runs with no errors. The text just isn't read or write. I've looked into super(), but It doesn't seem to do anything. I think my JTextArea (text) is being ran twice in my JFrame, and the text is being read to the bottom text area with the blank one on top.

    This has stumped me for so long, can anyone help?

    Thanks,
    Steven

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Stumped on unknown problem

    Without code, it's anyone's guess about where your bug is. A common newbie mistake though to look for is to make sure that you're writing to and reading from the correct GUI reference, that you're using the GUI object that is actually visualized on the screen and not creating a new non-visualized GUI that never displays the information written to it.

    To check for this, make sure that you are passing your references appropriately via constructor or method parameters. In other words, in your non-GUI classes that interact with the GUI, look suspiciously at any code where you call new GuiClass(), and instead make sure that you get the reference to the Gui class via a constructor or method (a setter method) parameter.

    Edit: and on review of your previous threads, I see that we've discussed just this very issue previously: 55130-cannot-print-text-file-jtextarea
    Last edited by Fubarable; 02-16-2012 at 01:33 AM.

  3. #3
    stevenfriz is offline Member
    Join Date
    Feb 2012
    Posts
    28
    Rep Power
    0

    Default Re: Stumped on unknown problem

    Java Code:
    	public void openFileHandler() throws FileNotFoundException, IOException
    	{
    		int status;      
    		JFileChooser chooser = new JFileChooser("Open File");
    		chooser.setAcceptAllFileFilterUsed(true);
    		
    
    		chooser.setSelectedFile(new File("NewFile.txt"));
    		status = chooser.showOpenDialog(null);              
    		if(status == JFileChooser.APPROVE_OPTION)
    		{
    			try
    			{
    				File opened = chooser.getSelectedFile();
    				String fileName = opened.getName();            
    				if(fileName.endsWith(".txt"))
    				{           		          
    					frame.setTitle("FrizPad - " + fileName);
    					fileNameAsString = fileName;        
    					fileLocation = opened;
    					if(fileLocation !=null)
    					{
    					save.setEnabled(true);
    					}
    					FileReader fileRead = new FileReader(opened);
    					BufferedReader buffRead = new BufferedReader(fileRead);
    					text.read(buffRead, opened.toString());
    				}
    				else
    				{
    					messagesHandlers initiateMessage = new messagesHandlers();
    					initiateMessage.choosedNoExtension();
    
    					this.openFileHandler();            	
    				}            		                        
    			}
    			catch(IOException ex)
    			{
    				messagesHandlers initiateMessage = new messagesHandlers();
    				initiateMessage.fileNotFound();                       
    			}
    		}
    		else if(status == JFileChooser.CANCEL_OPTION)
    		{   
    		}   
    	}
    There is a snippet of my program. text is my JTextArea. Are you saying that I put text in the parenthesis of openfileHandler()? I don't use parameters very often.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Stumped on unknown problem

    See edit to my answer above.

    But how are you getting your reference to text? Can you show the code where you get this?

  5. #5
    stevenfriz is offline Member
    Join Date
    Feb 2012
    Posts
    28
    Rep Power
    0

    Default Re: Stumped on unknown problem

    Text is a global variable in my textEditor class. My fileHandlers class extends textEditor.

    Java Code:
    	protected JFrame frame = new JFrame("FrizPad");
    	protected JTextArea text = new JTextArea();
    	protected JScrollPane scrollPane = new JScrollPane(text, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
    			JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    	protected JPopupMenu popup = new JPopupMenu();
    Those are a few of my global variables.

    EDIT: And my openFileHandler() just accesses textEditor.class for the variable.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Stumped on unknown problem

    Quote Originally Posted by stevenfriz View Post
    Text is a global variable in my textEditor class. My fileHandlers class extends textEditor.
    Aha!!!

    You're misusing inheritance, using it in place of using reference, and this is causing your problem. First off, the FileHandler classes should not extend a GUI class since they don't pass the basic rule of inheritance, the "is-a" rule. Is a FileHandler truly also a GUI object? Nope, it's behaviors and states are completely different.

    But even if it passed this test, realize that when you create a FileHandler object, while it extends the GUI, it is a completely new and distinct object from the visualized GUI, and any changes to the Swing components made in the FileHandler will never be visualized.

    Solution,
    • Don't have your control or model classes extend your view or GUI classes.
    • Don't misuse inheritance for what is truly a reference problem. Only use inheritance where it truly makes sense.
    • Instead get rid of all of those extends.
    • Give your control classes GUI variable(s) and use parameters to pass the visualized GUI into the control either in the control class's constructor or with a setGui(...) method that is called once in the beginning of your program.

  7. #7
    stevenfriz is offline Member
    Join Date
    Feb 2012
    Posts
    28
    Rep Power
    0

    Default Re: Stumped on unknown problem

    So if I have what your saying correctly, a GUI class can only be extended by a GUI class? Is there anyway I can have the global variables in one class, the frame constructor (textEditor.class), and my file handlers in separate classes? Or, basically, the only methods I can use in an outside class are methods that don't call for a GUI based object?

    I just want to get this down correctly.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Stumped on unknown problem

    Quote Originally Posted by stevenfriz View Post
    So if I have what your saying correctly, a GUI class can only be extended by a GUI class?
    No. I'm saying only using inheritance for situations where it is proper. For instance it is proper to have your Dog class extend Animal and have your Beagle class extend Dog. Don't try to use inheritance just to get access to another class's variables because that simply doesn't work.

    Is there anyway I can have the global variables in one class, the frame constructor (textEditor.class), and my file handlers in separate classes? Or, basically, the only methods I can use in an outside class are methods that don't call for a GUI based object?
    This is not the way of OOPs. With object oriented programming you don't want to have global variables and in fact you don't even want to expose hardly any variables. Instead do as I suggested: pass references and call public methods on the references.

    For example, please have a look at my code over on stackoverflow.com (I'm "Hovercraft full of eels"): mvc-progress-bar-threading and how-to-work-with-swing-with-multiple-classes
    Last edited by Fubarable; 02-16-2012 at 04:52 AM.

  9. #9
    stevenfriz is offline Member
    Join Date
    Feb 2012
    Posts
    28
    Rep Power
    0

    Default Re: Stumped on unknown problem

    I see. Well, I appreciate your help on this. It looks like I'm going to have to rework my code some.

    Off topic, do you know of any tutorials that explain fileFilters well? I cannot for love of hope get anything to work correctly, even oracles java docs don't help.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

Similar Threads

  1. problem about Unknown Black Screen
    By lawlawlaws in forum Java 2D
    Replies: 1
    Last Post: 02-02-2012, 03:08 AM
  2. Replies: 9
    Last Post: 01-29-2012, 04:03 AM
  3. Replies: 0
    Last Post: 04-02-2011, 11:22 AM
  4. stumped......
    By trueblue in forum New To Java
    Replies: 21
    Last Post: 07-13-2009, 05:16 PM
  5. Unknown problem
    By sanchir0805 in forum New To Java
    Replies: 4
    Last Post: 03-20-2009, 04:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •