I have a pogram in java where I have a JFrame with a Jtable with 10 rows and 4 columns in the third column is a jcombobox that goes to DB to fetch the values that belong to column 3 for the values entered in column 2 and 1 of that row. Here is the code:
//JCOMBOBOX CREATED
TableColumn sportColumn = table.getColumnModel().getColumn(3);
JComboBox comboBox = new JComboBox();
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
comboBox.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent event) {
try{
int comboboxRow = table.getSelectedRow();
String OFNUPK = (String) table.getValueAt(comboboxRow, 1);
String TFCCMP = (String) table.getValueAt(comboboxRow, 2);
// Se o valor for nulo ou não inteiro isto vai atirar erro
int OFNUP = Integer.parseInt(OFNUPK);
// Verificar se o valor é válido
if (TFCCMP != null && !TFCCMP.isEmpty()) {
Connection con = DriverManager.getConnection("jdbc:as400://" + host, user, pwd);
// create new statement from connection
Statement stmt = con.createStatement();
// Se puderes aqui usa prepared statements como te mostrei ontem
String s = "SELECT DISTINCT a.TFCPAI FROM $$CLI00F55.FOFFAN a, SICGA00F55.FORFAB f WHERE "
+ "a.TFCCMP = '" + TFCCMP + "' AND f.OFNUPK = " + OFNUP +" AND f.OFNUPK = a.TFSNOF";
ResultSet rs = stmt.executeQuery(s);
//APAGAR OS DADOS ANTERIORES
while(rs.next())
{
comboBox.addItem(rs.getString(1));
}
I'm having 2 problems when clicking on the Jcombobox in the same row its adding the same values. And in the next row the data that appears in the previous row also appear.
If you need more information please comment. Thank you!