Write The Contents of a JFrame to a Text File and Display Recorded Data to a JTable

By this tutorial you can learn how to write the contents of a JFrame(JTextField, JTextArea, JComboBox, JRadioButton, JCheckBox) to a text file and also display the data to a JTable or JTextArea. You can also learn the use of JFileChooser.

The following method retrieves all records form JFrame components:

public void getAllRecord() {
        _fn = txtName.getText();
        _add = txtAdd.getText();
        _coun = cmbCountry.getSelectedItem().toString();
        if (rdMale.isSelected()) {
            _gender = "Male";
        } else {
            _gender = "Female";
        }

        //get value from Checkbox   
        for (int i = 0; i < pnlDegree.getComponentCount(); i++) {
            JCheckBox checkBox = (JCheckBox) pnlDegree.getComponent(i);
            if (checkBox.isSelected()) {
                _degree += checkBox.getText();
                _degree += gap;
            }
        }

    }

The writeAll() method writes the records to a text file

private void writeAll() {

        BufferedWriter buf;
        try {
            buf = new BufferedWriter(new FileWriter(file, true));
            buf.write(_fn + "," + _add + "," + _coun + "," + _degree + "," + _gender);
            buf.newLine();
            buf.close();
            JOptionPane.showMessageDialog(this, "Data Saved");

        } catch (Exception e) {
        }
    }

To display the records to a JTextArea use the following method.

private void displayAll() {
        FileInputStream fobj = null;
        try {
            String str4 = "";
            File f = new File("d://info.txt");
            fobj = new FileInputStream(f);
            int len = (int) f.length();
            for (int j = 0; j < len; j++) {
                char str5 = 0;
                try {
                    str5 = (char) fobj.read();
                    //System.out.println(str5);
                    if (str5 == ',') {
                        str5 = '\t';
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                str4 = str4 + str5;
            }
            txtShow.setText(str4);
            fobj.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

You can display the records to a JTable by using the following method.

private void displayinTable() {

        InputStream is;

        try {
            File f = new File("d://info.txt");
            is = new FileInputStream(f);
            Scanner scan = new Scanner(is);
            String[] array;
            while (scan.hasNextLine()) {
                String line = scan.nextLine();
                if (line.indexOf(",") > -1) {
                    array = line.split(",");
                } else {
                    array = line.split("\n");
                }
                Object[] data = new Object[array.length];
                for (int i = 0; i < array.length; i++) {
                    data[i] = array[i];
                }

                model.addRow(data);
            }


        } catch (Exception e) {
        }

    }

To clear the form data use the clear() method.

public void clear() {
        //Clear all text field
        txtName.setText("");
        txtAdd.setText("");
        //set combo box to select first value
        cmbCountry.setSelectedIndex(0);
        //clearing variavle degree so that it can take new value each time
        _degree = "";
        //deselecting all radio
        gender.clearSelection();
        //deselecting all check box
        for (int i = 0; i < pnlDegree.getComponentCount(); i++) {
            JCheckBox checkBox = (JCheckBox) pnlDegree.getComponent(i);
            if (checkBox.isSelected()) {
                checkBox.setSelected(false);
            }
        }
        //System.out.println(tblStudent.getRowCount());
        // remove all row from table
        if (tblStudent.getRowCount() > 0) {
            for (int i = tblStudent.getRowCount() - 1; i > -1; i--) {
                model.removeRow(i);
            }
        }
        txtShow.setText("");
    }

Screenshot:
Student_info

 

admin

Hello! I am Md. Abdul Bari; founder and admin of javaknowledge; currently providing training as a J2EE faculty in The Computers Ltd. Dhaka, Bangladesh under an IT Scholarship project provided by IDB-BISEW. I am a self learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.

More Posts - Website

Follow Me:
TwitterFacebookGoogle Plus

11,055` Total Views

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *


+ 8 = fourteen