Google Answers Logo
View Question
 
Q: JTable renderer issue with rendering Integer object ( No Answer,   0 Comments )
Question  
Subject: JTable renderer issue with rendering Integer object
Category: Computers > Programming
Asked by: gooseman-ga
List Price: $5.50
Posted: 25 Oct 2002 02:19 PDT
Expires: 25 Oct 2002 11:52 PDT
Question ID: 89579
I have a piece of JTable code that has a custom editor and cell
renderer, so that it can display checkboxes, Strings and other objects
in the same column. The problem is I can't figure out how to get the
Integer renderer to work. I'd like it to behave so that the numbers
are right justified, and can take in only number characters. Here is
part of the code:

class MultiRenderer extends DefaultTableCellRenderer {
  JCheckBox checkBox = new JCheckBox();

  public Component getTableCellRendererComponent(
                     JTable table, Object value,
                     boolean isSelected, boolean hasFocus,
                     int row, int column) {
    if (value instanceof Boolean) {                    // Boolean
      checkBox.setSelected(((Boolean)value).booleanValue());
      checkBox.setHorizontalAlignment(JLabel.CENTER);
      return checkBox;
    }
    if (value instanceof Integer){                     // Integer
      Integer in = (Integer) value;
      return super.getTableCellRendererComponent(
         table,in,isSelected,hasFocus,row,column);
    }
    else{
      String str = (value == null) ? "" : value.toString();
      return super.getTableCellRendererComponent(
           table,str,isSelected,hasFocus,row,column);
    }
  }
}

class ComboString {
  String str;
  ComboString(String str) {
    this.str = str;
  }
  public String toString() {
    return str;
  }
}

class MultiEditor implements TableCellEditor {
  private final static int      COMBO = 0;
  private final static int    BOOLEAN = 1;
  private final static int     STRING = 2;
  private final static int    INTEGER = 3;
  private final static int NUM_EDITOR = 4;
  DefaultCellEditor[] cellEditors;
  JComboBox comboBox;
  int flg;

  public MultiEditor() {
    cellEditors = new DefaultCellEditor[NUM_EDITOR];
    comboBox = new JComboBox();
    comboBox.addItem("true");
    comboBox.addItem("false");
    cellEditors[COMBO]   = new DefaultCellEditor(comboBox);
    JCheckBox checkBox   = new JCheckBox();
    checkBox.setOpaque( true );
    checkBox.setHorizontalAlignment(JLabel.CENTER);
    cellEditors[BOOLEAN] = new DefaultCellEditor(checkBox);
    JTextField textField = new JTextField();
    cellEditors[STRING]  = new DefaultCellEditor(textField);
    cellEditors[INTEGER]  = new DefaultCellEditor(textField);
    flg = NUM_EDITOR;      // nobody
  }

  public Component getTableCellEditorComponent(JTable table, Object
value,
              boolean isSelected, int row, int column) {
    if (value instanceof ComboString) {                       //
ComboString
      flg = COMBO;
      String str = (value == null) ? "" : value.toString();
      return cellEditors[COMBO].getTableCellEditorComponent(
                       table, str,   isSelected, row, column);
    } else if (value instanceof Boolean) {                    //
Boolean
      flg = BOOLEAN;
      return cellEditors[BOOLEAN].getTableCellEditorComponent(
                       table, value, isSelected, row, column);
    } else if (value instanceof String) {                     //
String
      flg = STRING;
      return cellEditors[STRING].getTableCellEditorComponent(
                       table, value, isSelected, row, column);
    } else if (value instanceof Integer) {                    //
Integer
        // Assuming that passing Integer object 
        // will get renderer to work, but doesn't 
     flg = INTEGER;
      return cellEditors[INTEGER].getTableCellEditorComponent(
                       table, value, isSelected, row, column);
    }
    return null;
  }

  public Object getCellEditorValue() {
    switch (flg) {
      case   COMBO:
        String str = (String)comboBox.getSelectedItem();
        return new ComboString(str);
      case BOOLEAN:
      case  STRING:
        return cellEditors[flg].getCellEditorValue();
      case INTEGER:
        return cellEditors[flg].getCellEditorValue();
      default:         return null;
    }
  }

  public Component getComponent() {
    return cellEditors[flg].getComponent();
  }
  public boolean stopCellEditing() {
    return cellEditors[flg].stopCellEditing();
  }
  public void cancelCellEditing() {
    cellEditors[flg].cancelCellEditing();
  }
  public boolean isCellEditable(EventObject anEvent) {
    return true;//cellEditors[flg].isCellEditable(anEvent);
  }
  public boolean shouldSelectCell(EventObject anEvent) {
    return cellEditors[flg].shouldSelectCell(anEvent);
  }
  public void addCellEditorListener(CellEditorListener l) {
    cellEditors[flg].addCellEditorListener(l);
  }
  public void removeCellEditorListener(CellEditorListener l) {
    cellEditors[flg].removeCellEditorListener(l);
  }
  public void setClickCountToStart(int n) {
    cellEditors[flg].setClickCountToStart(n);
  }
  public int getClickCountToStart() {
    return cellEditors[flg].getClickCountToStart();
  }
}

If you want to see more code, just ask.

Clarification of Question by gooseman-ga on 25 Oct 2002 11:01 PDT
If I have not made my question clear, feel free to ask me to clarify!
Answer  
There is no answer at this time.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy