Cell Renderer for individual cell

I'm currently building a GUI to view and modify some info that is much like a database, but each line is different. So far I've got the 2 column table. The left column will hold the "Variable Name" and the right will hold the "Value". Some "Variables" can only be explicit values and I would like to use a ComboBox. Others are Boolean and others still are just plain user entered Text.

The following web page says that a single CellRenderer is used for an entire column. Is this true? Is there any way around it? If I just want to put a ComboBox or something in a single cell, can I not do that?

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

TIA

Jake

Comments

  • [b]

    Java is capable of everything (almost).
    I think you have to read the page again.
    But have you tried it yourself?

    If you look at the picture of the ComboBox editor
    on that page (of the link you specified below), it looks like the combobiox is just for a single cell Ta particular row)in the table.
    Not for the entire column.

    Try again

    bashiro

    [/b]

    : I'm currently building a GUI to view and modify some info that is much like a database, but each line is different. So far I've got the 2 column table. The left column will hold the "Variable Name" and the right will hold the "Value". Some "Variables" can only be explicit values and I would like to use a ComboBox. Others are Boolean and others still are just plain user entered Text.
    :
    : The following web page says that a single CellRenderer is used for an entire column. Is this true? Is there any way around it? If I just want to put a ComboBox or something in a single cell, can I not do that?
    :
    : http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
    :
    : TIA
    :
    : Jake
    :

  • I think you shouldn't use a CellRenderer at all. We used table models in a huge GUI application last year.

    First, make a subclass of AbstractTableModel and override the GetValue( i, j ) method, which returns an Object. Make this function return the object required to represent the value at that location (use Boolean, Integer, String classes or ComboBox, CheckBox, etc for choices). Attach the TableModel class to your table via the constructor of JTable.

  • First off, I was incorrect in stating Cell Renderer. I was corrected on a different forum. I meant Cell Editor. Regardless, I can't seem to find a way to get the idea to work. I can't find getValue, do you mean getValueAt?

    Are you suggesting that I use some sort of popup to obtain the values?





    : I think you shouldn't use a CellRenderer at all. We used table models in a huge GUI application last year.
    :
    : First, make a subclass of AbstractTableModel and override the GetValue( i, j ) method, which returns an Object. Make this function return the object required to represent the value at that location (use Boolean, Integer, String classes or ComboBox, CheckBox, etc for choices). Attach the TableModel class to your table via the constructor of JTable.
    :



  • :
    : First off, I was incorrect in stating Cell Renderer. I was corrected on a different forum. I meant Cell Editor. Regardless, I can't seem to find a way to get the idea to work. I can't find getValue, do you mean getValueAt?
    :

    Yes and I'm sorry.

    : Are you suggesting that I use some sort of popup to obtain the values?
    :

    No what kind of pop-up? If you return the right objects Java should render them right for you. I could send you the files we used, but they're BIG and I think it will be HARD to distill your stuff from them (because it also involves a TableSorter, sorting the tables ascending).
  • The idea is the following. Create a custom table model:
    [code]
    class TableModel extends AbstracTableModel
    {
    Object getValueAt( int i, int j )
    {
    if ( i == whatever || && ^^ AND SO ON j == )
    return new Boolean( true );
    return new Integer( 52 );
    return new CheckBox( true );
    or anything...
    }
    }
    [/code]
    Then attach this to your table:
    [code]
    JTable t = new JTable( new TableModel() );
    [/code]

  • Unfortunately, this is not the case. You must assign the same ComboBox to the entire column as seen here.

    TableColumn sportColumn = table.getColumnModel().getColumn(2);
    ...
    JComboBox comboBox = new JComboBox();
    comboBox.addItem("Snowboarding");
    comboBox.addItem("Rowing");
    comboBox.addItem("Chasing toddlers");
    comboBox.addItem("Speed reading");
    comboBox.addItem("Teaching high school");
    comboBox.addItem("None");
    sportColumn.setCellEditor(new DefaultCellEditor(comboBox));

    I need different ComboBoxes with different contents for each row.

    I've tried to modify the TableModel and even the CellEditor at other's suggestions, but it never seems to actually work. I am beginning to question whether this is actually possible.

    Jake


  • This doesn't seem to work. When you return a ComboBox, it fills the cell with a very explicit text description of a ComboBox, but no actual combobox.

    I've tried modifying the editor's getTableCellEditorComponent function, but that doesn't quite work either. When you click on a cell, I can get it to show a combobox, but when you select something the value of the cell never changes?

    Jake


  • :
    : Unfortunately, this is not the case. You must assign the same ComboBox to the entire column as seen here.
    :
    : TableColumn sportColumn = table.getColumnModel().getColumn(2);
    : ...

    It is the case, but I did say not to start out with a column. If you start out with a column, you get a column. I talked about TableModels in which you can retrieve individual cells, marked by (i,j) in the table.

    : I've tried to modify the TableModel and even the CellEditor at other's suggestions, but it never seems to actually work. I am beginning to question whether this is actually possible.

    It is possible because I actually got it to work. I got the code as well, as I said, for anyone who wants it. But it is a huge GUI so prepare for a lot of crappy code.

  • If you could forward that code, that would be great. I'll see what I can do with it.

    Thanks,

    jakewendt@yahoo.com
  • Allright I went on a search for you. Altough this is the first place you should look for stuff (which is why I assumed you did...), Sun has a nice short demo on this stuff doing what you want to have:

    http://java.sun.com/docs/books/tutorial/uiswing/components/example-swing/TableDemo.java

    They show some stuff for booleans but it should be easy to change your data to multi-type.

  • I've seen Sun's sample codes. The problem is having ComboBoxes with different contents, and even TextFields, all in the same column. All of Sun's samples are for just one of these components or the exact same combobox in all the cells in the column. Something like (but contents completely different) the example table below.


    Jake
    Example Table
    ___________________________________
    FirstName | TextField
    LastName | TextField
    Fav Color | ColorComboBox
    Worst Color | ColorComboBox
    Fav Digit | DigitComboBox
    BirthState | StateComboBox
    etc...


  • I got your problem from the start right on. That is why I said the example should be not so hard to modify to contain different types. I tried for a (very) short while, but couldn't get it to work. Whic is why I am a bit more sceptic by now, altough I had it working once.

    Your example table made me think of something else; maybe you already considered this and decided not to go there. In my opinion, you are using the (data) table more as a means of layout (like an HTML table). Why not use a GridLayout and display components individually?

  • Your example table made me think of something else; maybe you already considered this and decided not to go there. In my opinion, you are using the (data) table more as a means of layout (like an HTML table). Why not use a GridLayout and display components individually?

    I thought about that, but I needed everything in an array and I needed to be able to add to the list at runtime. I got the following from bithir on Project Swing on forums.java.sun.com. It needs all the meat and some fine tuning, but it appears to work as needed.

    Thanks for all of your help.
    Jake


    // It should really extend AbstractCellEditor instead of DefaultCellEditor
    // But I did not have 1.4 available.
    public class FITSCellEditor extends DefaultCellEditor implements ActionListener {

    private JComboBox[] combos = null;
    private JComboBox editor = null;

    public FITSCellEditor() {

    // This line should be removed if AbstractCellEditor is used
    // The JTextField It is NOT used - it is just here to avoid
    // NullPointerException.
    super( new JTextField() );

    // Note: This should obviously be four from the context of the original posting

    // Pre construct the three selected resources
    // This could be done as lazy instantation if there are many instantiated
    // versions of this default cell editor. Probably not needed
    combos = new JComboBox[3];
    combos[0] = new JComboBox( new Object[] { "0select1", "0select2", "0select3" } );
    combos[1] = new JComboBox( new Object[] { "1select1", "1select2", "1select3" } );
    combos[2] = new JComboBox( new Object[] { "2select1", "2select2", "2select3" } );

    // We need to register ourself so we know when the user decided they
    // had enough and we should stop the cell editing
    // Obviously this introduces some proplems with using arrow keys and
    // mouse clicks, but we decided that the users know how to make the action
    // event appear.
    for(int i = 0; i < combos.length; i++)
    {
    combos.addActionListener( this );
    }

    }

    public void actionPerformed(ActionEvent ae)
    {
    // If any of the comboboxes are editing, we better stop it.
    stopCellEditing();
    // we could at this point make sure that the editor is null
    // both for editing stopped and editing cancelled
    }

    public Object getCellEditorValue()
    {
    if( editor == null )
    {
    // Panick! We are in a state we should never end upp in.
    // Normally this should be an assert.
    return null;
    }
    return editor.getSelectedItem();
    }


    public Component getTableCellEditorComponent(JTable table,Object value, boolean isSelected, int row, int column)
    {
    if( row >= 0 && row < combos.length )
    {
    // We want the current value to be selected
    // Apropriate checks for null could be handled here as well.
    editor = combos[row];
    editor.setSelectedItem(value);
    return editor;
    }
    else
    {
    // If the row is outside our "scope" somehow we should throw a nice little error
    throw new IllegalArgumentException("Row is out of scope, no associated combobox");
    // Alternatives for this would be to:
    // a) Just let it throw the ArrayIndexOutOfBondsException
    // b) Run modulus % on the row and just keep on repeating the pattern
    }
    }
    }




Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories