Results 1 to 2 of 2
  1. #1
    shy_ted is offline Member
    Join Date
    Sep 2010
    Posts
    19
    Rep Power
    0

    Default looping around a table renderer, is it possible?

    i am trying to get rows to change colours, red, green or white. Depending on whether the value in the cells has increased or decreased etc.

    I have it on a loop that currently prints out the previous, current value, and if its increased or not. As it stands i just have a value inserting into a cell but not updating a colour.

    the code i have for the timer and method that updates the cell and compares values is...

    Java Code:
    private void updateStockPrices() {
    
            for (int i = 0; i < stockBeans.size(); i++) {
    
                Object obj1 = getData(jTable, i, 1);
                String obj2 = obj1.toString();
                float newVal = Float.parseFloat(obj2);
    
                StockBean currentBean = stockBeans.get(i);
                final StockBean currentStockBean = stockTickerDAO.refreshStockInfo(currentBean);
                System.out.println(currentStockBean.symbol);
                tableModel.setValueAt(currentStockBean.symbol, i, 0);
                tableModel.setValueAt(currentStockBean.price, i, 1);
                tableModel.setValueAt(currentStockBean.change, i, 2);
    
                float stock = currentStockBean.price;
    
                System.out.println("Comparing " + stock + " and " + newVal);
               
                if (stock > newVal) setData("INCREASE", i, 3);
                if (stock < newVal) setData("DECREASE", i, 3);
                if (stock == newVal)setData("NO CHANGE", i, 3);
                
            }
        }
    The table renderer code is..

    Java Code:
      JeksTable jTable = new JeksTable(tableModel){
    
            public Component prepareRenderer
                      (TableCellRenderer renderer,int Index_row, int Index_col) {
            Component comp = super.prepareRenderer(renderer, Index_row, Index_col);
    
            if (Index_row % 1 == 0 && isCellSelected(Index_row, Index_col)) {
              comp.setBackground(Color.LIGHT_GRAY);
            }
            else {
              comp.setBackground(Color.white);
            }
            return comp;
          }
       }
    Can i have 3 different renderers and call the appropriate one each time the above if statements are run?

    or does anyone have a more simple solution?

    Thanks

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    1. By convention, Java variable names (that includes method and constructor parameters) start with a lowercase letter. More on
    Code Conventions for the Java(TM) Programming Language: Contents

    2. Do you know what the modulo operator % does? What's the remainder when you divide an int by 1?

    3. We have no idea of the functionality of the class JeksTable.

    Can i have 3 different renderers and call the appropriate one each time the above if statements are run?
    You don't *call* a renderer. You can set different renderers to different columns, and you can return different components from the getTableCellRendererComponent(...) method.

    To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.

    db
    Last edited by DarrylBurke; 10-14-2010 at 02:51 PM.

Similar Threads

  1. ProgressBar Renderer?
    By greatmajestics in forum AWT / Swing
    Replies: 2
    Last Post: 04-20-2010, 04:12 AM
  2. ProgressBar renderer Help?
    By greatmajestics in forum AWT / Swing
    Replies: 0
    Last Post: 04-17-2010, 09:39 PM
  3. table renderer problem in nimbus look and feel
    By Allgorythm in forum New To Java
    Replies: 0
    Last Post: 02-22-2010, 12:20 PM
  4. Custom renderer (almost works)
    By geforce2000 in forum AWT / Swing
    Replies: 11
    Last Post: 12-13-2009, 09:15 PM
  5. how to use renderer in JTable
    By sunilpatel28 in forum Advanced Java
    Replies: 0
    Last Post: 12-09-2008, 08:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •