Indicator Table Example : Grid Table « Swing Components « Java






Indicator Table Example

Indicator Table Example
 

// Example from http://www.crionics.com/products/opensource/faq/swing_ex/SwingExamples.html


/* (swing1.1) */


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;

/**
 * @version 1.0 03/03/99
 */
public class IndicatorTableExample extends JPanel {
  private static final int MAX = 100;

  private static final int MIN = 0;

  public IndicatorTableExample() {
    setLayout(new BorderLayout());

    DefaultTableModel dm = new DefaultTableModel() {
      public Class getColumnClass(int col) {
        switch (col) {
        case 0:
          return String.class;
        case 1:
          return Integer.class;
        case 2:
          return Integer.class;
        default:
          return Object.class;
        }
      }

      public boolean isCellEditable(int row, int col) {
        switch (col) {
        case 2:
          return false;
        default:
          return true;
        }
      }

      public void setValueAt(Object obj, int row, int col) {
        if (col != 1) {
          super.setValueAt(obj, row, col);
          return;
        }
        try {
          Integer integer = new Integer(obj.toString());
          super.setValueAt(checkMinMax(integer), row, col);
        } catch (NumberFormatException ex) {
          ex.printStackTrace();
        }
      }
    };
    dm.setDataVector(new Object[][] {
        { "not human", new Integer(100), new Integer(100) },
        { "hard worker", new Integer(76), new Integer(76) },
        { "ordinary guy", new Integer(51), new Integer(51) },
        { "lazy fellow", new Integer(12), new Integer(12) } },
        new Object[] { "Name", "Result", "Indicator" });

    JTable table = new JTable(dm);

    IndicatorCellRenderer renderer = new IndicatorCellRenderer(MIN, MAX);
    renderer.setStringPainted(true);
    renderer.setBackground(table.getBackground());

    // set limit value and fill color
    Hashtable limitColors = new Hashtable();
    limitColors.put(new Integer(0), Color.green);
    limitColors.put(new Integer(60), Color.yellow);
    limitColors.put(new Integer(80), Color.red);
    renderer.setLimits(limitColors);
    table.getColumnModel().getColumn(2).setCellRenderer(renderer);

    table.getModel().addTableModelListener(new TableModelListener() {
      public void tableChanged(TableModelEvent e) {
        if (e.getType() == TableModelEvent.UPDATE) {
          int col = e.getColumn();
          if (col == 1) {
            int row = e.getFirstRow();
            TableModel model = (TableModel) e.getSource();
            Integer value = (Integer) model.getValueAt(row, col);
            model.setValueAt(checkMinMax(value), row, ++col);
          }
        }
      }
    });

    JScrollPane pane = new JScrollPane(table);
    add(pane, BorderLayout.CENTER);
  }

  public static void main(String[] args) {
    JFrame f = new JFrame("IndicatorTable Example");
    f.getContentPane()
        .add(new IndicatorTableExample(), BorderLayout.CENTER);
    f.setSize(400, 120);
    f.setVisible(true);
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
  }

  private Integer checkMinMax(Integer value) {
    int intValue = value.intValue();
    if (intValue < MIN) {
      intValue = MIN;
    } else if (MAX < intValue) {
      intValue = MAX;
    }
    return new Integer(intValue);
  }
}

/**
 * @version 1.0 03/03/99
 */

class IndicatorCellRenderer extends JProgressBar implements TableCellRenderer {
  private Hashtable limitColors;

  private int[] limitValues;

  public IndicatorCellRenderer() {
    super(JProgressBar.HORIZONTAL);
    setBorderPainted(false);
  }

  public IndicatorCellRenderer(int min, int max) {
    super(JProgressBar.HORIZONTAL, min, max);
    setBorderPainted(false);
  }

