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

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 6.87 KB  |  hits: 15  |  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. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4.  
  5. import java.awt.GridLayout;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.awt.event.FocusEvent;
  9. import java.awt.event.FocusListener;
  10. import java.awt.event.KeyAdapter;
  11. import java.awt.event.KeyEvent;
  12.  
  13. import java.io.BufferedReader;
  14. import java.io.FileInputStream;
  15. import java.io.FileNotFoundException;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.io.InputStreamReader;
  19. import java.net.URL;
  20. import java.net.URLConnection;
  21. import java.util.ArrayList;
  22.  
  23.  
  24. import javax.swing.JButton;
  25. import javax.swing.JComboBox;
  26. import javax.swing.JFrame;
  27. import javax.swing.JLabel;
  28. import javax.swing.JPanel;
  29. import javax.swing.JScrollPane;
  30. import javax.swing.JTabbedPane;
  31. import javax.swing.JTable;
  32.  
  33. import javax.swing.table.DefaultTableModel;
  34.  
  35.  
  36.  
  37. public class stockTicker extends JFrame
  38. {
  39. JTabbedPane tabbedPane;
  40.                         JPanel          panel1;
  41.                         JPanel          panel2;
  42.                         JPanel          panel3;
  43.                         JPanel          panel4;
  44.                         JPanel          panel5;
  45.                         JLabel          read1;
  46.                         JLabel          read2;
  47.                         JLabel          read3;
  48.                         JLabel          read4;
  49.                        
  50.                         JButton         butt1;
  51.                         JButton         butt2;
  52.                        
  53.                         JComboBox ipList = new JComboBox();
  54.                        
  55.                 ArrayList<Object> Stock = new ArrayList<Object>();
  56.                 String name, symbol;
  57.                
  58.                 public stockTicker(String n1, String s1) {
  59.                     name = n1;
  60.                     symbol = s1;
  61.                 }
  62.                 public String toString() {
  63.                    return name;
  64.                 }
  65.  
  66.                 //TableModel
  67.                 String[] columnNames = {"Stock Name",
  68.                                 "Last Price",
  69.                                 "Change",
  70.                                 "Volume",};
  71.                 Object[][] stock= null;
  72.  
  73.                                 DefaultTableModel model = new DefaultTableModel(stock, columnNames);
  74.                                 // final JTable table = new JTable(data, columnNames);
  75.                                 final JTable table = new JTable(model);
  76.                                 JScrollPane jsp = new JScrollPane(table);
  77.                                
  78. public stockTicker()
  79. {
  80.         // NOTE: to reduce the amount of code in this example, it uses
  81.         // panels with a NULL layout.  This is NOT suitable for
  82.         // production code since it may not display correctly for
  83.         // a look-and-feel.
  84.        
  85.  
  86.          table.setPreferredScrollableViewportSize(new Dimension(500, 100));
  87.         setTitle( "Stock Ticker" );
  88.         setSize( 500, 300 );
  89.         setBackground( Color.gray );
  90.  
  91.         JPanel topPanel = new JPanel();
  92.         topPanel.setLayout( new BorderLayout() );
  93.         getContentPane().add( topPanel );
  94.  
  95.         // Create the tab pages
  96.         createPage1();
  97.         createPage2();
  98.        
  99.  
  100.         // Create a tabbed pane
  101.         tabbedPane = new JTabbedPane();
  102.        
  103.         tabbedPane.addTab( "Stocks", panel1);
  104.         tabbedPane.addTab( "Search", panel4  );
  105.        
  106.         topPanel.add( tabbedPane, BorderLayout.CENTER);
  107.        
  108.  
  109.     String line = null;
  110.   try
  111. {
  112.   FileInputStream fis = new FileInputStream(
  113.                   ("C:/stock.txt"));
  114.   InputStreamReader isr = new InputStreamReader(fis);
  115.   BufferedReader input = new BufferedReader(isr);
  116.   while ((line = input.readLine()) != null){
  117.           String[] StockInfo = line.split(",");  
  118.           stockTicker myitem = new stockTicker(StockInfo[0],StockInfo[1]);
  119.           ipList.addItem(myitem);
  120.           ipList.setEditable(true);
  121.          
  122.  
  123.   }      
  124. }
  125.  
  126. catch(FileNotFoundException FNF){
  127. }
  128. catch(IOException E)
  129. {
  130. }
  131.        
  132.        
  133. }
  134.  
  135. public void createPage1()
  136. {
  137.         panel1 = new JPanel();
  138.         panel1.setLayout(new GridLayout(1,0) );
  139.        
  140.  
  141.         panel1.add(jsp );
  142.        
  143.  
  144. }
  145.  
  146.        
  147. public void createPage2()
  148. {
  149.         panel4 = new JPanel();
  150.         panel4.setLayout(new BorderLayout() );
  151.        
  152.         panel5 = new JPanel();
  153.         panel5.add(ipList);
  154.         panel4.add(panel5);
  155.        
  156.         /*ipList.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
  157.                 public void keyTyped(KeyEvent e) {
  158.                         ipList_keyTyped(e);
  159.                 }
  160.                 public void keyPressed(KeyEvent e) {
  161.                         ipList_keyPressed(e);
  162.                 }
  163.                 public void keyReleased(KeyEvent e) {
  164.                         ipList_keyReleased(e);
  165.                
  166.                 if (ipList.isDisplayable()) ipList.setPopupVisible(true);
  167.         }
  168.                 });*/
  169.        
  170.         /*ipList.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
  171.         public void keyTyped(KeyEvent e) {
  172.                 ArrayList input = new ArrayList();
  173.                 input.add(new stockTicker( name, symbol));
  174.                  for (stockTicker s : input) {
  175.                          if (ipList.equals(s.getName())) {
  176.                                  ipList.setPopupVisible(true);
  177.                          }
  178.                  }
  179.                  
  180.                  if (ipList.isDisplayable()) ipList.setPopupVisible(true);
  181.         }
  182.     });*/
  183.         ipList.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
  184.         public void keyTyped(KeyEvent e) {
  185.                 if (ipList.isDisplayable()) ipList.setPopupVisible(true);
  186.         }
  187.     });
  188.        
  189.           ipList.getEditor().getEditorComponent().addFocusListener(new FocusListener()
  190.       {         public void focusGained(FocusEvent ef)
  191.                       { ipList.setPopupVisible(true);
  192.                       }
  193.                       public void focusLost(FocusEvent ef){}
  194.               });
  195.        
  196.  
  197.          
  198.  ipList.addActionListener(new ActionListener() {
  199.          public void actionPerformed(ActionEvent e) {
  200.                 String line = null;
  201.                  try {                                                  
  202.                         String input1 = ((stockTicker) ipList.getSelectedItem()).symbol;
  203.                        
  204.                          URL url = new URL("http://download.finance.yahoo.com/d/quotes.csv?s="+input1+"&f=sl1d1t1c1ohgv&e=.csv");
  205.                          URLConnection connection = url.openConnection();
  206.                             connection.setDoInput(true);
  207.                             InputStream inStream = connection.getInputStream();
  208.                             BufferedReader input =
  209.                             new BufferedReader(new InputStreamReader(inStream));
  210.                             while ((line = input.readLine()) != null){
  211.                                 String[] yahooStockInfo = line.split(",");
  212.                                 model.addRow(new Object[]{yahooStockInfo[0], yahooStockInfo[1], yahooStockInfo[4], yahooStockInfo[8]});
  213.                             //  data[1].add(yahooStockInfo[0]);
  214.                                                 //data[][].setText(yahooStockInfo[1]);
  215.                                                 //data.setText(yahooStockInfo[4]);
  216.                                                 //data.setText(yahooStockInfo[8]);
  217.                                  
  218.                        
  219.                        
  220.  
  221.                          }
  222.                                 } catch (IOException ex) {
  223.                                         ex.printStackTrace();
  224.  
  225.                                 }
  226.                         }
  227.                 });                
  228.                      
  229.  
  230.                
  231.  
  232.  
  233. }
  234. /*protected void ipList_keyReleased(KeyEvent e) {
  235.         // TODO Auto-generated method stub
  236.        
  237. }
  238. protected void ipList_keyPressed(KeyEvent e) {
  239.         // TODO Auto-generated method stub
  240.        
  241. }
  242. protected void ipList_keyTyped(KeyEvent e) {
  243.         // TODO Auto-generated method stub
  244.        
  245. }*/
  246. // Main method to get things started
  247. public static void main( String args[] )
  248. {
  249.         // Create an instance of the test application
  250.         stockTicker mainFrame   = new stockTicker();
  251.         mainFrame.setVisible( true );
  252. }
  253. }