Follow on twitter : Ajay Halthor https://twitter.com/ajhalthor (@ajhalthor)
Code on Github : https://github.com/ajhalthor/interpreter/tree/master/GUI_Program
In the last video, we
1. installed flex and bison
2. installed syntax highlighting for flex and bison in sublime
3. Understood the code the code files
4. and got it running on a sample file.
That was the brains of the application. Now, we are going to create an interface where users can choose their sample file. This is gonna be using Java Swings. Some may think its a good choice. Others may disagree. I’ll be using other languages (like python) for future videos. But lets stick to java for now. I’ll first show you how to run this in the eclipse IDE. and then we’ll see how to do the same on the command line.
This project will have 3 classes:
SSMain — that contains the main function
MyFrame — creates the window frame, the file chooser and the rest of the UI
and Listener class— which listens to mouse events and perform the execution of the “output” file
Take the output file generated from the compilation of lex.yy.c and y.tab.c and put it in our project directory, on the sample level as the source folder. Before I move any further, I’ll just gloss over the 2 main classes here : MyFrame and Listener.
Myframe extends the JFrame class and inherits its properties and methods.
- JPanel to give a nice background to the app.
- JScrollPane will be used to display the error message.
- JLabel is used to display text. We will use it to display the full path of the sample file chosen, on the botton and other places where text is required.
- JButton is used to create the file chooser button.
In our constructor, we set the initial size of the window to be 500 by 500 pixels. We then instantiate all the elements I just discussed. In the createGUI method , the components are added and positioned on the screen. At the end of the method, we add a mouse listener for the JButton we created. The “listener” is actually the Listener Class, which I’ll explain now.
Listener handles 3 major mouse events.
a Mouse Click event
a Mouse Enter event
a mouse exit event
When the “Choose File” button is clicked, a file chooser opens up where the user selects their sample file. Once the file is chosen, we execute the typical output command that we use in the terminal in the last video . The response is stored in the message variable and displayed on the screen for the user.
setBorderColor method creates a red border and text if an error is displayed. Otherwise, the text and border color are both green. The other 2 mouse events are just used to give a hover effect on the “choose” button for a nice UI. And that all code for the GUI. Now, lets run thing with 4 sample files.
Sample_1 : ERROR on line 12: duplicate identifier ‘man’ found
Sample_2 : ERROR ON LINE 7 : syntax error, unexpected SEMI_COLON, expecting IDENTIFIER or ARRAY_IDENTIFIER
Sample_3 : ERROR ON LINE 1 : Invalid Token -.
Sample_4 : No Errors
And like that, we have a neat user interface to accompany our little interpreter.
If you want to execute this program on command line, go to your terminal and enter your project directory. and enter the src folder. Since our main class SSMain is in the package p, we compile it with the command
$ javac p/SSMain.java
This will generate the corresponding class files. Now, execute it with the command
$ java p.SSMain
And clearly, everything works the same even without eclipse. So, this will certainly come in useful for people who think eclipse, netbeans or any other java specific IDE is too slow.
Hope you guys liked this video and the others before this. If you jumped straight into this video without watching the others and ignored the link to the previous video at the beginning of this video….. then….. just go back and watch that last video. I’ve explained everything there.
In any case, Hope you guys liked this video! Please subscribe to my channel. Please. Pretty please. In my last video, I asked for 10 likes. This time, I’m gonna be greedy and ask for 13 likes. 13 come on! Lets hit that unlucky number! If we hit this number, I will give my 13th liker a year supply of —.