  public Component getTableCellRendererComponent(JTable table, Object value,
      boolean isSelected, boolean hasFocus, int row, int column) {
    int n = 0;
    if (!(value instanceof Number)) {
      String str;
      if (value instanceof String) {
        str = (String) value;
      } else {
        str = value.toString();
      }
      try {
        n = Integer.valueOf(str).intValue();
      } catch (NumberFormatException ex) {
      }
    } else {
      n = ((Number) value).intValue();
    }
    Color color = getColor(n);
    if (color != null) {
      setForeground(color);
    }
    setValue(n);
    return this;
  }

  public void setLimits(Hashtable limitColors) {
    this.limitColors = limitColors;
    int i = 0;
    int n = limitColors.size();
    limitValues = new int[n];
    Enumeration e = limitColors.keys();
    while (e.hasMoreElements()) {
      limitValues[i++] = ((Integer) e.nextElement()).intValue();
    }
    sort(limitValues);
  }

  private Color getColor(int value) {
    Color color = null;
    if (limitValues != null) {
      int i;
      for (i = 0; i < limitValues.length; i++) {
        if (limitValues[i] < value) {
          color = (Color) limitColors
              .get(new Integer(limitValues[i]));
        }
      }
    }
    return color;
  }

  private void sort(int[] a) {
    int n = a.length;
    for (int i = 0; i < n - 1; i++) {
      int k = i;
      for (int j = i + 1; j < n; j++) {
        if (a[j] < a[k]) {
          k = j;
        }
      }
      int tmp = a[i];
      a[i] = a[k];
      a[k] = tmp;
    }
  }
}



           
         
  








Related examples in the same category

1.HyperLink in TableHyperLink in Table
2.Column popup menuColumn popup menu
3.Tree TableTree Table
4.Swing Table in ComboBoxSwing Table in ComboBox
5.Tabbable Currency TableTabbable Currency Table
6.Icon Currency TableIcon Currency Table
7.MultiLine Header TableMultiLine Header Table
8.ToolTip TableToolTip Table
9.Striped Currency TableStriped Currency Table
10.CurrencyTableCurrencyTable
11.Calculated Column TableCalculated Column Table
12.Table Utilities
13.Fraction Currency TableFraction Currency Table
14.Highlight Currency TableHighlight Currency Table
15.Highlight Currency Table 2Highlight Currency Table 2
16.MultiLine TableMultiLine Table
17.Updatable Highlight Currency TableUpdatable Highlight Currency Table
18.Editable Highlight Currency TableEditable Highlight Currency Table
19.ComboBox TableComboBox Table
20.Groupable(Group) Header ExampleGroupable(Group) Header Example
21.MultiWidth Header ExampleMultiWidth Header Example
22.MultiLine Header ExampleMultiLine Header Example
23.Table Row Header ExampleTable Row Header Example
24.Fixed Table Column ExampleFixed Table Column Example
25.Button Table ExampleButton Table Example
26.Radio Button Table ExampleRadio Button Table Example
27.RadioButton Table Example 2RadioButton Table Example 2
28.MultiLine Cell ExampleMultiLine Cell Example
29.Each Row with different Editor ExampleEach Row with different Editor Example
30.Multiple Component Table: Checkbox and ComboboxMultiple Component Table: Checkbox and Combobox
31.multiple Component Table 2: checkboxmultiple Component Table 2: checkbox
32.Union Data Table ExampleUnion Data Table Example
33.Total(Calculate) Row ExampleTotal(Calculate) Row Example
34.Colored Cell Table Example
35.multiple Font Cell Table Example
36.Multi Span Cell Table Example
37.Mixed Table Example
38.Pushable Table Header ExamplePushable Table Header Example
39.Sortable Table ExampleSortable Table Example
40.ToolTip Header Table ExampleToolTip Header Table Example
41.Fixed Table Row ExampleFixed Table Row Example
42.multiple Row Header Example
43.Column Border Table ExampleColumn Border Table Example
44.Cell Border Table ExampleCell Border Table Example
45.Hide Column Table ExampleHide Column Table Example
46.Animated Icon Table ExampleAnimated Icon Table Example
47.Animated Icon Header Example
48.Editable Header Table ExampleEditable Header Table Example
49.Editable Header Table Example 2Editable Header Table Example 2
50.JSortTable