package op;
import java.awt.*;import java.awt.event.*;import java.util.*;import javax.swing.*;import tools.*;
public class ReturnAsset extends JPanel { private JScrollPane jScrollPane1 = new JScrollPane(); private DBConnection dcon = null; private JTable table; private Mytable model; private String tempID = ""; private HashMap chMap = new HashMap(); JButton btnturn = new JButton(); JComboBox choice = new JComboBox(); String user; public ReturnAsset(String _user) { try { this.user = _user; jbInit(); } catch (Exception ex) { ex.printStackTrace(); } }
//init choice private void setChoice() { if (getChoiceList() != null || getChoiceList().length != 0) { btnturn.setEnabled(true); String[] s = getChoiceList(); for (int i = 0; i < s.length; i++) { choice.addItem(s[i]); } } else { btnturn.setEnabled(false); } if(choice.getItemCount()==0) { btnturn.setEnabled(false); } }
private void jbInit() throws Exception { setChoice(); model = new Mytable(4); model.setTitle(getTitle()); if (choice.getSelectedItem() != null) { model.setContent(getContents(choice.getSelectedItem().toString(). trim())); } else { model.setContent(getContents("")); } table = new JTable(model); table.setRowSelectionAllowed(false); this.setLayout(null); jScrollPane1.setBounds(new Rectangle(11, 54, 475, 231)); btnturn.setBounds(new Rectangle(296, 14, 84, 32)); btnturn.setText("详细"); choice.setBounds(new Rectangle(39, 11, 122, 32)); this.add(jScrollPane1); this.add(btnturn); this.add(choice); jScrollPane1.getViewport().add(table); this.setSize(500, 400); //行选择编号改变 table.addMouseListener(new MouseListener() { public void mouseClicked(MouseEvent e) { if (table.getSelectedRow() != -1 && table.getSelectedColumn() != -1) { tempID = table.getValueAt(table.getSelectedRow(), 0). toString(); } else { tempID = ""; } }
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { } }); btnturn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (tempID.trim().length() != 0) {
returnAsset(); } else { JOptionPane.showMessageDialog(table, "请选择要操作的记录"); } } });
choice.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { model.setContent(getContents(choice.getSelectedItem().toString(). trim())); table.updateUI(); } }); }
private void returnAsset() { AssetReturn ar = new AssetReturn(choice.getSelectedItem().toString(). trim(), tempID, getID(choice.getSelectedItem(). toString(). trim())); Dimension frmsize = getSize(); Point loc = getLocation(); ar.setLocation((frmsize.width - ar.WIDTH) / 2 + loc.x, (frmsize.height - ar.HEIGHT) / 2 + loc.y); ar.setSize(350, 450); ar.setModal(true); ar.setVisible(true); model.setContent(getContents(choice.getSelectedItem().toString(). trim())); table.updateUI(); this.updateUI(); tempID = ""; }
//获取choice列表 private String[] getChoiceList() { dcon = new DBConnection(); String sql = "select b.asset_user,a.employee_name from employee as a,(select asset_user from Asset group by asset_user) as b where ( b.asset_user = a.employee_id)"; Vector v = dcon.select(sql); int count = v.size(); String s[] = new String[count]; for (int i = 0; i < count; i++) { String id = ((Vector) v.get(i)).get(0).toString().trim(); String name = ((Vector) v.get(i)).get(1).toString().trim(); s[i] = name; chMap.put(name, id); } return s; }
//getid private String getID(String name) { if (chMap.get(name) != null) { return chMap.get(name).toString().trim(); } else { return "12221111111111111111112"; } }
//获取表格的列表 private String[] getTitle() { dcon = new DBConnection(); String sql = "select asset_id as 编号, asset_name as 名称,asset_type as 类型,asset_price as 单价,asset_buytime as 购买时间,asset_status as 状态,remark as 备注 from asset order by asset_id asc"; return dcon.getColumnname(sql); }
//获取表格的内容 private Vector getContents(String con1) { String con = getID(con1); dcon = new DBConnection(); // String sql = "select asset_id as 编号, asset_name as 名称,asset_type as 类型,asset_price as 单价,asset_buytime as 购买时间,asset_status as 状态,remark as 备注 from asset where(asset_user is null or len(asset_user)=0) order by asset_id asc"; String sql = "select asset_id as 编号, asset_name as 名称,asset_type as 类型,asset_price as 单价,asset_buytime as 购买时间,asset_status as 状态,remark as 备注 from asset where((asset_user is not null or len(Ltrim(rtrim(asset_user)))<>0 ) and (asset_user='" + con + "')) order by asset_id asc"; return dcon.select(sql); } public void fresh() { if(choice.getSelectedItem()!=null){ model.setContent(getContents(choice.getSelectedItem().toString(). trim())); }else { model.setContent(getContents("")); } table.updateUI(); }
}