Boa noite pessoal;
Sou iniciante em java, gostaria de saber como fazer para para chamar uma JTable dentro de um JFrame. Eu não estou usando o DefaultTableModel, tenho uma classe chamada ModeloTabela, e um formulário foi feito tudo usando o eclipse manualmente sem por motivo de aprendizagem e conhecer melhor os comandos, outra coisa eu não consigo popular a tabela vindo com os registro do banco de dados.
// Formulario JFrame
package br.com.clinica.view;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import br.com.clinica.ADO.ADOEstado;
import br.com.clinica.model.ModelEstado;
import br.com.clinica.util.Conexao;
import br.com.clinica.util.ModeloTabela;
public class TelaEstado extends JFrame {
Conexao conecta = new Conexao();
ModelEstado model = new ModelEstado();
ADOEstado ado = new ADOEstado();
Font Titulo = new Font("Serif", Font.BOLD, 25);
private JLabel lblTitulo, lblcodigo, lblestado, lblUF;
private JTextField txtcodigo, txtestado, txtUF;
private JButton btnNovo, btnSalvar, btnEditar, btnExcluir, btnCancelar;
public TelaEstado() {
conecta.conexao();
// Label Titulo
lblTitulo = new JLabel("Manutenção de Estado Brasileiros");
lblTitulo.setForeground(Color.RED);
lblTitulo.setFont(Titulo);
lblTitulo.setBounds(70, 5, 580, 30);
add(lblTitulo);
// Label codigo estado
lblcodigo = new JLabel("Código: ");
lblcodigo.setBounds(10, 38, 102, 20);
add(lblcodigo);
lblcodigo.setForeground(Color.BLACK);
// Text Nome estado
txtcodigo = new JTextField("");
txtcodigo.setBounds(10, 60, 80, 20);
add(txtcodigo);
txtcodigo.setBackground(Color.YELLOW);
txtcodigo.setForeground(Color.BLACK);
txtcodigo.setEditable(false);
txtcodigo.setEnabled(false);
// Label Estado
lblestado = new JLabel("Estado: ");
lblestado.setBounds(100, 38, 102, 20);
add(lblestado);
lblestado.setForeground(Color.BLACK);
// Text Nome estado
txtestado = new JTextField("");
txtestado.setBounds(100, 60, 300, 20);
add(txtestado);
txtestado.setBackground(Color.YELLOW);
txtestado.setForeground(Color.BLACK);
txtestado.setEnabled(false);
// Label UF
lblUF = new JLabel("UF: ");
lblUF.setBounds(408, 38, 102, 20);
add(lblUF);
lblUF.setForeground(Color.BLACK);
// Text UF
txtUF = new JTextField("");
txtUF.setBounds(408, 60, 50, 20);
add(txtUF);
txtUF.setBackground(Color.YELLOW);
txtUF.setForeground(Color.BLACK);
txtUF.setEnabled(false);
// Botões
btnNovo = new JButton("Novo");
btnNovo.setBounds(10, 280, 80, 30);
btnNovo.setMnemonic('O');
btnNovo.setToolTipText("Novo");
btnNovo.setForeground(Color.BLACK);
btnNovo.setBackground(Color.lightGray);
add(btnNovo);
btnSalvar = new JButton("Salvar");
btnSalvar.setBounds(100, 280, 80, 30);
btnSalvar.setMnemonic('O');
btnSalvar.setToolTipText("Salvar");
btnSalvar.setForeground(Color.BLACK);
btnSalvar.setBackground(Color.lightGray);
add(btnSalvar);
btnSalvar.setEnabled(false);
btnEditar = new JButton("Editar");
btnEditar.setBounds(190, 280, 80, 30);
btnEditar.setMnemonic('O');
btnEditar.setToolTipText("Editar");
btnEditar.setForeground(Color.BLACK);
btnEditar.setBackground(Color.lightGray);
add(btnEditar);
btnExcluir = new JButton("Excluir");
btnExcluir.setBounds(280, 280, 80, 30);
btnExcluir.setMnemonic('O');
btnExcluir.setToolTipText("Excluir");
btnExcluir.setForeground(Color.BLACK);
btnExcluir.setBackground(Color.lightGray);
add(btnExcluir);
btnCancelar = new JButton("Cancelar");
btnCancelar.setBounds(370, 280, 85, 30);
btnCancelar.setMnemonic('O');
btnCancelar.setToolTipText("Cancelar");
btnCancelar.setForeground(Color.BLACK);
btnCancelar.setBackground(Color.lightGray);
add(btnCancelar);
setLayout(null);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
getContentPane().setBackground(Color.WHITE);
setTitle("Cadastro e Manutenção de Estado Brasileiro");
setResizable(false);
setSize(800, 600);
setVisible(true);
setLocationRelativeTo(null);
btnNovo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
txtestado.setText("");
txtUF.setText("");
txtestado.setEnabled(true);
txtUF.setEnabled(true);
btnSalvar.setEnabled(true);
}
});
btnSalvar.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
model.setEstado(txtestado.getText().toUpperCase());
model.setUf(txtUF.getText().toUpperCase());
ado.SalvarRegistro(model);
txtestado.setEnabled(false);
txtUF.setEnabled(false);
btnSalvar.setEnabled(false);
}
});
}
}
//Modelo Tabela
package br.com.clinica.util;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
public class ModeloTabela extends AbstractTableModel {
private ArrayList linhas = null;
private String[] colunas = null;
public ModeloTabela(ArrayList lin, String[] col) {
setLinhas(lin);
setColunas(col);
}
public ArrayList getLinhas() {
return linhas;
}
public void setLinhas(ArrayList dados) {
linhas = dados;
}
public String[] getColunas() {
return colunas;
}
public void setColunas(String[] nomes) {
colunas = nomes;
}
public int getColumnCount() {
return colunas.length;
}
public int getRowCount() {
return linhas.size();
}
public String getColumnName(int numCol) {
return colunas[numCol];
}
public Object getValueAt(int numLin, int numCol) {
Object[] linha = (Object[]) getLinhas().get(numLin);
return linha[numCol];
}
}
//Metodo para popular a tabela
public void popularTabela(String SQL) {
ArrayList dados = new ArrayList();
String[] Colunas = new String[]{"CODIGO", "ESTADOR", "UF"};
try {
conecta.executeSQL(SQL);
conecta.rs.first();
do {
dados.add(new Object[]{conecta.rs.getInt("codigo"), conecta.rs.getString("estado"), conecta.rs.getString("uf")});
} while (conecta.rs.next());
} catch (SQLException erro) {
JOptionPane.showMessageDialog(null, "Não foi possivel popular a tabela" + erro.getMessage());
}
ModeloTabela model = new ModeloTabela(dados, Colunas);
TabelaEstado.setModel(model);
TabelaEstado..getColumnModel().getColumn(0).setPreferredWidth(55);
TabelaEstado. getColumnModel().getColumn(0).setResizable(false);
TabelaEstado..getColumnModel().getColumn(1).setPreferredWidth(250);
TabelaEstado. getColumnModel().getColumn(1).setResizable(false);
TabelaEstado..getColumnModel().getColumn(2).setPreferredWidth(40);
TabelaEstado. getColumnModel().getColumn(2).setResizable(false);
TabelaEstado. getTableHeader().setReorderingAllowed(false);
TabelaEstado. setAutoResizeMode(TabelaCidade.AUTO_RESIZE_OFF);
TabelaEstado..setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
Fiz usando Netbeans e tudo funciona normalmente preenche a tabela certinho, no netbeans fiz os formulários usando o GUI do netbeans, mais quando faço no Eclipse tudo na mão mesmo não funciona. Já tentei de várias formas chamar, no netbeans eu só coloco os campos que quero que aparece na tabela e já vem, já pelo eclipse não vem nada
Quem puder me ajudar fico agradecido.