Results 1 to 10 of 10
Thread: Stumped on unknown problem
- 02-16-2012, 12:08 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 28
- Rep Power
- 0
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
-
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-jtextareaLast edited by Fubarable; 02-16-2012 at 12:33 AM.
- 02-16-2012, 12:33 AM #3
Member
- Join Date
- Feb 2012
- Posts
- 28
- Rep Power
- 0
Re: Stumped on unknown problem
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.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) { } }
-
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?
- 02-16-2012, 12:50 AM #5
Member
- Join Date
- Feb 2012
- Posts
- 28
- Rep Power
- 0
Re: Stumped on unknown problem
Text is a global variable in my textEditor class. My fileHandlers class extends textEditor.
Those are a few of my global variables.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();
EDIT: And my openFileHandler() just accesses textEditor.class for the variable.
-
Re: Stumped on unknown problem
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.
- 02-16-2012, 03:31 AM #7
Member
- Join Date
- Feb 2012
- Posts
- 28
- Rep Power
- 0
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.
-
Re: Stumped on unknown problem
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.
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.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?
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-classesLast edited by Fubarable; 02-16-2012 at 03:52 AM.
- 02-16-2012, 05:24 AM #9
Member
- Join Date
- Feb 2012
- Posts
- 28
- Rep Power
- 0
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.
- 02-16-2012, 09:55 AM #10
Re: Stumped on unknown problem
Why do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
problem about Unknown Black Screen
By lawlawlaws in forum Java 2DReplies: 1Last Post: 02-02-2012, 02:08 AM -
Problem with using the keyboard - completely stumped
By cwbr in forum AWT / SwingReplies: 9Last Post: 01-29-2012, 03:03 AM -
import Excel file into Mysql DB via Java unknown problem
By black300 in forum JDBCReplies: 0Last Post: 04-02-2011, 10:22 AM -
stumped......
By trueblue in forum New To JavaReplies: 21Last Post: 07-13-2009, 04:16 PM -
Unknown problem
By sanchir0805 in forum New To JavaReplies: 4Last Post: 03-20-2009, 03:56 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks