Hola,
esto te puede servir Creamos una constante tipo String para el PreparedStatement nuestra sentencia precompilada private static final String UPDATE
= "UPDATE customer SET first_name=?, last_name=? WHERE id=?";
Añadimos una columna que contendrá nuestro botóndefaultTableModel.addColumn("Update item");
Obtenemos el nombre de la columna y le seteamos los 2 objetos de las clases que permitiran el updatejTable.getColumn("Update item").setCellEditor(new UpdateItem());
jTable.getColumn("Update item").setCellRenderer(new BotonRenderizado());
Añadimos un mouseListener a la tabla para setear los valores en el form con el click , tu debes hacer el ajuste con tu código @Override
int seletedRow = jTable.getSelectedRow();
jTextFieldID.setText(defaultTableModel.getValueAt(seletedRow, 0).toString());
jTextFirsName.setText(defaultTableModel.getValueAt(seletedRow, 1).toString());
jTextLastName.setText(defaultTableModel.getValueAt(seletedRow, 2).toString());
}
});
Creamos una clase que extiende de JButton
@Override
public Component getTableCellRendererComponent
(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column
) { if (value != null) {
setText("");
} else {
setText("update");
}
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground
(UIManager.
getColor("Button.background")); }
return this;
}
}
Aquí es donde esta la verdadera lógica del botón en la tabla desde la linea 21 a 38 es un ejemplo que te pongo, tu debes de programarlo acorde a tu códigoen la linea 33 puedes usar también DbUtils para ahorrarte mas lineas de código, sirve para refrescar la tabla
private int fila;
public UpdateItem() {
updateItem();
}
private void updateItem() {
editor.addActionListener(e -> {
if (table != null) {
fireEditingStopped();
try {
int getrow = table.getSelectedRow();
final Long id
= Long.
valueOf(jTextFieldID.
getText()); final String firstName
= jTextFirsName.
getText(); final String lastName
= jTextLastName.
getText();
ps.setString(1, firstName);
ps.setString(2, lastName);
ps.setLong(3, id);
ps.executeUpdate();
//DbUtils
model.setValueAt(id, getrow, 0);
model.setValueAt(firstName, getrow, 1);
model.setValueAt(lastName, getrow, 2);
ex.printStackTrace();
}
}
}
});
}
@Override
return true;
}
@Override
public Object getCellEditorValue
() { return value;
}
@Override
public Component getTableCellEditorComponent
(JTable table,
Object value,
boolean isSelected,
int row,
int column
) { this.table = table;
this.fila = row;
this.value = value;
if (value != null) {
editor.setText("");
} else {
editor.setText("update");
}
if (isSelected) {
editor.setForeground(table.getSelectionForeground());
editor.setBackground(table.getSelectionBackground());
} else {
editor.setForeground(table.getForeground());
editor.
setBackground(UIManager.
getColor("Button.background")); }
return editor;
}
}
package foro;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableModel;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.*;
import java.util.EventObject;
public class ConnectJDBCTest
extends JFrame {
private static final String URL = "jdbc:mysql://localhost:3306/demo?useSSL=false"; private static final String USERNAME
= "root"; //mala practica private static final String PASSWORD
= "password"; // mala practica
private static final String SELECT
= "SELECT id, first_name, last_name FROM customer"; private static final String UPDATE
= "UPDATE customer SET first_name=?, last_name=? WHERE id=?"; private static final String DELETE
= "DELETE FROM customer where id = ";
public ConnectJDBCTest() {
initLayout();
initConnection();
}
private void initLayout() {
defaultTableModel.addColumn("Id");
defaultTableModel.addColumn("First Name");
defaultTableModel.addColumn("Last Name");
defaultTableModel.addColumn("Update item");
@Override
int seletedRow = jTable.getSelectedRow();
jTextFieldID.setText(defaultTableModel.getValueAt(seletedRow, 0).toString());
jTextFirsName.setText(defaultTableModel.getValueAt(seletedRow, 1).toString());
jTextLastName.setText(defaultTableModel.getValueAt(seletedRow, 2).toString());
}
});
jTable.getColumn("Update item").setCellEditor(new UpdateItem());
jTable.getColumn("Update item").setCellRenderer(new BotonRenderizado());
jPanelSur.
add(new JLabel("ID")); jPanelSur.add(jTextFieldID);
jPanelSur.
add(new JLabel("First Name")); jPanelSur.add(jTextFirsName);
jPanelSur.
add(new JLabel("Last Name")); jPanelSur.add(jTextLastName);
add(jPanel);
setDefaultCloseOperation
(JFrame.
EXIT_ON_CLOSE); setLocationRelativeTo(null);
pack();
setVisible(true);
}
private void initConnection() {
try {
final Statement statement
= connection.
createStatement(); final ResultSet resultSet
= statement.
executeQuery(SELECT
); while (resultSet.next()) {
id = resultSet.getLong("id");
firstName = resultSet.getString("first_name");
lastName = resultSet.getString("last_name");
defaultTableModel.
addRow(new Object[]{id, firstName, lastName
}); }
e.printStackTrace();
}
}
private int fila;
public UpdateItem() {
updateItem();
}
private void updateItem() {
editor.addActionListener(e -> {
if (table != null) {
fireEditingStopped();
try {
int getrow = table.getSelectedRow();
final Long id
= Long.
valueOf(jTextFieldID.
getText()); final String firstName
= jTextFirsName.
getText(); final String lastName
= jTextLastName.
getText();
ps.setString(1, firstName);
ps.setString(2, lastName);
ps.setLong(3, id);
ps.executeUpdate();
//DbUtils
model.setValueAt(id, getrow, 0);
model.setValueAt(firstName, getrow, 1);
model.setValueAt(lastName, getrow, 2);
ex.printStackTrace();
}
}
}
});
}
@Override
return true;
}
@Override
public Object getCellEditorValue
() { return value;
}
@Override
public Component getTableCellEditorComponent
(JTable table,
Object value,
boolean isSelected,
int row,
int column
) { this.table = table;
this.fila = row;
this.value = value;
if (value != null) {
editor.setText("");
} else {
editor.setText("update");
}
if (isSelected) {
editor.setForeground(table.getSelectionForeground());
editor.setBackground(table.getSelectionBackground());
} else {
editor.setForeground(table.getForeground());
editor.
setBackground(UIManager.
getColor("Button.background")); }
return editor;
}
}
public static void main
(String...
blabla) { try {
if (osType.contains("Win")) {
UIManager.
setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } else if (osType.contains("Linux")) {
UIManager.
setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); }
//do nothing
}
new ConnectJDBCTest();
}).start();
}
}
Selecciona primero un item, editalo , luego actualiza