Pratheeba
Posts: 11
Nickname: pratheeba
Registered: Sep, 2003
|
|
Re: method that is not working inside thread
|
Posted: Nov 14, 2003 7:53 AM
|
|
hi, Here is my code
public class UserInterface extends Frame { public DefaultTableModel tableModel; JScrollPane tablePane; public JTable table; String catTableName,catTableDescription; Vector columnNames; Vector catData,catAllData; Vector vecCatData; JScrollPane tableScroll; public String findTicker,input; Client c; Socket s; HashMap allTicker = new HashMap(); int rowCountdelete ; /** Constructor */ public UserInterface() { setLayout(new BorderLayout()); Vector vecStockdata = new Vector(); Vector columnNames = new Vector(); String col1 ="Name"; String col2 ="Symbol"; String col3 ="Trade Market"; String col4 ="Last Trade Value"; String col5 ="52 weeks low"; String col6 ="52 weeks high"; columnNames.add(col1); columnNames.add(col2); columnNames.add(col3); columnNames.add(col4); columnNames.add(col5); columnNames.add(col6); int rowCount = 0; int columnCount = 6; tableModel =new DefaultTableModel(vecCatData,columnNames); table = new JTable(tableModel); TableColumn column = null; for(int i = 0; i < 6; i++) { column = table.getColumnModel().getColumn(i); if(i==0) { column.setPreferredWidth(200); } else if(i==1) { column.setPreferredWidth(30); } else { column.setPreferredWidth(50); } } table.setPreferredScrollableViewportSize(new Dimension(900, 383)); tableScroll = new JScrollPane(table); final JRadioButton addButton = new JRadioButton("Get"); final JRadioButton deleteButton = new JRadioButton("Delete"); JPanel radioPanel = new JPanel(new FlowLayout()); radioPanel.add(addButton); radioPanel.add(deleteButton); add(radioPanel, BorderLayout.SOUTH); add(tableScroll,BorderLayout.CENTER);
public synchronized void getAllTableValue() { System.out.println("Coming inside method..."); int i; Object tickRef = null; Socket tableSocket = null; int rowCount; rowCount = table.getRowCount(); System.out.println("THE ROWCOUNT " + rowCount); System.out.println("COUNT " + tableModel.getRowCount()); for(i=0;i<rowCount ;i++) { tickRef = table.getValueAt(i, 1); String tickerRefresh = tickRef.toString(); System.out.println("Ticker for refreshing is " + i + "--" + tickerRefresh); StockInfo sinfo = new StockInfo(tickerRefresh); try{ tableSocket =new Socket(InetAddress.getLocalHost(),1167); OutputStream ostable = (tableSocket.getOutputStream()); ObjectOutputStream oostable = new ObjectOutputStream(ostable); oostable.writeObject(tickerRefresh); InputStream istable = tableSocket.getInputStream(); ObjectInputStream iostable = new ObjectInputStream(istable); sinfo = (StockInfo) iostable.readObject(); table.setValueAt(sinfo.getStockName(),i,0); table.setValueAt(sinfo.getTicker(),i,1); table.setValueAt(sinfo.getTradeMarket(),i,2); table.setValueAt(sinfo.getLastTradeValue(),i,3); table.setValueAt(sinfo.get52WeekLow(),i,4); table.setValueAt(sinfo.get52WeekHigh(),i,5); System.out.println("Table refreshed successfully..."); } catch(UnknownHostException ue) { System.out.println("I can't find host"); } catch(Exception ee) { System.out.println("Caught in exception"); System.out.println(ee.getMessage()); } } } }
My Thread class code:
public class OurTableThread extends Thread implements Serializable {
UserInterface uitable ; public OurTableThread(){}
public OurTableThread(UserInterface uitable) { this.uitable = uitable; } public synchronized void run(){ while(true) { refresh(); } } public synchronized void refresh() { try{ uitable.getAllTableValues(); sleep(90000); } catch(InterruptedException ei){} catch(NullPointerException ni){} } }
My Main program in which i am calling those to class:
public class OurClient { public static void main(String[] args) { UserInterface ui = new UserInterface(); new OurTableThread(ui).start(); ui.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); ui.setSize(900, 300); ui.setVisible(true); ui.setTitle("Quote Server"); } }
Here i pasted my part of UserInterface class and Main class...I can't put full code... I hope this will be understandable...Thanks.Expecting reply...
|
|