2

In a program I have been working on, I need to refresh a table with new data. Currently, since I am new to GUI's, I was going to refresh the entire GUI with a new table. My program does this, but it ends up building underneath the previous GUI.

To continue the project and move on until I can rebuild, I would like to do one of two things. Either invalidate the previous GUI and use the new one, or edit the table model. Right now, I am using the DefaultTableModel since I constructed with JTable. The three key files are

AppGUI.java

//compile javac -cp ;./jsoup-1.6.3.jar;commons-logging-1.1.1.jar;commons-httpclient-3.0.1.jar;commons-codec-1.6.jar;httpclient-4.2.jar AppGUI.java

public class AppGUI extends JFrame{

public AppGUI(Company company)throws Exception {

        retrieveGUI(company);
}

public void retrieveGUI(Company company){
        this.company = company;
        incomeStatementPane = new IncomeStatementPane();
        balanceSheetPane = new BalanceSheetPane();
        cashFlowsPane = new CashFlowsPane();

        JTabbedPane leftTabbedPane = new JTabbedPane();             //Left Pane
        JComponent panel1 = incomeStatementPane.render(company);            //Income Statement 
        leftTabbedPane.addTab("Income Statement", panel1);
        leftTabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
        JComponent panel2 = balanceSheetPane.render(company);               //Balance Sheet PAnel
        leftTabbedPane.addTab("Balance Sheet", panel2);
        leftTabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
        JComponent panel3 = cashFlowsPane.render(company);                  //Cash Flows Panel
        leftTabbedPane.addTab("Cash Flows", panel3);
        leftTabbedPane.setMnemonicAt(2, KeyEvent.VK_3);

        JTabbedPane rightTabbedPane = new JTabbedPane();            //Right Pane
        JComponent panel4 = LiquidityPane.render(company);                  //Liquidity
        rightTabbedPane.addTab("Liquidity", panel4);
        //ratioTabbedPane.setMnemonicAt(3, KeyEvent.VK_4);
        JComponent panel5 = ProfitabilityPane.render(company);              //Profitability 
        rightTabbedPane.addTab("Profitability", panel5);
        //ratioTabbedPane.setMnemonicAt(4, KeyEvent.VK_5);
        JComponent panel6 = LongTermSolvencyPane.render(company);           //Long Term Solvency
        rightTabbedPane.addTab("Long Term Solvency", panel6);
        //ratioTabbedPane.setMnemonicAt(5, KeyEvent.VK_6);
        JComponent panel7 = CashFlowAdequacyPane.render(company);           //Cash Flow 
        rightTabbedPane.addTab("Cash Flow", panel7);

        add(rightTabbedPane, BorderLayout.CENTER);
        add(leftTabbedPane, BorderLayout.WEST);
        JPanel bottomPanel = new JPanel();
        bottomPanel.setBackground(new Color(150, 150, 150));

        JButton computeBtn = new JButton("Compute Ratios");
        //ActionListener computeBtnListener = new computeListener();        //computebutonlistener
        //computeBtn.addActionListener(computeBtnListener);     //actionlistener

        //compute()
        bottomPanel.add(computeBtn, BorderLayout.EAST);

        JPanel tickerPanel = new JPanel();
        JLabel tickerLabel = new JLabel("Search Ticker:");
        final JTextField tickerField = new JTextField(10);


        JButton searchBtn = new JButton("Search");
        searchBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                final String newTicker = tickerField.getText();
            try{
            refresh(tickerField.getText());
            }
            catch(Exception ae){}
            }
        });



        tickerPanel.add(tickerLabel/*, BorderLayout.WEST*/);
        tickerPanel.add(tickerField/*, BorderLayout.WEST*/);
        tickerPanel.add(searchBtn);
        add(tickerPanel, BorderLayout.NORTH);
        add(bottomPanel, BorderLayout.SOUTH);


}

public void setCompany(Company company) {
    this.company = company;
}

// Enter new symbol.
// compnay 

public void refresh(String newTicker)throws IOException{
Company newCompany = new Company(newTicker);  // Creates new Company. Updating methods are called from constructor automatically.
//System.out.println(newTicker);
//frame.invalidate();
//frame.validate();

retrieveGUI(newCompany);     // Stuff from contstructor.    
}

