quinta-feira, 6 de outubro de 2011

Java DESKTOP: Jtable Example

public void LoadRelatorio() throws ClassNotFoundException, SQLException {
_itens = new ArrayList();

Conexao.Conectar();
String query = "select * from produtosold";
ResultSet rs = Conexao.Consulta(query);

while (rs.next()) {
Produto prod = new Produto();
prod.setDescricao(rs.getString("descricaosold"));
prod.setPrecoCusto(rs.getDouble("precocustosold"));
prod.setCodigo(rs.getInt("codigosold"));
prod.setQuantidade(rs.getInt("quantidadesold"));
prod.setPrecoVenda(rs.getDouble("precovendasold"));
prod.setValorTotal(prod.getQuantidade() * prod.getPrecoVenda());
prod.setData(rs.getString("datasold"));

_itens.add(prod);
}
Conexao.fechaConexao();

table.setModel(new javax.swing.table.DefaultTableModel(
new Object[][]{},
new String[]{
//aqui adiciona-se as colunas e seus respectivos nomes
"Código", "Item", "Quantidade", "PrecoVenda", "Valor Total", "Data/Hora"
}));

javax.swing.table.DefaultTableModel dtm =
(javax.swing.table.DefaultTableModel) table.getModel();

double totalVenda = 0;
double totalCusto = 0;
DecimalFormat format = new DecimalFormat();
for (int i = 0; i < _itens.size(); i++) {
totalVenda += _itens.get(i).getValorTotal();
totalCusto += _itens.get(i).getPrecoCusto();
dtm.addRow(new Object[]{_itens.get(i).getCodigo(), _itens.get(i).getDescricao(), _itens.get(i).getQuantidade(),
String.format("RS %s", format.format(_itens.get(i).getPrecoVenda())), String.format("RS %s", format.format(_itens.get(i).getValorTotal())), _itens.get(i).getData()});
}


lblTotal.setText("RS " + String.valueOf(format.format(totalVenda)));
lblCusto.setText("RS " + String.valueOf(format.format(totalCusto)));

double lucro = totalVenda - totalCusto;
lblLucro.setText("RS " + String.valueOf(format.format(lucro)));



}

Nenhum comentário:

Postar um comentário