Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How To Configure Jcombobox In A Jtable To Get An Expression

jtable jcombobox jcelleditor combobox

  • Please log in to reply
2 replies to this topic

#1 AndreaFar

AndreaFar

    CC Regular

  • Member
  • PipPipPip
  • 26 posts

Posted 21 June 2012 - 07:15 AM

I cannot configure a jtable with jcombobox, I have seen various sample but without success.
I have a table with 3 columns and Jcombobox for each cell.
In the first and last JCombobox there are Sports and in the central a Criteria.
For each row I must use the values of the 3 column to make an Expression`(Sport s1, Criteria c, Sport s2);
I have tried to use a custom table model but with jcombobox I don't know how to configure
@Override
      public Object getValueAt(int arg0, int arg1) {
       // TODO Auto-generated method stub
       return null;
      }
and
public Object setValueAt(int arg0, int arg1) {
	       // TODO Auto-generated method stub
	       return null;
	      }
to get and set the values with JCombobox selections.

`

I use this model to implements my Table:
class MyTableModel extends AbstractTableModel {
      private static final long serialVersionUID = 1L;
      int numSports = DataSavedSports.loadNumeroSports();
      private final List<Sport> objects = DataSavedSports
    	.loadListSports();
   
      private final String[] columnNames = { "Name Sport 1", "Criteria",
    	"Name Sport 2" };
   
      private final Class<?>[] metaModell = new Class[] { String.class,
    	Integer.class };
   
      public int getColumnCount() {
       return columnNames.length;
      }
   
      public int getRowCount() {
       return objects.size();
      }
   
      @Override
      public String getColumnName(int col) {
       return columnNames[col];
      }
   
   
   
      private Sport getRow(int row) {
       return objects.get(row);
      }
   
      @Override
      public Class<?> getColumnClass(int c) {
       if (c < metaModell.length)
    	return metaModell[c];
       return Object.class;
      }
   
      @Override
      public boolean isCellEditable(int row, int col) {
       return true;
      }
   
   
   
      public List<Sport> getJobs() {
       return objects;
      }
   
      public void addRow(Sport v) {
       this.objects.add(v);
       fireTableDataChanged();
      }
   
      @Override
      public Object getValueAt(int arg0, int arg1) {
       // TODO Auto-generated method stub
       return null;
      }
     }
     
   
The methods that I have used to set the Jcombobox are something like this
     
     
     
     
      public void setColumnSports1(JTable table, TableColumn ColumnLav1) {
      // Set up the editor for the sport cells.
      final List<Sport> ListSports = DataSavedSports.loadListSports();
      final JComboBox comboBox1 = new JComboBox();
      for(Sport l : ListSports){
       comboBox1.addItem(l.getIdSport());
      }
      
      comboBox1.addItemListener(new ItemListener(){
    	 public void itemStateChanged(ItemEvent ie){
    	  String str = (String)comboBox1.getSelectedItem();
       
    	 
    	
    	  System.out.println("TESTTTT"+str);
    	  }
    	 });
      
      ColumnLav1.setCellEditor(new DefaultCellEditor(comboBox1));
   
      // Set up tool tips for the sport cells.
      DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
      renderer.setToolTipText("Click for combo box");
      ColumnLav1.setCellRenderer(renderer);
     }


PS
I must use the table in a JFrame and get the data of selected Jcombobox item, so the missing of the multiple inheritance, is another problem because of the overriding of various component.
  • 0

#2 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 21 June 2012 - 09:38 AM

new DefaultCellEditor(JComboBox) automatically adds a delegate to the JComboBox, so there's no need for you to add the ItemListener to it. Reference this tutorial to see how it's done:
http://docs.oracle.c...ents/table.html

  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3 AndreaFar

AndreaFar

    CC Regular

  • Member
  • PipPipPip
  • 26 posts

Posted 21 June 2012 - 04:12 PM

new DefaultCellEditor(JComboBox) automatically adds a delegate to the JComboBox, so there's no need for you to add the ItemListener to it. Reference this tutorial to see how it's done:
http://docs.oracle.c...ents/table.html


Thanks for the reply.
I have seen this tutorial but for some reason the implementation that I have posted doesn't update the Jcombobox selection when I choose an item. There is something wrong, but I haven't understand where
  • 0





Also tagged with one or more of these keywords: jtable, jcombobox, jcelleditor, combobox

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download