We were assigned to create a simple compiler as homework, which would take many instructions (containing variables, conditions, jumps, etc.) and evaluate them. This has already been done, but I thought that I would make my program a little more ... "brilliant" and add the ability to load instructions from a text file just for the convenience of the user; however, it seems that the JTextArea append () method does not seem to like me very much, since it does nothing. Here is the relevant code:
BufferedReader bufferedReader; File file; FileDialog fileDialog = new FileDialog (new Frame (), "Open File", FileDialog.LOAD); String line; fileDialog.setVisible (true); if (fileDialog.getFile () != null) { file = new File (fileDialog.getDirectory () + fileDialog.getFile ()); input.setText (""); // delete old first try { bufferedReader = new BufferedReader (new FileReader (file)); line = bufferedReader.readLine (); while (line != null) { input.append (line); System.out.println (line); line = bufferedReader.readLine (); } } catch (IOException ioe) { ioe.printStackTrace (); } }
(I use Awt FileDialog instead of Swing JFileChooser because it just looks better on Mac, as shown in Apple's official recommendation .)
The input variable used in this code points to an instance of JTextArea. The funny thing is: the part of reading the file should work flawlessly, since I see that the contents of the file are written to standard output by calling System.out.println () in a while . However, nothing is displayed in JTextArea , and I tried all the existing solutions that I found here in StackOverflow, which includes calls to the repaint () , revalidate () and updateUI () .
What am I missing? Thanks so much for any answer!
source share