Links

Lists

Latest Updates

Ruby On Rails List
Python list
Advanced Java
The JavaScript List
Apache Users
Full Disclosure
Linux Security

Search the archives!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Advanced-java] Display combo box in JTable cell?


  • From: mark@xxxxxxxxxxx (Mark Plamann)
  • Subject: [Advanced-java] Display combo box in JTable cell?
  • Date: Mon, 25 Feb 2002 10:32:49 -0600

At 04:07 PM 2/25/2002 +0000, Greg Munt wrote:

>Why would you want to render a combo box? A combo box as an editor, I can
>understand.. But a renderer?

You are correct. I am looking to write an editor for a combo box. I have 
come up with an editor that displays the combo box when the JTable cell is 
selected, but once the user clicks another cell, the combo box is no longer 
display, just the text of the option in the combo box that is selected.

I apologize that my questions may be pretty basic for Swing gurus. I'm new 
to Swing and Applet programming, so this is a learning experience for me.

Here is my code for the editor. If anyone can make suggestions how to just 
display the combo box and not the text of the selected combo box option, I 
would appreciate it. Also, if you see blatant stupidity, corrections are 
alway appreciated. :)

public class RowEditor implements TableCellEditor {

   protected Hashtable editors;
   protected TableCellEditor editor, defaultEditor;
   JTable table;


   public RowEditor(JTable table) {
     this.table = table;
     editors = new Hashtable();
     defaultEditor = new DefaultCellEditor(new JTextField());
   }

   public void setEditorAt(int row, TableCellEditor editor) {
     editors.put(new Integer(row),editor);
   }

   public Component getTableCellEditorComponent(JTable table,
       Object value, boolean isSelected, int row, int column) {
     //editor = (TableCellEditor)editors.get(new Integer(row));
     //if (editor == null) {
     //  editor = defaultEditor;
     //}
     return editor.getTableCellEditorComponent(table,
              value, isSelected, row, column);
   }

   public Object getCellEditorValue() {
     return editor.getCellEditorValue();
   }
   public boolean stopCellEditing() {
     return editor.stopCellEditing();
   }
   public void cancelCellEditing() {
     editor.cancelCellEditing();
   }
   public boolean isCellEditable(EventObject anEvent) {
     selectEditor((MouseEvent)anEvent);
     return editor.isCellEditable(anEvent);
   }
   public void addCellEditorListener(CellEditorListener l) {
     editor.addCellEditorListener(l);
   }
   public void removeCellEditorListener(CellEditorListener l) {
     editor.removeCellEditorListener(l);
   }
   public boolean shouldSelectCell(EventObject anEvent) {
     selectEditor((MouseEvent)anEvent);
     return editor.shouldSelectCell(anEvent);
   }

   protected void selectEditor(MouseEvent e) {
     int row;
     if (e == null) {
       row = table.getSelectionModel().getAnchorSelectionIndex();
     } else {
       row = table.rowAtPoint(e.getPoint());
     }
     editor = (TableCellEditor)editors.get(new Integer(row));
     if (editor == null) {
       editor = defaultEditor;
     }
   }
}