0

I am creating a JTable in Java and I am iterating over a list called methodtraces2, for columns 10, 11, 12 and 13, I would like each cell to be made of a JComboBox. For example, each cell within column 10 should contain a combo box and each row within the combo box should contain one entry within the list methodtrace.getCallersList(), each cell within column 10 would be different depending on the content of methodtrace.getCallersList(), which is different for every iteration of methodtraces2.

I am taking all the values for each combo box and storing them within the array of String items1, which represents the content of the combo box for each row of the table (each iteration of methodtraces2).

  • items1 should populate comboBox1 at each iteration of methodtraces2
  • items2 should populate comboBox2
  • items3 should populate comboBox3
  • items4 should populate comboBox4

I need to do the same thing for columns 11, 12 and 13.

  • items2 contains the values of the combo box for column 11
  • items3 contains the values of the combo box for column 12
  • items4 contains the values of the combo box for each row within column 14.

The method getCellEditor is responsible for assigning combo boxes to the appropriate columns, right now, I have the same combo box content input for all the rows within column 10.

 public class TableComboBoxByRow extends JFrame
{
List<TableCellEditor> editors = new ArrayList<TableCellEditor>(4);
static List<MethodTrace2> methodtraces2= new ArrayList<MethodTrace2>(); 


public TableComboBoxByRow() throws SQLException
{
    DatabaseReading2 db = new DatabaseReading2(); 
    DatabaseReading2.MakePredictions();
    methodtraces2= db.getMethodtraces2(); 
    int j=0; 
    String[] items1 = new String [methodtraces2.size()]; 
    String[] items2 = new String [methodtraces2.size()]; 
    String[] items3 = new String [methodtraces2.size()]; 
    String[] items4 = new String [methodtraces2.size()]; 
 Object[][] data = new Object[methodtraces2.size()][10000]; 
    // Create the editors to be used for each row
    for(MethodTrace2 methodtrace: methodtraces2) {
        data[j][0]= methodtrace.MethodRepresentation.getMethodid(); 
        data[j][1]= methodtrace.MethodRepresentation.getMethodname(); 
        data[j][2]= methodtrace.Requirement.getID(); 
        data[j][3]= methodtrace.Requirement.getRequirementName(); 
        data[j][4]= methodtrace.ClassRepresentation.classid; 
        data[j][5]= methodtrace.ClassRepresentation.classname; 
        data[j][6]= methodtrace.gold; 
        data[j][7]= methodtrace.subject; 
        data[j][8]= methodtrace.goldpredictionCaller; 
        data[j][9]= methodtrace.goldpredictionCallee; 

        int i=0; 
        items1 = new String[methodtrace.getCallersList().size()]; 
         for(Method2Representation caller: methodtrace.getCallersList()) {
              items1[i]=caller.toString(); 
              System.out.println(caller.toString());
              i++; 

          }
        // data[j][10]=items1; 
         int k=0; 
         items2 = new String[ methodtrace.getCalleesList().size()]; 
         for(Method2Representation caller: methodtrace.getCalleesList()) {
              items2[k]=caller.toString(); 
              System.out.println(caller.toString());
              k++; 

          }
         int r=0; 
         items3 = new String[methodtrace.getCallersListExecuted().size()]; 
         for(Method2Representation caller: methodtrace.getCallersListExecuted()) {
              items3[r]=caller.toString(); 
              System.out.println(caller.toString());
              r++; 

          }
         int z=0; 
         items4 = new String[10000]; 
         for(Method2Representation caller: methodtrace.getCalleesListExecuted()) {
              items4[z]=caller.toString(); 
              System.out.println(caller.toString());
              z++; 

          }




            JComboBox comboBox1 = new JComboBox( items1 );
            DefaultCellEditor dce1 = new DefaultCellEditor( comboBox1 );
            editors.add( dce1 );

            JComboBox comboBox2 = new JComboBox( items2 );
            DefaultCellEditor dce2 = new DefaultCellEditor( comboBox2 );
            editors.add( dce2 );


            JComboBox comboBox3 = new JComboBox( items3 );
            DefaultCellEditor dce3 = new DefaultCellEditor( comboBox3 );
            editors.add( dce3 );


            JComboBox comboBox4 = new JComboBox( items4);
            DefaultCellEditor dce4 = new DefaultCellEditor( comboBox4 );
            editors.add( dce4 );



         j++; 
    }






    String[] columnNames = {"MethodID","MethodName", "RequirementID", "RequirementName", "ClassID", "ClassName", "Gold", "Subject", "GoldPredictionCaller", "GoldPredictionCallee", 
            "Callers", "CallersExecuted", "Callees", "CalleesExecuted"};
    DefaultTableModel model = new DefaultTableModel(data, columnNames);
    JTable table = new JTable(model)
    {
        //  Determine editor to be used by row
        public TableCellEditor getCellEditor(int row, int column)
        {
            int modelColumn = convertColumnIndexToModel( column );

            if (modelColumn == 10 && row < methodtraces2.size())
                return editors.get(0);

            else
                return super.getCellEditor(row, column);
        }
    };
    table.getColumnModel().getColumn(10).setWidth(10000);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
    JScrollPane scrollPane = new JScrollPane( table );
    getContentPane().add( scrollPane );
}



class ComboBoxRenderer extends JComboBox implements TableCellRenderer
{

    public ComboBoxRenderer()
    {
        setBorder(new EmptyBorder(0, 0, 0, 0));
    }

    public Component getTableCellRendererComponent(
        JTable table, Object value, boolean isSelected,
        boolean hasFocus, int row, int column)
    {
        removeAllItems();
        addItem( value );
        return this;
    }
}


public static void main(String[] args) throws SQLException
{

    TableComboBoxByRow frame = new TableComboBoxByRow();
    frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
    frame.pack();
    frame.setVisible(true);
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • After editing & reformatting that wall of text, I still see no question. What is your question? – Andrew Thompson Jun 11 '18 at 05:48
  • Maybe this answer: https://stackoverflow.com/questions/4211452/how-to-add-unique-jcomboboxes-to-a-column-in-a-jtable-java/4211552#4211552 will give you some ideas. – camickr Jun 11 '18 at 19:44

1 Answers1

0

I am not sure I fully understand what is going on here, but you mentioned that you are iterating over a list called methodtraces2 however in the code you posted:

 for(Method2Representation caller: methodtrace (NOT methodtrace2!).getCallersList()) {
    items1[i]=caller.toString(); 
    System.out.println(caller.toString());
    i++; 

  }

You are in fact iterating over methodTrace (note: not methodTraces2). An additional thing to try when debugging these kinds of problems is to simplify such that:

 for(Method2Representation caller: methodtrace.getCallersList()) {
    items1[i]=caller.toString(); 
    System.out.println(caller.toString());
    i++; 

  }

can be changed to something like:

 String stuff[] = {"1", "2", "3", .... "N"};
 for(String s : stuff) {
    items1[i]=s
    System.out.println("testing items1[" + i + "]: " + items[i]);
    i++; 
  }

A final note, I hope you are changing the variable names to protect some proprietary code because those variable names will end up driving anyone mad! Use descriptive variable names, instead of items1, use callers or items2 use callees.

HH :)

bowzerfood
  • 29
  • 4