You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
173 lines
5.0 KiB
173 lines
5.0 KiB
package 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 dao.ZhuhuDao;
|
|
import dao.impl.ZhuhuImpl;
|
|
import entity.Zhuhu;
|
|
|
|
public class ZhuhuManageUIListPanel extends JPanel implements Tableinter{
|
|
ZhuhuDao dao = new ZhuhuImpl();
|
|
private JPanel contentPane;
|
|
|
|
|
|
|
|
|
|
private static String[] columnCount = { "id","户主","地址","联系电话" };
|
|
public JTable table;
|
|
private static DefaultTableModel tableModel;
|
|
private String mainid;
|
|
|
|
public ZhuhuManageUIListPanel(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 ZhuhuAddUI(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);
|
|
/**
|
|
根据主键进行查询,如果查询到则删除
|
|
*/
|
|
Zhuhu zh=new Zhuhu();
|
|
zh.setId(Integer.valueOf(keyvalue));
|
|
dao.remove(zh);
|
|
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 ZhuhuUpdateUI(me()).setVisible(true);
|
|
}
|
|
|
|
});
|
|
btnpanel.add(button_2);
|
|
System.out.println(dao.findAll().size());
|
|
refresh(dao.findAll());
|
|
}
|
|
public ZhuhuManageUIListPanel 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<Zhuhu> 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).getHuzhudxy(),list.get(i).getDizhidxy(),list.get(i).getLianxidianhuadxy() };
|
|
//如果包含图片格式则以图片形式显示
|
|
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;
|
|
}
|
|
}
|