Share on Google+Share on Google+

Display Sorted data in TextArea

Advertisement
In this section, you will learn how to display the sorted data into the textarea.

Display Sorted data in TextArea

By sorting, you can arrange the data into meaningful order so that you can analyze it more effectively. Here we have one file student.txt that contains the record of several students i.e id, name, marks and grade. The data is not properly arranged in the file. Now we have to display all the file data in a sorted way into the text area according to the id of the student. So for this, we have used Comparable class that compares all the data with respect to id and using the Arrays.sort() method, the data will get sorted. Then using the ArrayList and Iterator class, we have added this data to the text area and when the user clicks the VIEW button, the whole data will get displayed on the text area in a sorted way.

Here is the code:

import java.io.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class ShowData implements Comparable {
	int id;
	String name;
	int marks;
	String grade;

	public void setId(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setMarks(int marks) {
		this.marks = marks;
	}

	public int getMarks() {
		return marks;
	}

	public void setGrade(String grade) {
		this.grade = grade;
	}

	public String getGrade() {
		return grade;
	}

	public int compareTo(Object Student) throws ClassCastException {
		if (!(Student instanceof ShowData))
			throw new ClassCastException("Error");
		int ide = ((ShowData) Student).getId();
		return this.id - ide;
	}
}

public class SortFileData {
	SortFileData() {
		JFrame f = new JFrame();
		JPanel p = new JPanel();
		JButton b = new JButton("View");
		final JTextArea area = new JTextArea(8, 20);
		JScrollPane pane = new JScrollPane(area);

		p.add(pane);
		p.add(b);
		f.add(p);
		f.setVisible(true);
		f.pack();

		b.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				int j = 0;
				ShowData data[] = new ShowData[5];
				try {
					FileInputStream fstream = new FileInputStream(
							"C:\\school.txt");
					DataInputStream in = new DataInputStream(fstream);
					BufferedReader br = new BufferedReader(
							new InputStreamReader(in));
					String strLine;
					ArrayList list = new ArrayList();
					while ((strLine = br.readLine()) != null) {
						list.add(strLine);
					}
					Iterator itr;
					for (itr = list.iterator(); itr.hasNext();) {
						String str = itr.next().toString();
						String[] splitSt = str.split(" ");
						String id = "", name = "", marks = "", grade = "";
						for (int i = 0; i < splitSt.length; i++) {
							id = splitSt[0];
							name = splitSt[1];
							marks = splitSt[2];
							grade = splitSt[3];

						}
						data[j] = new ShowData();
						data[j].setId(Integer.parseInt(id));
						data[j].setName(name);
						data[j].setMarks(Integer.parseInt(marks));
						data[j].setGrade(grade);
						j++;
					}
				} catch (Exception e) {
				}
				Arrays.sort(data);
				System.out.println("********Sorted by id********");
				String strVal = "";
				for (int i = 0; i < 5; i++) {
					ShowData show = data[i];
					int no = show.getId();
					String name = show.getName();
					int marks = show.getMarks();
					String grade = show.getGrade();
					System.out.println(no + " " + name + " " + marks + " "
							+ grade);
					String d = no + "  " + name + "  " + marks + "  " + grade;
					ArrayList al = new ArrayList();
					al.add(d + "\n");

					Iterator itr = al.iterator();
					while (itr.hasNext()) {
						strVal += itr.next().toString() + " ";
					}
				}
				area.setText(strVal);
			}
		});
	}

	public static void main(String[] args) {
		SortFileData data = new SortFileData();

	}
}

Output:

Advertisements

Advertisement

Posted on: May 14, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.