Swing is a part of the JFC (Java Foundation Classes). Building Graphical User Interface in Java requires the use of Swings. Swing Framework contains a large set of components which allow a high level of customization and provide rich functionalities, and is used to create window-based applications. Java swing components are lightweight, platform-independent, provide powerful components like tables, scroll panels, buttons, list, colour chooser, etc.
In this article, we will see how to write the students information in a Jframe and store it in a file.
Approach: To solve this problem, the following steps are followed:
- First, we need to create a frame using JFrame.
- Next create JLabels, JTextFields, JComboBoxes, JButtons and set their bounds respectively.
- Name these components accordingly and set their bounds.
- Now, in order to save the data into the text file on button click, we need to add Event Handlers. In this case, we will add ActionListener to perform an action method known as actionPerformed in which first we need to get the values from the text fields which is default as a “string”.
- Finally, the Jbuttons, JLabels, JTextFields and JComboBoxes are added to the JFrame and the text is stored in a text file.
Below is the implementation of the above approach:
- The window displayed on running the program:
- Entering the data:
- The dialog box showed after clicking on the save button:
- The text file in which the data is stored:
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.