//public void retrieveNewData(String newTicker){
//Company newCompany = new Company(newTicker);

//}



private Company company;//company from ticker JLabel
private IncomeStatementPane incomeStatementPane;
private BalanceSheetPane balanceSheetPane;
private CashFlowsPane cashFlowsPane;

    public static void main(String[] args) throws Exception{
        Company company = new Company("FTK"); // Creates new Company. Updating methods are called from constructor automatically.
        AppGUI frame = new AppGUI(company); // Creates new App GUI.  Various panes are initialized from constructor.
        frame.retrieveGUI(company);
        frame.setTitle("Financial Calculator | Ratios");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setMinimumSize(new Dimension(1000, 500));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

//      public compute(){
//          refresh();
//          render();
//      }

}

Company.java

public class Company {
//Variables and Objects

String Ticker;

private Object[][] incomeStatementInfo = new String[52][11];
private Object[][] balanceSheetInfo = new String [46][11];
private Object[][] cashFlowsInfo = new String [22][11];


Company(){}

Company(String newTicker)throws IOException{
    this.Ticker = newTicker;
    updateCompanyInfo();
}

    //Get Methods
    public Object[][] getIncomeStatementInfo() {
        return incomeStatementInfo;
    }   

    public Object[][] getBalanceSheetInfo(){
        return balanceSheetInfo;
    }
    public Object[][] getCashFlowsInfo(){
        return cashFlowsInfo;
    }

    public void investoolsLogin(){
    HttpClient client = new HttpClient();  

    GetMethod method = new GetMethod("https://online.investools.com/authentication/auth.iedu");  
    client.getParams().setParameter("userid", "d*********");  
    client.getParams().setParameter("password", "S****"); 
    client.getParams().setParameter("http.protocol.single-cookie-header", true);
    client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    }

