Hi everyone, so today I am going to tap into the programmer in me and guide you as you create your first working windows application using java. We would be creating a “Text pad (notepad)”.
REQUIREMENTS.
A laptop with net beans /ellipse installed on it (but for the sake of this tutorial, I would be using net beans).
Next select “newJFrameform”
// import javax.swing.JFrame;//
// textpadgui frame = new textpadgui();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);” //
(be mindful of all the signs and symbols. // is to guide you on when the code begins and where it ends.)
Your main class is ready now.
Drag a “textfield” from the second group of icons into the GUI, right below the menu bar. Adjust it to fit the width of the GUI, Rename the textfield to “statusbar” by right clicking and changing ONLY the “variable name”.
Drag a “textarea” from the second group of the icons into the GUI too. Adjust it to fit the remaining area of the GUI, rename the “textarea” to “text” by right clicking and changing ONLY the “variable name”.
(All code descriptions are in the pictures that would be shown below)
Input these line of code.
// *text.setText("");* //
Test: test if the new button is working already by right clicking “mytextpad” icon and selecting “run file”, wait for it to run and then write something in the text area and click the new menu and see if it works, a fresh page is supposed to show up.
// JFileChooser saveChooser = new JFileChooser();
int chooserValue = saveChooser.showSaveDialog(this);
if( chooserValue == JFileChooser.APPROVE_OPTION){
try {
PrintWriter fout= new PrintWriter( saveChooser.getSelectedFile());
fout.print(text.getText());
JOptionPane.showMessageDialog(null, "Your file has been saved successfully","Save?",JOptionPane.INFORMATION_MESSAGE);
fout.close();
statusbar.setText("saved" + saveChooser. getSelectedFile().getAbsolutePath());
} catch (FileNotFoundException ex) {
}” //
These lines of code simply enables you to keep the file in a desired place on your laptop and displays the location on the status bar.
Test: save the file using CTRL+S and test to see if the save menu works by running the file and try saving a document, see if it works like the picture below.
//JFileChooser openChooser= new JFileChooser(); // instance of filechooser
int chooserValue = openChooser.showOpenDialog(this);
if( chooserValue == JFileChooser.APPROVE_OPTION){
try {
Scanner fin = new Scanner(openChooser.getSelectedFile());
String buffer = "";
while(fin.hasNext())
{
buffer += fin.nextLine() +'\n';
}
TEXT.setText(buffer);
fin.close();
STATUSBAR.setText("loaded" + openChooser. getSelectedFile().getAbsolutePath());
}
catch (FileNotFoundException ex)
{
JOptionPane.showMessageDialog(null, "FILE NOT FOUND!!!","MISSING FILE",JOptionPane.INFORMATION_MESSAGE);
}
}
}” //
Test: save the file using CTRL+S and test to see if the open menu works by running the file and opening the file you saved before in the save test
Test: save the file using CTRL+S and test to see if the exit menu works by running the file and checking if the application closes on click.
Now run your program, the picture below should be the end result. Test all your icons to be sure they are all working.
So that’s it. You just created a simple TEXT PAD windows application.
NOTE:
If you have any question, please feel free to ask in the comment box.