tablemodel: how to specify default editor for each cell in a column

Discussion in 'Java' started by Peter Grison, May 5, 2004.

  1. Peter Grison

    Peter Grison Guest

    I've made a implementation class for AbstractTableModel.

    JTable uses the getColumnClass method to determine the default renderer/
    editor for all cells in a column.

    public Class getColumnClass(int c)
    {
    return getValueAt(0, c).getClass();
    }

    I have different classes in the column, how can I set the class for each
    individual cell?

    kind regards,

    pete
    Peter Grison, May 5, 2004
    #1
    1. Advertising

  2. Peter Grison

    Guest

    Peace be unto you

    It involves extending JTable and overriding
    TableCellRenderer getCellRenderer(int row, int column)
    In that method, create the appropriate switch statements.

    Here is a screenshot
    <a href="http://members.rogers.com/mromarkhan/nntp-test/HTMLFileEditor.jpg">
    screenshot
    </a>

    And here is an example that uses some code
    from Sun
    <code>


    import javax.swing.JTable;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import java.awt.BorderLayout;
    import javax.swing.BorderFactory;
    import javax.swing.JLabel;
    import javax.swing.JTable;
    import java.io.File;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    import javax.swing.Icon;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import java.io.IOException;
    import javax.swing.JFileChooser;
    import javax.swing.filechooser.FileFilter;
    import javax.swing.border.Border;
    import javax.swing.table.TableCellRenderer;
    import java.awt.Color;
    import java.awt.Component;
    import javax.swing.AbstractCellEditor;
    import javax.swing.table.TableCellEditor;
    import javax.swing.JButton;
    import javax.swing.JColorChooser;
    import javax.swing.JDialog;
    import javax.swing.JTable;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.table.AbstractTableModel;
    import javax.swing.JComboBox;
    import javax.swing.DefaultCellEditor;
    public class HTMLTableEditor extends JFrame
    {
    private JTable table;
    private HTMLTableModel tableModel;
    private ColorRenderer colorRenderer;
    private ColorEditor colorEditor;
    private FileEditor fileEditor;
    private FileRenderer fileRenderer;
    private DefaultCellEditor booleanEditor;
    public HTMLTableEditor()
    {
    fileEditor = new FileEditor();
    fileRenderer = new FileRenderer(true);
    JComboBox boolComboBox = new JComboBox();
    boolComboBox.addItem("True");
    boolComboBox.addItem("False");

    booleanEditor = new DefaultCellEditor(boolComboBox);
    colorRenderer = new ColorRenderer(true);
    colorEditor = new ColorEditor();
    tableModel = new HTMLTableModel();
    table = new JTable(tableModel)
    {
    public TableCellRenderer getCellRenderer(int row, int column)
    {
    if ((row == 0) && (column == 1))
    {
    return colorRenderer;
    }
    else if((row == 2) && (column == 1))
    {
    return fileRenderer;
    }
    return super.getCellRenderer(row, column);
    }
    public TableCellEditor getCellEditor(int row, int column)
    {
    if ((row == 0) && (column == 1))
    {
    return colorEditor;
    }
    else if((row == 1) && (column == 1))
    {
    return booleanEditor;
    }
    else if((row == 2) && (column == 1))
    {
    return fileEditor;
    }
    return super.getCellEditor(row, column);
    }
    };
    table.setRowHeight(2,200);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.getContentPane().add(new JScrollPane(table),BorderLayout.CENTER);
    this.pack();
    this.setVisible(true);
    this.setTitle("HTML Editor");
    }
    public static void main(String [] s)
    {
    new HTMLTableEditor();
    }

    class HTMLTableModel extends AbstractTableModel
    {
    private String[] columnNames = {"Name",
    "Value"};
    private Object[][] data =
    {
    {"Background-Color", new Color(0,0,128)},
    {"isTextWrap", new Boolean(true)},
    {"Background-Image", new String("c:/windows/desktop/hypercard.png")}
    };

    public int getColumnCount()
    {
    return columnNames.length;
    }

    public int getRowCount()
    {
    return data.length;
    }

    public String getColumnName(int col)
    {
    return columnNames[col];
    }

    public Object getValueAt(int row, int col)
    {
    return data[row][col];
    }

    public Class getColumnClass(int c)
    {
    return getValueAt(0, c).getClass();
    }

    public boolean isCellEditable(int row, int col)
    {
    if (col < 1)
    {
    return false;
    }
    else
    {
    return true;
    }
    }

    public void setValueAt(Object value, int row, int col)
    {
    data[row][col] = value;
    fireTableCellUpdated(row, col);
    }
    }

    public class FileEditor extends AbstractCellEditor
    implements TableCellEditor,
    ActionListener
    {
    String currentFile;
    JButton button;
    JFileChooser fileChooser;
    protected static final String EDIT = "edit";

    public FileEditor()
    {
    button = new JButton();
    button.setActionCommand(EDIT);
    button.addActionListener(this);
    button.setBorderPainted(false);
    fileChooser = new JFileChooser();
    }

    /**
    * Handles events from the editor button and from
    * the dialog's OK button.
    */
    public void actionPerformed(ActionEvent e)
    {
    if (EDIT.equals(e.getActionCommand()))
    {
    button.setText(currentFile);
    //The user has clicked the cell, so
    //bring up the dialog.
    int returnVal = fileChooser.showOpenDialog(HTMLTableEditor.this);
    if(returnVal == JFileChooser.APPROVE_OPTION)
    {
    try
    {
    currentFile = fileChooser.getSelectedFile().getCanonicalPath();
    }
    catch(IOException ie)
    {
    }
    }
    //Make the renderer reappear.
    fireEditingStopped();
    }
    }

    //Implement the one CellEditor method that AbstractCellEditor doesn't.
    public Object getCellEditorValue()
    {
    return currentFile;
    }

    //Implement the one method defined by TableCellEditor.
    public Component getTableCellEditorComponent(JTable table,
    Object value,
    boolean isSelected,
    int row,
    int column)
    {
    currentFile = (String)value;
    return button;
    }
    }


    public class ColorEditor extends AbstractCellEditor
    implements TableCellEditor,
    ActionListener
    {
    Color currentColor;
    JButton button;
    JColorChooser colorChooser;
    JDialog dialog;
    protected static final String EDIT = "edit";

    public ColorEditor()
    {
    button = new JButton();
    button.setActionCommand(EDIT);
    button.addActionListener(this);
    button.setBorderPainted(false);

    colorChooser = new JColorChooser();
    dialog = JColorChooser.createDialog(button,
    "Pick a Color",
    true, //modal
    colorChooser,
    this, //OK button handler
    null); //no CANCEL button handler
    }

    /**
    * Handles events from the editor button and from
    * the dialog's OK button.
    */
    public void actionPerformed(ActionEvent e)
    {
    if (EDIT.equals(e.getActionCommand()))
    {
    //The user has clicked the cell, so
    //bring up the dialog.
    button.setBackground(currentColor);
    colorChooser.setColor(currentColor);
    dialog.setVisible(true);

    //Make the renderer reappear.
    fireEditingStopped();

    }
    else
    {
    //User pressed dialog's "OK" button.
    currentColor = colorChooser.getColor();
    }
    }

    //Implement the one CellEditor method that AbstractCellEditor doesn't.
    public Object getCellEditorValue()
    {
    return currentColor;
    }

    //Implement the one method defined by TableCellEditor.
    public Component getTableCellEditorComponent(JTable table,
    Object value,
    boolean isSelected,
    int row,
    int column)
    {
    currentColor = (Color)value;
    return button;
    }
    }

    public class ColorRenderer extends JButton
    implements TableCellRenderer
    {
    Border unselectedBorder = null;
    Border selectedBorder = null;
    boolean isBordered = true;

    public ColorRenderer(boolean isBordered)
    {
    this.isBordered = isBordered;
    setOpaque(true);
    }

    public Component getTableCellRendererComponent(
    JTable table, Object color,
    boolean isSelected, boolean hasFocus,
    int row, int column)
    {
    Color newColor = (Color)color;
    this.setIcon(new MiniIcon(newColor));
    this.setText("("+newColor.getRed() + " "+
    newColor.getGreen() + " "+
    newColor.getBlue()+")");
    if (isBordered)
    {
    if (isSelected)
    {
    if (selectedBorder == null)
    {
    selectedBorder = BorderFactory.createMatteBorder(2,5,2,5,
    table.getSelectionBackground());
    }
    setBorder(selectedBorder);
    }
    else
    {
    if (unselectedBorder == null)
    {
    unselectedBorder = BorderFactory.createMatteBorder(2,5,2,5,
    table.getBackground());
    }
    setBorder(unselectedBorder);
    }
    }
    return this;
    }
    }

    public class MiniIcon implements Icon
    {
    private Color color;
    public MiniIcon(Color c)
    {
    color = c;
    }

    public int getIconHeight()
    {
    return 16;
    }
    public int getIconWidth()
    {
    return 16;
    }

    public void paintIcon(Component c, Graphics g, int x, int y)
    {
    Graphics2D g2 = (Graphics2D) g;
    g2.setColor(color);
    g2.fill(new Rectangle(x, y, 16, 16));
    }
    }

    public class FileRenderer extends JButton
    implements TableCellRenderer
    {
    Border unselectedBorder = null;
    Border selectedBorder = null;
    boolean isBordered = true;

    public FileRenderer(boolean isBordered)
    {
    this.isBordered = isBordered;
    //setOpaque(true); //MUST do this for background to show up.
    }

    public Component getTableCellRendererComponent(
    JTable table, Object file,
    boolean isSelected, boolean hasFocus,
    int row, int column)
    {
    ImageIcon image = new ImageIcon((String)file);
    setIcon(image);
    if (isBordered)
    {
    if (isSelected)
    {
    if (selectedBorder == null)
    {
    selectedBorder = BorderFactory.createMatteBorder(2,5,2,5,
    table.getSelectionBackground());
    }
    setBorder(selectedBorder);
    }
    else
    {
    if (unselectedBorder == null)
    {
    unselectedBorder = BorderFactory.createMatteBorder(2,5,2,5,
    table.getBackground());
    }
    setBorder(unselectedBorder);
    }
    }
    setToolTipText((String) file);
    return this;
    }
    }
    }
    </code>
    References
    --
    Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.
    http://www.altavista.com/
    http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

    Have a good day.
    , May 20, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Blair
    Replies:
    1
    Views:
    396
    Eliyahu Goldin
    Aug 3, 2005
  2. news.rcn.com
    Replies:
    1
    Views:
    9,628
    Chris Smith
    Jul 12, 2004
  3. farseer
    Replies:
    7
    Views:
    665
    farseer
    Jun 9, 2005
  4. Lizard
    Replies:
    2
    Views:
    655
    Lizard
    Dec 21, 2006
  5. djc

    how to specify each column width?

    djc, Nov 2, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    3
    Views:
    174
    Eliyahu Goldin
    Nov 3, 2004
Loading...

Share This Page