I need some help guys ayaw mag output ng queries sa JTABLE ko. pero naka stored na sya sa arraylist at naaoutput naman using console yung data.
class TablePanel:
//bottom
layout.helperBackgroundcolor(bottomPanel, Color.yellow);
layout.helperDimensionHeight(bottomPanel, 80);
add(bottomPanel, BorderLayout.SOUTH);
//table panel
Object[][] data = {};
String[] colNames = {"ID", "Name", "Total Purchases"};
tableModel = new DefaultTableModel(data, colNames);
customerTable = new JTable(tableModel);
customerTable.setFillsViewportHeight(true);
scrollPane = new JScrollPane(customerTable);
add(scrollPane, BorderLayout.CENTER);
}
public void addData(Object[] data) {
tableModel.addRow(data);
}
class Database:
//retrieve customers data
public List<Customer> getCustomers() throws Exception{
String sql = "SELECT * FROM CUSTOMER";
List<Customer> list = new ArrayList<Customer>();
connectDb();
st = conn.prepareStatement(sql);
rs = st.executeQuery(sql);
while(rs.next()) {
Customer cust = new Customer();
cust.setId(rs.getInt("cust_id"));
cust.setName(rs.getString("cust_Name"));
cust.setTotalpurchases(rs.getInt("cust_TotalPurchases"));
list.add(cust);
}
return list;
}
class customerController:
public void load() {
List<Customer> db;
try
{
db = dbModel.getCustomers();
Object rowData[] = new Object[3];
for(int i = 0; i < db.size(); i++){
rowData[0] = db.get(i).getId();
rowData[1] = db.get(i).getName();
rowData[2] = db.get(i).getTotPurchased();
guiTable.addData(rowData);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
class MenuController:
public class MenuController {
private MenuUI view;
public MenuController(MenuUI view) {
this.view = view;
view.addButtonListener(new buttonListener());
}
public MenuController() {
}
class buttonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
JButton clicked = (JButton)e.getSource();
if(clicked == view.getbtnProducts()) {
view.getCardLayout().show(view.getContworkspace(), "2");
}else if(clicked == view.getbtnHome()) {
view.getCardLayout().show(view.getContworkspace(), "1");
}else if(clicked == view.getbtnCustomer()) {
view.getCardLayout().show(view.getContworkspace(), "3");
CustomerController controller = new CustomerController();
controller.load();
}
}
}
}
class TablePanel:
//bottom
layout.helperBackgroundcolor(bottomPanel, Color.yellow);
layout.helperDimensionHeight(bottomPanel, 80);
add(bottomPanel, BorderLayout.SOUTH);
//table panel
Object[][] data = {};
String[] colNames = {"ID", "Name", "Total Purchases"};
tableModel = new DefaultTableModel(data, colNames);
customerTable = new JTable(tableModel);
customerTable.setFillsViewportHeight(true);
scrollPane = new JScrollPane(customerTable);
add(scrollPane, BorderLayout.CENTER);
}
public void addData(Object[] data) {
tableModel.addRow(data);
}
class Database:
//retrieve customers data
public List<Customer> getCustomers() throws Exception{
String sql = "SELECT * FROM CUSTOMER";
List<Customer> list = new ArrayList<Customer>();
connectDb();
st = conn.prepareStatement(sql);
rs = st.executeQuery(sql);
while(rs.next()) {
Customer cust = new Customer();
cust.setId(rs.getInt("cust_id"));
cust.setName(rs.getString("cust_Name"));
cust.setTotalpurchases(rs.getInt("cust_TotalPurchases"));
list.add(cust);
}
return list;
}
class customerController:
public void load() {
List<Customer> db;
try
{
db = dbModel.getCustomers();
Object rowData[] = new Object[3];
for(int i = 0; i < db.size(); i++){
rowData[0] = db.get(i).getId();
rowData[1] = db.get(i).getName();
rowData[2] = db.get(i).getTotPurchased();
guiTable.addData(rowData);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
class MenuController:
public class MenuController {
private MenuUI view;
public MenuController(MenuUI view) {
this.view = view;
view.addButtonListener(new buttonListener());
}
public MenuController() {
}
class buttonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
JButton clicked = (JButton)e.getSource();
if(clicked == view.getbtnProducts()) {
view.getCardLayout().show(view.getContworkspace(), "2");
}else if(clicked == view.getbtnHome()) {
view.getCardLayout().show(view.getContworkspace(), "1");
}else if(clicked == view.getbtnCustomer()) {
view.getCardLayout().show(view.getContworkspace(), "3");
CustomerController controller = new CustomerController();
controller.load();
}
}
}
}
Attachments
-
You do not have permission to view the full content of this post. Log in or register now.