JTable & JComboBox

Hi, I have a problem with JTable when I want to insert a JComboBox.

My table has got five (of ten) column with JComboBox. The problem appears when i'm programming the aplication ( case 1 ) or when i want to insert values in the JComboBox ( case 2 )

Two cases ( two forms to program this code):

case (1) : In my class Control's Events, i have programmed:

void setUpColumnOrganizacion(JTable table,TableColumn organizacionColumn,ArrayList organizacion){

JComboBox comboBox = new JComboBox();

Iterator it = organizacion.iterator();

String nombre = null ;

while (it.hasNext()){

nombre = (String)it.next();

if (!duplicate(nombre,comboBox)){

comboBox.addItem(nombre);

}

}

organizacionColumn.setCellEditor(new DefaultCellEditor(comboBox));

//Set up tool tips for the sport cells.

DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();

renderer.setToolTipText("Haz clic para elegir organizacion");

organizacionColumn.setCellRenderer(renderer);

}

But the problem is that exist the "row 0 ( for example ) column 8"( JComboBox ) when you select an item, the program queries the Data Base and in the column 9 "( another JComboBox) insert others values. And how to i have programmed the another column with seemed code, the aplication insert into column the same values and i want only in the row 0 column 9.

Another form > Case (2), i am seeing examples of this page and i have found the next

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

import javax.swing.table.*;

public class TableRenderDemo extends JFrame

{

ArrayList editors = new ArrayList(3);

public TableRenderDemo()

{

// Create the editors to be used for each row

LINE 1>>>>>>>> String[] items1 = { "Red", "Blue", "Green" };

JComboBox comboBox1 = new JComboBox( items1 );

DefaultCellEditor dce1 = new DefaultCellEditor( comboBox1 );

editors.add( dce1 );

LINE 2>>>>>>>> String[] items2 = { "Circle", "Square", "Triangle" };

JComboBox comboBox2 = new JComboBox( items2 );

DefaultCellEditor dce2 = new DefaultCellEditor( comboBox2 );

editors.add( dce2 );

LINE 3>>>>>>>>String[] items3 = { "Apple", "Orange", "Banana" };

JComboBox comboBox3 = new JComboBox( items3 );

DefaultCellEditor dce3 = new DefaultCellEditor( comboBox3 );

editors.add( dce3 );

// Create the table with default data

Object[][] data =

{

{"Color", "Red"},

{"Shape", "Square"},

{"Fruit", "Banana"},

{"Plain", "Text"}

};

String[] columnNames = {"Type","Value"};

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)

{

if (column == 1 && row < 3)

{

return (TableCellEditor)editors.get(row);

}

else

return super.getCellEditor(row, column);

}

};

JScrollPane scrollPane = new JScrollPane( table );

getContentPane().add( scrollPane );

}

public static void main(String[] args)

{

TableRenderDemo frame = new TableRenderDemo();

frame.setDefaultCloseOperation( EXIT_ON_CLOSE );

frame.pack();

frame.setVisible(true);

}

}

The problem in the two case is that the values of LINES 1, LINE 2 and LINE 3 are predefined, and how to say before, the program queries the Data Base.

What is the best form ? have I been mistaken in something ?

Sorry for my English!

Thanks.

[3857 byte] By [pakkkAttacka] at [2007-11-26 20:07:05]
# 1

Use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

> The problem in the two case is that the values of LINES 1, LINE 2 and

> LINE 3 are predefined, and how to say before, the program queries the Data Base.

I don't see how this is a problem. Build your database query and build the combo boxes dynamically.

Also you don't have to use a ArrayList to hold the editors. You could use a Hashmap and use the row number as the key to the hashmap.

camickra at 2007-7-9 23:09:01 > top of Java-index,Desktop,Core GUI APIs...
# 2
Hi, thanks for your answer, but i follow with problems because in the case 2, how I make to update a JComboBox? that is to say, to insert values in this component after making the query in the data base ...It's the principal problem.Thanks a lot.
pakkkAttacka at 2007-7-9 23:09:01 > top of Java-index,Desktop,Core GUI APIs...
# 3
Read the JComboBox API. Use the addItem(...) method.
camickra at 2007-7-9 23:09:01 > top of Java-index,Desktop,Core GUI APIs...
# 4

Ok, I know this operation, my problem is how to obtain the JComboBox related to this cell of my JTable, example:

In the definition of GUI: (tablaV < JTable )

tablaV = new JTable(modTablaV){

public TableCellEditor getCellEditor(int row, int col){

if (col == 0 || col == 2 || col == 3 || col == 4 || col == 6){

return super.getCellEditor(row, col);

}

else {

return new DefaultCellEditor(new JComboBox());

}

}

};

But in the Event Controller I don't know to put in the code:

((JComboBox)ventanaModificacion.tablaV.getCellEditor(i, 1)).addItem(nombreOrganizacion1);

This example would be for column 1 ( JComboBox ) , ventanaModificacion is the reference of the GUI and nombreOrganizacion1 is a String

Thanks

pakkkAttacka at 2007-7-9 23:09:01 > top of Java-index,Desktop,Core GUI APIs...
# 5
Sorry...it's my problem: 靠靠?((JComboBox)ventanaModificacion.tablaV.getCellEditor(i, 1)).addItem(nombreOrganizacion1); ?Thanks.
pakkkAttacka at 2007-7-9 23:09:01 > top of Java-index,Desktop,Core GUI APIs...
# 6
Please, I need your help... :(Thanks
pakkkAttacka at 2007-7-9 23:09:01 > top of Java-index,Desktop,Core GUI APIs...