Ola All
Abaixo segue a rotina que estou usando para criar jtable, mas estou com algumas duvidas.
private void showDatabaseTable() {
try {
try {
url = "jdbc:odbc:CollegeMySQL";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection = DriverManager.getConnection(url);
}
catch(ClassNotFoundException cnfx) {
cnfx.printStackTrace();
JOptionPane.showMessageDialog(null,"O Sistema não obteve acesso ao Banco de Dados");
}
catch (SQLException sqlx) {
sqlx.printStackTrace();
JOptionPane.showMessageDialog(null,"O Sistema não obteve acesso ao Banco de Dados");
}
catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null,"O Sistema não obteve acesso ao Banco de Dados");
}
Connection conexao = DriverManager.getConnection(url);
Statement st = conexao.createStatement();
ResultSet rec = st.executeQuery(
"SELECT codigo, descricao, unidade FROM Unidades ");
displayDatabaseRecords(rec);
st.close();
}
catch (SQLException sqlx) {
sqlx.printStackTrace();
}
}
private void displayDatabaseRecords(ResultSet rS) throws SQLException {
boolean moreRecords = rS.next();
if (!moreRecords) {
}
try {
ResultSetMetaData rs = rS.getMetaData();
for (int i = 1; i <= rs.getColumnCount(); ++i)
gridColumns.addElement(rs.getColumnName(i));
do {
gridRows.addElement(getNextRow(rS,rs));
} while (rS.next());
jTable1 = new JTable(gridRows, gridColumns) {
public Component prepareRenderer(TableCellRenderer renderer,
int rowIndex, int vColIndex) {
Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
if (rowIndex % 2 == 0 && !isCellSelected(rowIndex, vColIndex)) {
c.setBackground(Color.orange);
} else {
c.setBackground(getBackground());
}
return c;
}
};
int vColIndex = 0;
jTable1.getColumnModel().getColumn(vColIndex).setHeaderValue("CODIGO");
jTable1.getColumnModel().getColumn(vColIndex).setMaxWidth(80);
vColIndex = 1;
jTable1.getColumnModel().getColumn(vColIndex).setHeaderValue("DESCRIÇÃO");
vColIndex = 2;
jTable1.getColumnModel().getColumn(vColIndex).setHeaderValue("UNIDADE");
jTable1.setOpaque(true);
jTable1.setBackground(new java.awt.Color(255, 255, 255));
JScrollPane scroller = new JScrollPane(jTable1);
scroller.setBackground(new java.awt.Color(255, 255, 255));
scroller.setOpaque(true);
jPanel1.setBackground(new java.awt.Color(255, 255, 255));
jPanel1.setOpaque(true);
jTable1.setRowHeight(20);
jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jTable1.setRowSelectionAllowed(true);
jPanel1.add(scroller);
scroller.revalidate();
validate();
}
catch(SQLException sqlx) {
sqlx.printStackTrace();
}
}
private Vector getNextRow(ResultSet rS, ResultSetMetaData rs) throws SQLException {
Vector currentRow = new Vector();
for (int i = 1; i <= rs.getColumnCount(); ++i)
if (rs.getColumnType(i) == Types.VARCHAR) {
currentRow.addElement(rS.getString(i));
}
else {
//
}
return currentRow;
}
Tenho mais alguns botões para incluir, alterar e excluir dados, sendo que quando clico nos botões é aberto um jinternalframe para o usuário incluir seus dados, quando gravo e fecho a janela gostaria que os dados apareçam no jtable, mas para isto tenho que fechar a janela e abrir novamente.
Tem alguma forma de que este jtable seja automaticamente renovado.
No FoxPro outra linguagem de programo tem um comando chamado thisform.refresh(), quando chamo esta função o formulário é refeito como os dados incluídos.
Será que tem alguma coisa parecida no Java, que refaça ou re-chame.
Desde já agradeço
Renato V. Neto
[email removido]