parent
66bd2a9361
commit
44d3d3daab
@ -0,0 +1,176 @@
|
||||
package com.ym.view;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
|
||||
import java.awt.Component;
|
||||
import java.awt.FlowLayout;
|
||||
import java.awt.GridLayout;
|
||||
import java.awt.Image;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTable;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
import javax.swing.table.DefaultTableModel;
|
||||
|
||||
import com.ym.dao.ShouruqingkuangymDao;
|
||||
//import dao.ZhuhuDao;
|
||||
import com.ym.dao.impl.ShouruqingkuangymImpl;
|
||||
//import dao.impl.ZhuhuImpl;
|
||||
import com.ym.entity.Shouruqingkuangym;
|
||||
|
||||
public class ShouruqingkuangManageUIListPanel extends JPanel implements Tableinter{
|
||||
ShouruqingkuangymDao dao = new ShouruqingkuangymImpl();
|
||||
//ZhuhuDao zhuhuhuzhuymdao = new ZhuhuImpl();
|
||||
private JPanel contentPane;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private static String[] columnCount = { "id","明细","发生日期","发生费用","户主" };
|
||||
public JTable table;
|
||||
private static DefaultTableModel tableModel;
|
||||
private String mainid;
|
||||
|
||||
public ShouruqingkuangManageUIListPanel(String mainid) {
|
||||
this.mainid=mainid;
|
||||
setLayout(new BorderLayout(0, 0));
|
||||
contentPane = new JPanel();
|
||||
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||
contentPane.setLayout(new BorderLayout(0, 0));
|
||||
add(contentPane);
|
||||
JScrollPane scrollPane = new JScrollPane();
|
||||
contentPane.add(scrollPane, BorderLayout.CENTER);
|
||||
table = new JTable();
|
||||
scrollPane.setViewportView(table);
|
||||
JPanel toppanel = new JPanel();
|
||||
contentPane.add(toppanel, BorderLayout.NORTH);
|
||||
toppanel.setLayout(new GridLayout(2, 1, 0, 0));
|
||||
JPanel btnpanel = new JPanel();
|
||||
btnpanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
|
||||
toppanel.add(btnpanel);
|
||||
|
||||
|
||||
JButton button = new JButton("添加");
|
||||
button.addMouseListener(new MouseAdapter() {
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
new ShouruqingkuangAddUI(me()).setVisible(true);
|
||||
}
|
||||
});
|
||||
btnpanel.add(button);
|
||||
|
||||
JButton button_1 = new JButton("删除");
|
||||
/**
|
||||
删除操作
|
||||
*/
|
||||
button_1.addMouseListener(new MouseAdapter() {
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
int row = table.getSelectedRow();
|
||||
if (row == -1) {
|
||||
JOptionPane.showMessageDialog(null, "请选择一行进行删除");
|
||||
return;
|
||||
}
|
||||
String keyvalue = (String) table.getValueAt(row, 0);
|
||||
/**
|
||||
根据主键进行查询,如果查询到则删除
|
||||
*/
|
||||
Shouruqingkuangym sf=new Shouruqingkuangym();
|
||||
sf.setId(Integer.valueOf(keyvalue));
|
||||
dao.remove(sf);
|
||||
JOptionPane.showMessageDialog(null, "删除成功");
|
||||
refresh(dao.findAll());
|
||||
}
|
||||
});
|
||||
btnpanel.add(button_1);
|
||||
|
||||
JButton button_2 = new JButton("修改");
|
||||
/**
|
||||
修改操作
|
||||
*/
|
||||
button_2.addMouseListener(new MouseAdapter() {
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
int row = table.getSelectedRow();
|
||||
if (row == -1) {
|
||||
JOptionPane.showMessageDialog(null, "请选择一行进行修改");
|
||||
return;
|
||||
}
|
||||
//跳转到修改页面
|
||||
new ShouruqingkuangUpdateUI(me()).setVisible(true);
|
||||
}
|
||||
|
||||
});
|
||||
btnpanel.add(button_2);
|
||||
refresh(dao.findAll());
|
||||
}
|
||||
public ShouruqingkuangManageUIListPanel me(){
|
||||
return this;
|
||||
}
|
||||
//刷新表格列表
|
||||
public void refresh(List list){
|
||||
|
||||
|
||||
tableModel = new DefaultTableModel(transferList(list), columnCount){
|
||||
public Class getColumnClass(int column) {
|
||||
Vector<?> v = (Vector<?>) dataVector.elementAt(0);
|
||||
if(v.elementAt(column)!=null){
|
||||
return v.elementAt(column).getClass();
|
||||
}else{
|
||||
return String.class;
|
||||
}
|
||||
}
|
||||
public boolean isCellEditable(int row, int col) {
|
||||
Class<?> columnClass = getColumnClass(col);
|
||||
return columnClass != ImageIcon.class;
|
||||
}
|
||||
};
|
||||
table.setModel(tableModel);
|
||||
|
||||
|
||||
updateRowHeights();
|
||||
}
|
||||
//自动调整列高
|
||||
private void updateRowHeights()
|
||||
{
|
||||
for (int row = 0; row < table.getRowCount(); row++)
|
||||
{
|
||||
int rowHeight = table.getRowHeight();
|
||||
|
||||
for (int column = 0; column < table.getColumnCount(); column++)
|
||||
{
|
||||
Component comp = table.prepareRenderer(table.getCellRenderer(row, column), row, column);
|
||||
rowHeight = Math.max(rowHeight, comp.getPreferredSize().height);
|
||||
}
|
||||
table.setRowHeight(row, rowHeight);
|
||||
}
|
||||
}
|
||||
//将实体列表转换为表格实体
|
||||
public static Object[][] transferList(List<Shouruqingkuangym> list) {
|
||||
Object[][] olist = new Object[list.size()][];
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
Object str[] = {list.get(i).getId().toString(), list.get(i).getMingxiym(),list.get(i).getFashengriqiym(),list.get(i).getFashengfeiyongym(),list.get(i).getHuzhuym() };
|
||||
//如果包含图片格式则以图片形式显示
|
||||
for (int j = 0; j < str.length; j++) {
|
||||
if(str[j]!=null&&(str[j].toString().contains("png")||str[j].toString().contains("jpg")||str[j].toString().contains("gif"))){
|
||||
str[j]=new ImageIcon(new ImageIcon(str[j].toString()).getImage().getScaledInstance(100, 100, Image.SCALE_DEFAULT));
|
||||
}
|
||||
}
|
||||
olist[i] = str;
|
||||
}
|
||||
|
||||
return olist;
|
||||
}
|
||||
public JTable getTable() {
|
||||
return table;
|
||||
}
|
||||
public String getMainid(){
|
||||
return mainid;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue