devdaily home | java | perl | unix | directory | blog


What this is

This file is part of the Tame Swing collection that we have compiled. All of the Java source code in these examples was created by Nobuo Tamemasa. As these examples have become hard to find on the internet, we have put them in one place here, supported by minimal advertising. In time we will include images of each running sample Java/Swing application.

The intent of having these files here is to help you learn Java and Swing by example, and Tame certainly created some terrific examples of the power of the Swing framework.

Other links

The source code

/* (swing1.1) */
package tame.table;

import java.awt.Color;
import java.awt.Component;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.swing.JProgressBar;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

/**
@author Nobuo Tamemasa
@version 1.0 03/03/99
*/
public 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 enum = limitColors.keys();
    while (enum.hasMoreElements()) {
      limitValues[i++] = ((Integer)enum.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;
    }
  }
}

Copyright © 1998-2009 DevDaily.com
All Rights Reserved.