package dialogDemo;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
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.JTextField;
import javax.swing.GroupLayout.Alignment;
import javax.swing.table.DefaultTableModel;
import problema080915.Produs;
import problema080922.DepozitApp;
import problema081017.ProdusDao;
public class SearchByCodDlg extends JDialog implements ActionListener {
JLabel label;
JLabel label1 ;
JTextField tf1;
JTable table;
Produs produs;
JButton ok;
JButton cancel;
ProdusDao produsDao;
DefaultTableModel model;
String[] columnNames = {"Cod",
"Data",
"Denumire",
"Cantitate",
"Valoare unitara"};
public SearchByCodDlg(JFrame owner){
super(owner, "Whatever", true);
label = new JLabel();
label1 = new JLabel("Denumirea");
Dimension size = label1.getPreferredSize();
label1.setBounds(25, 40, size.width, size.height);
tf1 = new JTextField();
size = tf1.getPreferredSize();
//tf1.setBounds(25, 40, size.width, size.height);
//tf1.setColumns(20);
//tf1.setSize(20, 30);
produsDao = ((DepozitApp)owner).getProdusDao();
JScrollPane scrollPane = null;
try {
produs = produsDao.gasesteDupaCod(tf1.getText());
Object[][] data = initModelData(produs);
model = new DefaultTableModel(data, columnNames);
table = new JTable(model);
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) {
e.printStackTrace();
}
ok = new JButton("OK");
ok.setActionCommand("ok");
ok.addActionListener(this);
cancel = new JButton("Cancel");
cancel.setActionCommand("cancel");
cancel.addActionListener(this);
size = ok.getPreferredSize();
ok.setBounds(25, 40, size.width, size.height);
size = cancel.getPreferredSize();
cancel.setBounds(25, 80, size.width, size.height);
GroupLayout layout = new GroupLayout(this.getContentPane());
this.getContentPane().setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
GroupLayout.ParallelGroup hGroup = layout.createParallelGroup(Alignment.LEADING);
hGroup.addGroup(layout.createSequentialGroup().
addComponent(label1).
addComponent(tf1))
.addGroup(layout.createSequentialGroup().
addComponent(scrollPane))
.addGroup(layout.createSequentialGroup().
addComponent(ok).
addComponent(cancel));
layout.setHorizontalGroup(hGroup);
GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
vGroup.addGroup(layout.createParallelGroup(Alignment.LEADING).
addComponent(label1).
addComponent(tf1));
vGroup.addGroup(layout.createParallelGroup(Alignment.LEADING).
addComponent(scrollPane));
vGroup.addGroup(layout.createParallelGroup(Alignment.LEADING).
addComponent(ok).
addComponent(cancel));
layout.setVerticalGroup(vGroup);
this.setSize(500, 300);
}
private Object[][] initModelData(Produs pr) {
// initializare tabel cu rezultate:
Object[][] data = new Object[1][5];
data[0][0] = pr.getCod();
data[0][1] = pr.getData();
data[0][2] = pr.getDenumire();
data[0][3] = pr.getCantitatea();
data[0][4] = pr.getValoarea_unitara();
return data;
}
public void actionPerformed(ActionEvent event) {
String cmd = event.getActionCommand();
if("ok".equals(cmd)) {
label.setText("Butonul ok a fost apasat");
try{
Produs produs = produsDao.gasesteDupaCod(tf1.getText());
Object[][] data = initModelData(produs);
//MetodeUtilitare.afiseazaRezultateInterogare(list);
//se schimba datele din modelul tabelului:
model.setDataVector(data, columnNames);
}catch (SQLException e){
System.out.println("Exceptie.");
e.printStackTrace();
}
} else if ("cancel".equals(cmd)) {
label.setText("Butonul cancel a fost apasat");
this.setVisible(false);
}
}
}
miercuri, 12 noiembrie 2008
Abonați-vă la:
Postări (Atom)