- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import java.util.StringTokenizer;
- import javax.swing.*;
- class JFileChooserExample1 extends JFrame implements ActionListener {
- private JEditorPane jep = new JEditorPane();
- public JFileChooserExample1() {
- super("Editor");
- Container cp = getContentPane( );
- cp.add(new JScrollPane(jep));
- cp.setLayout(new GridLayout(1,2));
- JMenu menu = new JMenu("File");
- menu.add(make("Open"));
- menu.add(make("Quit"));
- JMenuBar menuBar = new JMenuBar();
- menuBar.add(menu); setJMenuBar(menuBar);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setSize(200,300);
- }
- private JMenuItem make(String name) {
- JMenuItem m = new JMenuItem(name);
- m.addActionListener(this);
- return m;
- }
- public void actionPerformed(ActionEvent e) {
- String ac = e.getActionCommand();
- if(ac.equals("Open")) openFile();
- else if(ac.equals("Quit")) System.exit(0);
- }
- public static void main(String[] args) {
- //Creating two blank LinkedLists to put sorted and unsorted in it.
- new JFileChooserExample1().setVisible(true);
- //creating a Measurement object to record the lengths. creating a buffer reader to read the the input file in the next line.
- }
- private void openFile() {
- JFileChooser jfc = new JFileChooser();
- int result = jfc.showOpenDialog(this);
- if(result == JFileChooser.CANCEL_OPTION) return;
- try {
- File file = jfc.getSelectedFile();
- LinkedList unsorted = new LinkedList();
- LinkedList sorted = new LinkedList();
- //codes to initialize the jframe windows. Will add the contenct later.
- JFrame myFrame = new JFrame();
- myFrame.setVisible(true);
- myFrame.setSize(400, 400);
- myFrame.setLocation(100, 100);
- myFrame.setTitle("Sorted and Unsorted Measurement");
- myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- JMenuItem item;
- JMenu fileMenu = new JMenu("File");
- FileMenuHandler fmh = new FileMenuHandler(this);
- item = new JMenuItem("Open"); //Open...
- item.addActionListener( fmh );
- fileMenu.add( item );
- //grid layout will separate the region in to two sides. creating 2 TextArea in them in the next lines.
- myFrame.setLayout(new GridLayout(1,2));
- TextArea unsortedArea = new TextArea();
- TextArea sortedArea = new TextArea();
- Container Panel = myFrame.getContentPane();
- Panel.add(unsortedArea);
- Panel.add(sortedArea);
- Measurement m = new Measurement();
- BufferedReader br = new BufferedReader(new FileReader(file));
- String token = br.readLine();
- //the while loop will read each line and separate by "|" and then put them into Measurement class after separating inch and feet from each other.
- while (token != null)
- {
- StringTokenizer myTokens = new StringTokenizer(token, "|");
- int feet = Integer.parseInt(myTokens.nextToken());
- float inches = Float.parseFloat(myTokens.nextToken());
- m = new Measurement(feet, inches);
- //each Measurement is then put in to the sorted and unsorted list. the sorted list will sort them from small to large automatically.
- unsorted.append(m);
- sorted.insert(m);
- token = br.readLine();
- }
- //The 2 TextAreas then append the the sorted and unsorted list and then which then get added to the panel form jFrame.
- unsortedArea.append("Unsorted list \n" + unsorted);
- sortedArea.append("Sorted list \n" + sorted);
- } catch (Exception e) {
- JOptionPane.showMessageDialog(this,e.getMessage(),
- "File error",JOptionPane.ERROR_MESSAGE);}
- }
- }