Witam.
Mam napisany program z oknem głównym oraz dodatkowym, który dodaje elementy do bazy.
Jak najprościej zrobić aktualizację tabeli w oknie głównym po tym, jak zostanie kliknięty przycisk "Dodaj" w oknie dodatkowym?
Są to 2 różne klasy. Niby mam metodę, która wypełnia tabelę ale nie mogę jej wywołać w innej klasie, bo miałbym do przekazania dużo parametrów i takie działanie jest bez sensu.
public class MainWindow extends javax.swing.JFrame {
DefaultTableModel model;
final JFrame frame = new JFrame(MainWindow.class.getSimpleName());
/**
* Creates new form MainWindow
*/
// private Order zamowienie;
public MainWindow() {
initComponents();
String n[] = {"ID", "Imie", "Nazwisko", "Produkt", "Ilość", "Razem(zł)", "Status", "Edytuj", "Usuń"};
model = new DefaultTableModel(null, n);
jTable1.setModel(model);
jTable1.putClientProperty("terminateEditorOnFocusLost", Boolean.TRUE);
jTable1.getColumnModel().getColumn(0).setMaxWidth(40);
jTable1.getColumnModel().getColumn(4).setMaxWidth(40);
RowSorter<TableModel> sorter = new TableRowSorter<>(model);
jTable1.setRowSorter(sorter);
ShowTable(model, jTable1, jLabel3, jLabel1, jLable2);
}
private static void ShowTable(DefaultTableModel model, JTable jTable1, JLabel jLabel3, JLabel jLabel1, JLabel jLable2){
OrdersDAL db = new OrdersDAL();
Connection connection;
connection = db.createConnection();
int numberOfOrders=db.CountOrdersInTheDb();
int numberOfClients=db.CountClientsInTheDb();
int numberOfProducts=db.CountProductsInTheDb();
int numberOfOrderProducts=db.CountOrderProductsInTheDb();
Order[] orders = db.TakeOrdersFromDb(numberOfOrders);
Client[] clients = db.TakeClientsFromDb(numberOfClients);
Product[] products = db.TakeProductsFromDb(numberOfProducts);
OrderProduct[] ordProd = db.TakeOrderProductsFromDb(numberOfOrderProducts);
double summaryValue = 0;
for (int i = 0; i < numberOfOrders; i++) {
String firstname = "";
String lastname = "";
int product_id = 0;
String productName = "";
double productPrice = 0.00;
int numBoughtProducts = 0;
int client_id = orders[i].getClient_id();
String order_status = orders[i].getStatus();
for (int j = 0; j < numberOfClients; j++) {
if (clients[j].IsThisClient(client_id)) {
firstname = clients[j].getFirstname();
lastname = clients[j].getLastname();
}
}
for (int j = 0; j < numberOfOrderProducts; j++) {
if (ordProd[j].IsThisOrderProduct(orders[i].getId())) {
product_id = ordProd[j].getProduct_id();
numBoughtProducts = ordProd[j].getNumber();
}
}
for (int j = 0; j < numberOfProducts; j++) {
if (products[j].IsThisProduct(product_id)) {
productName = products[j].getName();
productPrice = products[j].getPrice();
}
}
summaryValue += productPrice*numBoughtProducts;
model.addRow(new Object[]{orders[i].getId(), firstname, lastname, productName , numBoughtProducts, productPrice*numBoughtProducts ,order_status, "Edytuj", "Usun"});
}
jTable1.getColumn("Edytuj").setCellRenderer(new ButtonRenderer());
jTable1.getColumn("Edytuj").setCellEditor(new ButtonEditor(new JCheckBox(),connection, jTable1, 1, model, numberOfOrders, numberOfClients,orders,ordProd, clients));
jTable1.getColumn("Usuń").setCellRenderer(new ButtonRenderer());
jTable1.getColumn("Usuń").setCellEditor(new ButtonEditor(new JCheckBox(),connection, jTable1, 2, model, numberOfOrders, numberOfClients,orders,ordProd, clients));
jLabel3.setText(""+summaryValue+" zł");
summaryValue = summaryValue/numberOfOrders;
summaryValue *= 100;
summaryValue = Math.round(summaryValue);
summaryValue /= 100;
jLabel1.setText(""+numberOfOrders);
jLable2.setText(""+summaryValue+" zł");
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String firstname = jTextField1.getText();
String lastname = jTextField2.getText();
String street = jTextField3.getText();
String postcode = jTextField4.getText();
String city = jTextField5.getText();
String phone = jTextField6.getText();
int order_id =0;
int client_id = 0;
if (phone.matches("[0-9]+") && phone.length() < 12) {
OrdersDAL con = new OrdersDAL();
Connection connection;
connection = con.createConnection();
client_id = con.TakeMaxIdFromClients();
Client client = new Client(client_id, firstname, lastname, street, postcode, city, phone);
client.AddToDatabase(connection);
order_id = con.TakeMaxIdFromOrders();
Order order = new Order(order_id, client_id);
order.AddToDatabase(connection);
int product_id=0;
String product = (String)jComboBox2.getSelectedItem();
String number = (String)jComboBox1.getSelectedItem();
OrderProduct ordprod = new OrderProduct(order_id, Integer.parseInt(number));
ordprod.AddToDatabase(connection);
}else{
showMessageDialog(null, "Uzupełnij poprawnie numer telefonu");
}
}