Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 3.84 KB  |  hits: 25  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.io.*;
  6. import java.util.StringTokenizer;
  7.  
  8. import javax.swing.*;
  9.  
  10. class JFileChooserExample1 extends JFrame implements ActionListener {
  11.         private JEditorPane jep = new JEditorPane();
  12.            public JFileChooserExample1() {
  13.               super("Editor");
  14.               Container cp = getContentPane( );
  15.               cp.add(new JScrollPane(jep));
  16.               cp.setLayout(new GridLayout(1,2));
  17.               JMenu menu = new JMenu("File");
  18.               menu.add(make("Open"));
  19.               menu.add(make("Quit"));
  20.               JMenuBar menuBar = new JMenuBar();
  21.               menuBar.add(menu); setJMenuBar(menuBar);
  22.               setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  23.               setSize(200,300);
  24.            }
  25.            
  26.            private JMenuItem make(String name) {
  27.                       JMenuItem m = new JMenuItem(name);
  28.                       m.addActionListener(this);
  29.                       return m;
  30.                    }
  31.  
  32.            public void actionPerformed(ActionEvent e) {
  33.               String ac = e.getActionCommand();
  34.               if(ac.equals("Open")) openFile();
  35.              
  36.               else if(ac.equals("Quit")) System.exit(0);
  37.            }
  38.  
  39.         public static void main(String[] args)  {
  40.                 //Creating two blank LinkedLists to put sorted and unsorted in it.
  41.                
  42.                 new JFileChooserExample1().setVisible(true);
  43.                
  44.                
  45.                
  46.                 //creating a  Measurement object to record the lengths. creating a buffer reader to read the the input file in the next line.
  47.                
  48.        
  49.                
  50.         }
  51.        
  52.         private void openFile() {
  53.                
  54.                
  55.               JFileChooser jfc = new JFileChooser();
  56.               int result = jfc.showOpenDialog(this);
  57.               if(result == JFileChooser.CANCEL_OPTION) return;
  58.               try {
  59.                  File file = jfc.getSelectedFile();
  60.                  LinkedList unsorted = new LinkedList();
  61.                         LinkedList sorted = new LinkedList();
  62.                        
  63.                         //codes to initialize the jframe windows. Will add the contenct later.
  64.                         JFrame myFrame = new JFrame();
  65.                         myFrame.setVisible(true);
  66.                         myFrame.setSize(400, 400);
  67.                         myFrame.setLocation(100, 100);
  68.                         myFrame.setTitle("Sorted and Unsorted Measurement");
  69.                         myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  70.                         JMenuItem item;
  71.                       JMenu fileMenu = new JMenu("File");
  72.                       FileMenuHandler fmh  = new FileMenuHandler(this);
  73.  
  74.                       item = new JMenuItem("Open");    //Open...
  75.                       item.addActionListener( fmh );
  76.                       fileMenu.add( item );
  77.                        
  78.                         //grid layout will separate the region in to two sides. creating 2 TextArea in them in the next lines.  
  79.                         myFrame.setLayout(new GridLayout(1,2));
  80.                         TextArea unsortedArea = new TextArea();
  81.                         TextArea sortedArea = new TextArea();
  82.                        
  83.                         Container Panel = myFrame.getContentPane();
  84.                         Panel.add(unsortedArea);
  85.                         Panel.add(sortedArea);
  86.                        
  87.                  Measurement m = new Measurement();    
  88.                  
  89.                  BufferedReader br = new BufferedReader(new FileReader(file));
  90.                  String token = br.readLine();
  91.                        
  92.                         //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.
  93.                         while (token != null)
  94.                         {
  95.                                
  96.                                 StringTokenizer myTokens = new StringTokenizer(token, "|");
  97.                                 int feet = Integer.parseInt(myTokens.nextToken());
  98.                                 float inches = Float.parseFloat(myTokens.nextToken());
  99.                                 m = new Measurement(feet, inches);
  100.                                 //each Measurement is then put in to the sorted and unsorted list. the sorted list will sort them from small to large automatically.
  101.                                 unsorted.append(m);
  102.                                 sorted.insert(m);
  103.                                 token = br.readLine();
  104.                         }
  105.                         //The 2 TextAreas then append the the sorted and unsorted list and then which then get added to the panel form jFrame.
  106.                         unsortedArea.append("Unsorted list \n" + unsorted);
  107.                         sortedArea.append("Sorted list \n" + sorted);
  108.                
  109.               } catch (Exception e) {
  110.                  JOptionPane.showMessageDialog(this,e.getMessage(),
  111.                  "File error",JOptionPane.ERROR_MESSAGE);}
  112.            }
  113.        
  114. }