galera to querendo excluir dados da minha tabela e to com duvida com eu coloco
AbstractTableModel
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
public class FuncionarioTM extends AbstractTableModel{
private List<Funcionario> linhas;
private String[] colunas = new String[]{"Mat.","Nome","CPF"};
public FuncionarioTM() {
linhas = new ArrayList<Funcionario>();
}
public FuncionarioTM(List<Funcionario> lista) {
linhas = new ArrayList<Funcionario>(lista);
}
@Override
public int getColumnCount() {
return colunas.length;
}
@Override
public int getRowCount() {
return linhas.size();
}
@Override
public String getColumnName(int columnIndex) {
return colunas[columnIndex];
}
@Override
public Class<?> getColumnClass(int columnIndex) {
/*
switch(columnIndex){
case 0:
return Integer.class;
default:
return String.class;
}
*/
return String.class;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Funcionario f = linhas.get(rowIndex);
switch (columnIndex) {
case 0:
return f.getMatricula();
case 1:
return f.getNome();
case 2:
return f.getCPF();
default:
throw new IndexOutOfBoundsException("columnIndex out of bounds");
}
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
Funcionario f = linhas.get(rowIndex);
switch (columnIndex) {
case 0:
f.setMatricula(aValue.toString());
break;
case 1:
f.setNome(aValue.toString());
break;
case 2:
f.setCPF(aValue.toString());
break;
default:
}
fireTableCellUpdated(rowIndex, columnIndex);
}
public void setValueAt(Funcionario aValue, int rowIndex) {
Funcionario f = linhas.get(rowIndex);
f.setMatricula(aValue.getMatricula());
f.setNome(aValue.getNome());
f.setCPF(aValue.getCPF());
fireTableCellUpdated(rowIndex, 0);
fireTableCellUpdated(rowIndex, 1);
fireTableCellUpdated(rowIndex, 2);
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
public Funcionario getFuncionario(int indiceLinha) {
return linhas.get(indiceLinha);
}
public void addFuncionario(Funcionario f) {
linhas.add(f);
int ultimoIndice = getRowCount() - 1;
fireTableRowsInserted(ultimoIndice, ultimoIndice);
}
public void remove(int indiceLinha) {
linhas.remove(indiceLinha);
fireTableRowsDeleted(indiceLinha, indiceLinha);
}
public void addLista(List<Funcionario> f) {
int tamanhoAntigo = getRowCount();
linhas.addAll(f);
fireTableRowsInserted(tamanhoAntigo, getRowCount() - 1);
}
public void limpar() {
linhas.clear();
fireTableDataChanged();
}
public boolean isEmpty() {
return linhas.isEmpty();
}
}
jframe
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
public class Principal extends javax.swing.JFrame {
FuncionarioTM Modelo;
public Principal() {
initComponents();
setLocationRelativeTo(null);
Modelo = new FuncionarioTM();
tbl_funcionarios.setModel(Modelo);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
btn_cadastra = new javax.swing.JButton();
c_mat = new javax.swing.JTextField();
c_nome = new javax.swing.JTextField();
c_cpf = new javax.swing.JFormattedTextField();
btn_limpar = new javax.swing.JButton();
btn_excluir = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
tbl_funcionarios = new javax.swing.JTable();
btn_sair = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Funcionário"));
jLabel1.setText("Matrícula:");
jLabel2.setText("Nome:");
jLabel3.setText("CPF:");
btn_cadastra.setText("Cadastrar");
btn_cadastra.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_cadastraActionPerformed(evt);
}
});
c_mat.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
c_matKeyTyped(evt);
}
});
c_nome.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
c_nomeKeyTyped(evt);
}
});
try {
c_cpf.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
} catch (java.text.ParseException ex) {
ex.printStackTrace();
}
btn_limpar.setText("Limpar");
btn_limpar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_limparActionPerformed(evt);
}
});
btn_excluir.setText("Excluir");
btn_excluir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_excluirActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel3)
.addComponent(jLabel2)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(c_mat)
.addComponent(c_nome)
.addComponent(c_cpf)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(136, 136, 136)
.addComponent(btn_cadastra)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btn_limpar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btn_excluir)
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(c_mat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(c_nome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(c_cpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btn_limpar)
.addComponent(btn_cadastra)
.addComponent(btn_excluir))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
tbl_funcionarios.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Mat.", "Nome", "CPF"
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.String.class, java.lang.String.class
};
boolean[] canEdit = new boolean [] {
false, false, false
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
tbl_funcionarios.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
tbl_funcionariosMouseClicked(evt);
}
});
jScrollPane1.setViewportView(tbl_funcionarios);
if (tbl_funcionarios.getColumnModel().getColumnCount() > 0) {
tbl_funcionarios.getColumnModel().getColumn(0).setPreferredWidth(50);
tbl_funcionarios.getColumnModel().getColumn(1).setPreferredWidth(200);
tbl_funcionarios.getColumnModel().getColumn(2).setPreferredWidth(100);
}
btn_sair.setText("Sair");
btn_sair.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_sairActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn_sair, javax.swing.GroupLayout.Alignment.TRAILING))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btn_sair))
);
pack();
}// </editor-fold>
private void btn_cadastraActionPerformed(java.awt.event.ActionEvent evt) {
Validar();
Funcionario F = new Funcionario();
F.setMatricula(c_mat.getText());
F.setNome(c_nome.getText());
F.setCPF(c_cpf.getText());
Modelo.addFuncionario(F);
}
public void Validar(){
if(c_nome.getText().equals(""))
{
JOptionPane.showMessageDialog(null,"campo Nome obrigatorio","Aviso",JOptionPane.WARNING_MESSAGE);
c_nome.requestFocus();
}
if(c_cpf.getText().equals(" . . - "))
{
JOptionPane.showMessageDialog(null,"campo CPF obrigatorio","Aviso",JOptionPane.WARNING_MESSAGE);
c_cpf.requestFocus();
}
if(c_mat.getText().equals(""))
{
JOptionPane.showMessageDialog(null,"campo RG obrigatorio", "Aviso", JOptionPane.WARNING_MESSAGE);
c_mat.requestFocus();
}
}
private void btn_sairActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void c_matKeyTyped(java.awt.event.KeyEvent evt) {
if(c_mat.getText().length()>=4)
{
JOptionPane.showMessageDialog(this,"só é aceito 4 digitos"); }
String caracteres="0123456789";
if(!caracteres.contains(evt.getKeyChar()+"")){
evt.consume();
}
}
private void c_nomeKeyTyped(java.awt.event.KeyEvent evt) {
if(c_nome.getText().length()>=50)
{
JOptionPane.showMessageDialog(this,"só é aceito 4 digitos"); }
String caracteres="aAbBcCdDeEfFgGhHiIjJlkKmMnNoOpPqQrRsStTuUvVwWxXyYzZ";
if(!caracteres.contains(evt.getKeyChar()+"")){
evt.consume();
}
}
private void btn_limparActionPerformed(java.awt.event.ActionEvent evt) {
Limpar();
}
private void btn_excluirActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTable
//System.out.println("linha selecionada: " + tbl_funcionarios.getSelectedRow());
/*int selecionada = tbl_funcionarios.getSelectedRow();
if (selecionada == -1) {
return;}*/
/*String cpf = tbl_funcionarios.getValueAt(linhaSelecionada, 0).toString()*/
}
private void tbl_funcionariosMouseClicked(java.awt.event.MouseEvent evt) {
}
public void Limpar(){
c_mat.setText("");
c_nome.setText("");
c_cpf.setText("");
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Principal().setVisible(true);
}
});
}
como eu faço ? por favor ajuda