    //Updating Methods from Internet or computer
    public void updateCompanyIs()throws IOException{

    File f = new File ("Data/"+(Ticker)+"/IS.txt");
    if(f.exists() == true){


        Scanner input = new Scanner(f);

        while (input.hasNextLine()){    
        for (int row = 0, row_size = incomeStatementInfo[row].length; row < row_size; row++){
                for (int col = 0, col_size = incomeStatementInfo.length; col < col_size; col++){
                    if(input.hasNextLine()){
                    incomeStatementInfo[col][row] = input.nextLine();
                    }
                }
            }           
        }
    }

    if(f.exists() == false){
    //OptionPane.showMessageDialog("Offline Ticker Data Does Not Exist");
        investoolsLogin();

        Document doc = Jsoup.connect("http://toolbox.investools.com/graphs/fundamentalAnalysis.iedu?report=BS&symbol="+(Ticker)).get();

        System.out.println("doc is null: " + (doc == null));
        // Elements table = doc.select("table");
        Elements columns = doc.getElementById("fundamentalsForm").children().select("table").get(0).select("tr").get(0).select("td");
        Iterator<Element> columnIterator = columns.iterator();
        int col = 0;
        int row = 0;
                while (columnIterator.hasNext()) {
                    Element column = columnIterator.next();
                    Elements rows = column.select("table").get(0).select("tr");
                    Iterator<Element> rowsIterator = rows.iterator();
                    col = col + 1;
                    while (rowsIterator.hasNext()){
                    row = row + 1;
                    //Element rowIterator.next = ;
                    incomeStatementInfo[col][row] = rowsIterator.next();
                }
            }
        }
    }   
    public void updateCompanyBs()throws IOException{

    File f = new File ("Data/"+(Ticker)+"/BS.txt");
    if(f.exists() == true){
        //OptionPane.showMessageDialog("Offline Ticker Data Does Not Exist");

        Scanner input = new Scanner(f);
            while (input.hasNextLine()){
            for (int row = 0, row_size = balanceSheetInfo[row].length; row < row_size; row++){
                for (int col = 0, col_size = balanceSheetInfo.length; col < col_size; col++){
                    if(input.hasNextLine()){
                    balanceSheetInfo[col][row] = input.nextLine();
                    }
                }
            }                   
        }
    }   
    if(f.exists() == false){
        investoolsLogin();

        Document doc = Jsoup.connect("http://toolbox.investools.com/graphs/fundamentalAnalysis.iedu?report=BS&symbol="+(Ticker)).get();
            // Elements table = doc.select("table");
        Elements columns = doc.select("fundamentalsForm").first().select("table").get(0).select("tr").get(0).select("td");
        Iterator<Element> columnIterator = columns.iterator();
        int col = 0;
        int row = 0;
                while (columnIterator.hasNext()) {
                    Element column = columnIterator.next();
                    Elements rows = column.select("table").get(0).select("tr");
                    Iterator<Element> rowsIterator = rows.iterator();
                    col = col + 1;
                    while (rowsIterator.hasNext()){
                    row = row + 1;
                    //Element rowIterator.next = ;
                    balanceSheetInfo[col][row] = rowsIterator.next();
                }
            }
        }   
    }
    public void updateCompanyCf()throws IOException{

    File f = new File ("Data/"+(Ticker)+"/CF.txt");
    if(f.exists() == true){
        //OptionPane.showMessageDialog("Offline Ticker Data Does Not Exist");

        Scanner input = new Scanner(f);
        while (input.hasNextLine()){    
                for (int row = 0, row_size = cashFlowsInfo[row].length; row < row_size; row++){
                    for (int col = 0, col_size = cashFlowsInfo.length; col < col_size; col++){
                        if(input.hasNextLine()){
                        cashFlowsInfo[col][row] = input.nextLine();
                        }
                    }
                }                           
            }
        }

    if(f.exists() == false){    
        investoolsLogin();

            Document doc = Jsoup.connect("http://toolbox.investools.com/graphs/fundamentalAnalysis.iedu?report=BS&symbol="+(Ticker)).get();
            // Elements table = doc.select("table");
            Elements columns = doc.getElementById("fundamentalsForm").children().select("table").get(0).select("tr").get(0).select("td");
            Iterator<Element> columnIterator = columns.iterator();
            int col = 0;
            int row = 0;
                while (columnIterator.hasNext()) {
                    Element column = columnIterator.next();
                    Elements rows = column.select("table").get(0).select("tr");
                    Iterator<Element> rowsIterator = rows.iterator();
                    col = col + 1;
                    while (rowsIterator.hasNext()){
                    row = row + 1;
                    //Element rowIterator.next = ;
                    cashFlowsInfo[col][row] = rowsIterator.next();
                }
            }
        }
    }
public void updateCompanyInfo() throws IOException {
        updateCompanyIs();
        updateCompanyBs();
        updateCompanyCf();

}
}

and IncomeStatePane.java (along with similar other object classes where I build the tablemodel

public class IncomeStatementPane {

public static JComponent render(Company company) {
    // Refreshes all cells
    //Parse HTML for object company
    String[] columnNames = {"","Year","Year", "Year", "Year", "Year","Year","Year", "Year", "Year", "Year"};

    Object[][] is = company.getIncomeStatementInfo();

    JPanel isPanel = new JPanel();
    JTable isTable = new JTable(is, columnNames);
    JScrollPane scrollPane = new JScrollPane(isTable);
    isPanel.add(scrollPane, BorderLayout.CENTER);

    return isPanel;
    }
}

a fully compilable version is @ http://dl.dropbox.com/u/43354605/IS.zip

How can I either for the AppGUI to invalidate and repaint, or update the table model when the ticker search is executed. (I know a GUI refresh may not be ideal, but I just want to move on with minimal reconstruction for the version). If anyone has any insight to help me move the project along that would be great. PS in order to see how the second gui is refreshed under the previous gui you need to maximize the window.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user1093111
  • 1,091
  • 5
  • 21
  • 47

1 Answers1

2

All you need to do is modify the model of the table. It will fire the appropriate events which will make the view refresh itself. For example, to add a new row:

DefaultTableMode model = (DefaultTableModel) table.getModel();
model.addRow(newRow);

Browse the javadoc of DefaultTableModel to know all the possible operations. And read the tutorial about JTable.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 1
    I read both, but I am not sure where to do this. I dont need to add a new row, just import a new financial statement. Same rows and columns. I have browsed the tutorial, but I am not sure how to do this from my refresh method since my model is in another class. Thats mainly where I am confused. – user1093111 Jul 22 '12 at 22:18