Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Connecting JComboBox and JTable

holodMar 6 2007 — edited Mar 9 2007
I set ComboBoxItem as TableCellRenderer.
My ComboBoxItem is like JcomboBox but it can keep two values.
One of them (Description) it shows to user and the other one (ID)it sends to table.
ComboBoxItem is like JcomboBox(<>Vector).Elements of vector are class Item.
Item has two variables:
ID(int)
Description(String)


The problem is:

Column, where ComboBoxItem set as DefaultCellRenderer keeps ID.
I want ComboBoxItem show appropriate Description for Id form table cell.
Now, this column shows just ID, no appropriate description to this ID.

The situation is:
When I load JTable, I see ID in column where ComboBoxItem is set as DefaultCellRenderer.
When I click on cell from this column, I can get list od Descriptions.
Then ComboBox sends ID of chosen description to Jtable and JTable stores new value.

The problem is that ComboBoxItem does not "see" values in JTable cell.
ComboBoxItem can only send right ID to sell.
I want it to read ID from sell and set option corresponding to ID in cell.

Here is the pict (very small, 92,5 Kb)

http://foto.mail.ru/mail/ice_holod/448/s-450.jpg


Here is the code where I create Jtable:
 
public class TableCreate extends JPanel{
    private JTableAdapter jta;
   
   /** Creates a new instance of TableCreate */
    public TableCreate() {
        super(new GridLayout(1,0));
        LoadDriver loaddrv = new LoadDriver ("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/Document?", "root", "");
        loaddrv.setNewConnectionToDB();//connection to DB
        //creating JTable form TableAbstractModel
        /***/this.jta = new JTableAdapter(loaddrv.getAliveConnection());
        /***/JTable table = new JTable(jta);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        table.setPreferredScrollableViewportSize(new Dimension(500,210));
        
         JScrollPane scrollpane = new JScrollPane(table,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
         initColumnSizes(table);
        //set new renderer for table column
         setSheetTypeComboBoxItem(table, table.getColumnModel().getColumn(4),loaddrv.getAllSheetType() );
         add(scrollpane);
                
    }
//....and so on.....
And the method (it is in the same class):

 public void setSheetTypeComboBoxItem(JTable table, TableColumn SheetTypeIDcolumn,Vector inputvector){
        //creating ComboBoxItem values from Vector
        ComboBoxItem combobox = new ComboBoxItem(inputvector);
        SheetTypeIDcolumn.setCellEditor(new DefaultCellEditor(combobox.getComboBoxItem()));
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setToolTipText("Press to choose other value");
        SheetTypeIDcolumn.setCellRenderer(renderer);
    
    }//setSheetTypeComboBoxItem
So I hope I've explained problem clearly.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 4 2007
Added on Mar 6 2007
11 comments
1,079 views