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);
}
}

miercuri, 12 noiembrie 2008

Dialog Cautare Produs dupa Cod

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);
}
}
}

joi, 6 noiembrie 2008

Aplicatie pentru lucru cu intrari din Depozit

Clasa Produs:

import java.util.Date;

public class Produs implements Comparable{

private String cod;
private String denumire;
private Date data;
private Integer valoarea_unitara;
private Integer cantitatea;

public String getCod() {
return cod;
}
public void setCod(String cod) {
this.cod = cod;
}
public String getDenumire() {
return denumire;
}
public void setDenumire(String denumire) {
this.denumire = denumire;
}
public Integer getCantitatea() {
return cantitatea;
}
public void setCantitatea(Integer cantitatea) {
this.cantitatea = cantitatea;
}
public Date getData() {
return data;
}
public void setData(Date data) {
this.data = data;
}
public Integer getValoarea_unitara() {
return valoarea_unitara;
}
public void setValoarea_unitara(Integer valoarea_unitara) {
this.valoarea_unitara = valoarea_unitara;
}
public int compareTo(Object arg0) {

if(arg0 instanceof Produs){
Produs produs = (Produs)arg0;

return -produs.getDenumire().compareTo(denumire);//produs.getDenumire().compareTo(this.getDenumire())

}
return 1;
}
}