- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.FocusEvent;
- import java.awt.event.FocusListener;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import java.io.BufferedReader;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.URL;
- import java.net.URLConnection;
- import java.util.ArrayList;
- import javax.swing.JButton;
- import javax.swing.JComboBox;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JTabbedPane;
- import javax.swing.JTable;
- import javax.swing.table.DefaultTableModel;
- public class stockTicker extends JFrame
- {
- JTabbedPane tabbedPane;
- JPanel panel1;
- JPanel panel2;
- JPanel panel3;
- JPanel panel4;
- JPanel panel5;
- JLabel read1;
- JLabel read2;
- JLabel read3;
- JLabel read4;
- JButton butt1;
- JButton butt2;
- JComboBox ipList = new JComboBox();
- ArrayList<Object> Stock = new ArrayList<Object>();
- String name, symbol;
- public stockTicker(String n1, String s1) {
- name = n1;
- symbol = s1;
- }
- public String toString() {
- return name;
- }
- //TableModel
- String[] columnNames = {"Stock Name",
- "Last Price",
- "Change",
- "Volume",};
- Object[][] stock= null;
- DefaultTableModel model = new DefaultTableModel(stock, columnNames);
- // final JTable table = new JTable(data, columnNames);
- final JTable table = new JTable(model);
- JScrollPane jsp = new JScrollPane(table);
- public stockTicker()
- {
- // NOTE: to reduce the amount of code in this example, it uses
- // panels with a NULL layout. This is NOT suitable for
- // production code since it may not display correctly for
- // a look-and-feel.
- table.setPreferredScrollableViewportSize(new Dimension(500, 100));
- setTitle( "Stock Ticker" );
- setSize( 500, 300 );
- setBackground( Color.gray );
- JPanel topPanel = new JPanel();
- topPanel.setLayout( new BorderLayout() );
- getContentPane().add( topPanel );
- // Create the tab pages
- createPage1();
- createPage2();
- // Create a tabbed pane
- tabbedPane = new JTabbedPane();
- tabbedPane.addTab( "Stocks", panel1);
- tabbedPane.addTab( "Search", panel4 );
- topPanel.add( tabbedPane, BorderLayout.CENTER);
- String line = null;
- try
- {
- FileInputStream fis = new FileInputStream(
- ("C:/stock.txt"));
- InputStreamReader isr = new InputStreamReader(fis);
- BufferedReader input = new BufferedReader(isr);
- while ((line = input.readLine()) != null){
- String[] StockInfo = line.split(",");
- stockTicker myitem = new stockTicker(StockInfo[0],StockInfo[1]);
- ipList.addItem(myitem);
- ipList.setEditable(true);
- }
- }
- catch(FileNotFoundException FNF){
- }
- catch(IOException E)
- {
- }
- }
- public void createPage1()
- {
- panel1 = new JPanel();
- panel1.setLayout(new GridLayout(1,0) );
- panel1.add(jsp );
- }
- public void createPage2()
- {
- panel4 = new JPanel();
- panel4.setLayout(new BorderLayout() );
- panel5 = new JPanel();
- panel5.add(ipList);
- panel4.add(panel5);
- /*ipList.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
- public void keyTyped(KeyEvent e) {
- ipList_keyTyped(e);
- }
- public void keyPressed(KeyEvent e) {
- ipList_keyPressed(e);
- }
- public void keyReleased(KeyEvent e) {
- ipList_keyReleased(e);
- if (ipList.isDisplayable()) ipList.setPopupVisible(true);
- }
- });*/
- /*ipList.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
- public void keyTyped(KeyEvent e) {
- ArrayList input = new ArrayList();
- input.add(new stockTicker( name, symbol));
- for (stockTicker s : input) {
- if (ipList.equals(s.getName())) {
- ipList.setPopupVisible(true);
- }
- }
- if (ipList.isDisplayable()) ipList.setPopupVisible(true);
- }
- });*/
- ipList.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
- public void keyTyped(KeyEvent e) {
- if (ipList.isDisplayable()) ipList.setPopupVisible(true);
- }
- });
- ipList.getEditor().getEditorComponent().addFocusListener(new FocusListener()
- { public void focusGained(FocusEvent ef)
- { ipList.setPopupVisible(true);
- }
- public void focusLost(FocusEvent ef){}
- });
- ipList.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String line = null;
- try {
- String input1 = ((stockTicker) ipList.getSelectedItem()).symbol;
- URL url = new URL("http://download.finance.yahoo.com/d/quotes.csv?s="+input1+"&f=sl1d1t1c1ohgv&e=.csv");
- URLConnection connection = url.openConnection();
- connection.setDoInput(true);
- InputStream inStream = connection.getInputStream();
- BufferedReader input =
- new BufferedReader(new InputStreamReader(inStream));
- while ((line = input.readLine()) != null){
- String[] yahooStockInfo = line.split(",");
- model.addRow(new Object[]{yahooStockInfo[0], yahooStockInfo[1], yahooStockInfo[4], yahooStockInfo[8]});
- // data[1].add(yahooStockInfo[0]);
- //data[][].setText(yahooStockInfo[1]);
- //data.setText(yahooStockInfo[4]);
- //data.setText(yahooStockInfo[8]);
- }
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- });
- }
- /*protected void ipList_keyReleased(KeyEvent e) {
- // TODO Auto-generated method stub
- }
- protected void ipList_keyPressed(KeyEvent e) {
- // TODO Auto-generated method stub
- }
- protected void ipList_keyTyped(KeyEvent e) {
- // TODO Auto-generated method stub
- }*/
- // Main method to get things started
- public static void main( String args[] )
- {
- // Create an instance of the test application
- stockTicker mainFrame = new stockTicker();
- mainFrame.setVisible( true );
- }
- }