joi, 13 noiembrie 2008

Dialogul de afisare a continutului bazei de date

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.GroupLayout.Alignment;
import problema080915.Produs;
import problema080922.DepozitApp;
import problema081017.ProdusDao;

public class FindEntryDlg extends JDialog implements ActionListener {

JLabel label;
ProdusDao produsDao;
JTable table;
List list;

public FindEntryDlg(JFrame owner){
super(owner, "Whatever", true);

label = new JLabel();
produsDao = ((DepozitApp)owner).getProdusDao();

JButton ok = new JButton("OK");
ok.setActionCommand("ok");
ok.addActionListener(this);

JButton cancel = new JButton("Cancel");
cancel.setActionCommand("cancel");
cancel.addActionListener(this);

Dimension size = ok.getPreferredSize();
ok.setBounds(25, 40, size.width, size.height);
size = cancel.getPreferredSize();
cancel.setBounds(25, 80, size.width, size.height);

JScrollPane scrollPane = null;

try{
list = produsDao.gasesteProduse();

// initializare tabel cu rezultate:
Object[][] data = new Object[list.size()][5];
String[] columnNames = {"Cod",
"Data",
"Denumire",
"Cantitate",
"Valoare unitara"};

int contor = 0;
for(Iterator it = list.iterator(); it.hasNext(); ){
Produs pr = (Produs)it.next();

data[contor][0] = pr.getCod();
data[contor][1] = pr.getData();
data[contor][2] = pr.getDenumire();
data[contor][3] = pr.getCantitatea();
data[contor][4] = pr.getValoarea_unitara();
contor++;
}

table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);

// Create the scroll pane and add the table to it.
scrollPane = new JScrollPane(table);
}catch (SQLException e){
System.out.println("Exceptie.");
e.printStackTrace();
}

GroupLayout layout = new GroupLayout(this.getContentPane());
this.getContentPane().setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);

GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();

hGroup.addGroup(layout.createParallelGroup().
addComponent(ok).addComponent(scrollPane));
hGroup.addGroup(layout.createParallelGroup().
addComponent(cancel));
layout.setHorizontalGroup(hGroup);

GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();

vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
addComponent(scrollPane));
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
addComponent(ok).addComponent(cancel));
layout.setVerticalGroup(vGroup);

this.setSize(600, 300);
}

public void actionPerformed(ActionEvent event) {

String cmd = event.getActionCommand();

if("ok".equals(cmd)) {
label.setText("Butonul ok a fost apasat");

try{
list = produsDao.gasesteProduse();

MetodeUtilitare.afiseazaRezultateInterogare(list);

}catch (SQLException e){
System.out.println("Exceptie.");
e.printStackTrace();
}
} else if ("cancel".equals(cmd)) {
label.setText("Butonul cancel a fost apasat");
}
this.setVisible(false);
}
}

Niciun comentariu: