Jcomboboxes in jtable select the same value..

Discussion in 'Java' started by 6e, Jul 29, 2005.

  1. 6e

    6e Guest

    Hi,

    Ive created a JTable that has one column that is full of JCombobox
    objects.

    The weird thing is that when I select one value in.. say row 3.., that
    value is used not only in row 3, but also row 4,5, and 6 and all the
    way to the bottom-1 of the table.

    I created a custom celleditor and cellrenderer, code I included below
    for your perusal...

    --------main app code-------------
    String[] values = new String[]{" ", "Heading", "Body"};

    table.getColumnModel().getColumn(table.convertColumnIndexToView(iFORMAT)).setCellEditor(new
    ComboBoxEditor(values));

    table.getColumnModel().getColumn(table.convertColumnIndexToView(iFORMAT)).setCellRenderer(new
    ComboBoxRenderer(values));



    ------------combobox editor---------------
    public class ComboBoxEditor extends DefaultCellEditor {
    public ComboBoxEditor(String[] items) {
    super(new JComboBox(items));
    }
    }


    --------------Combobox renderer----------
    ublic class ComboBoxRenderer extends JComboBox implements
    TableCellRenderer {
    public ComboBoxRenderer(String[] items) {
    super(items);
    }

    public Component getTableCellRendererComponent(JTable table, Object
    value,
    boolean isSelected, boolean hasFocus, int row, int column)
    {
    if (isSelected) {
    setForeground(table.getSelectionForeground());
    super.setBackground(table.getSelectionBackground());
    } else {
    setForeground(table.getForeground());
    setBackground(table.getBackground());
    }

    // Select the current value
    setSelectedItem(value);
    return this;
    }
    }
    6e, Jul 29, 2005
    #1
    1. Advertising

  2. 6e

    jan V Guest

    > The weird thing is that when I select one value in.. say row 3.., that
    > value is used not only in row 3, but also row 4,5, and 6 and all the
    > way to the bottom-1 of the table.


    Sounds like your combo boxes are sharing underlying data models...

    > I created a custom celleditor and cellrenderer, code I included below
    > for your perusal...
    >
    > --------main app code-------------
    > String[] values = new String[]{" ", "Heading", "Body"};
    >
    >

    table.getColumnModel().getColumn(table.convertColumnIndexToView(iFORMAT)).se
    tCellEditor(new
    > ComboBoxEditor(values));
    >
    >

    table.getColumnModel().getColumn(table.convertColumnIndexToView(iFORMAT)).se
    tCellRenderer(new
    > ComboBoxRenderer(values));


    You may get happier results if your renderers don't share the values
    array...
    jan V, Jul 29, 2005
    #2
    1. Advertising

  3. 6e

    6e Guest

    I appreciate the idea, I explored it previously by
    new ComboBoxEditor( new String[]{" ", "Heading", "Body"}; ... but it
    didn't seem to work...

    unless I just don't understand what you're trying to tell me... If
    this is the case, please explain further.. or let me know if you have
    any other ideas?..

    Thanks!
    6e, Jul 29, 2005
    #3
    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. Tivo Escobar
    Replies:
    1
    Views:
    7,256
    manusa
    Apr 12, 2007
  2. Tintin92
    Replies:
    1
    Views:
    1,671
    Andrew Thompson
    Feb 14, 2007
  3. RC
    Replies:
    5
    Views:
    859
    Daniele Futtorovic
    Jan 30, 2008
  4. palmiere
    Replies:
    1
    Views:
    376
    Erwin Moller
    Feb 9, 2004
  5. Haircuts Are Important
    Replies:
    3
    Views:
    296
    Haircuts Are Important
    Jun 4, 2013
Loading...

Share This Page