日期:2014-05-20 浏览次数:20858 次
package com.vtars.ttsdemo.panels;
import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
/*
* 此类主要定义列表类
* 将列表以单独的类实现并将列表生成在滚动面板上返回供底层面板调用
*/
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.apache.poi.hslf.record.Document;
import com.vtars.ttsdemo.business.Speaker;
import com.vtars.ttsdemo.business.ebi.StuEbi;
import com.vtars.ttsdemo.business.factory.StuEbiFactory;
import com.vtars.ttsdemo.dao.txtRead;
import com.vtars.ttsdemo.dao.dao.StudentDao;
import com.vtars.ttsdemo.dao.factory.StudentDaoFactoty;
import com.vtars.ttsdemo.framework.io.ReadExcel;
import com.vtars.ttsdemo.vo.StudentModel;
public class ListPanelUI {
private JPanel listJPanel; // 较低层面板-此面板用来承装滚动面板和搜索框
private static JTextField textFieldSearch;// 用来搜索滴
private static JScrollPane listJScrollPane; // 滚动面板-此面板承装列表
private static JList IdNameList;
private static DefaultListModel model = new DefaultListModel();// 列表模型
private static ListPanelUI singlistpJPanel = new ListPanelUI();// 单例调用
static String initStr = "快速查找“学号”“姓名”";
public static ListPanelUI getInstance() {
return singlistpJPanel;
}
private ListPanelUI() {
InitlistJPanel();
}
/*
* 获取整合后的面板
*/
public JPanel getlistJPanel() {
return listJPanel;
}
/*
* 初始化承装滚动面板和搜索框的底层面板
*/
public void InitlistJPanel() {
listJPanel = new JPanel();
listJPanel.setLayout(null);
listJPanel.add(InitlistJScrollPane());
listJPanel.add(InittextFieldSearch());
}
/*
* 初始化搜索输入框
*/
public JTextField InittextFieldSearch() {
JTextField textFieldSearch = new JTextField(initStr);
textFieldSearch.setForeground(Color.LIGHT_GRAY);
textFieldSearch.setBounds(10, 590, 161, 27);
textFieldSearch.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
JTextField src = (JTextField) e.getSource();
src.setForeground(Color.blue);
if (src.getText().equals(initStr)) {
src.setText(null);
}
}
public void focusLost(FocusEvent e) {
JTextField src = (JTextField) e.getSource();
if (src.getText().trim().isEmpty()) {
src.setText(initStr);
src.setForeground(Color.LIGHT_GRAY);
}
}
});
textFieldSearch.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
});
return textFieldSearch;
}
public void FindTheItem() {
// TODO 自动生成的方法存根
String key = textFieldSearch.getText();
if ((key == null) || (key.trim().isEmpty())) {
// 处理为空格的时候
textFieldSearch.setText("");// 当为空格的时候直接删除
}
if (model.contains(key)) {
int index = model.indexOf(key);
IdNameList.setSelectedIndex(index);
}
else {
IdNameList.clearSelection();
System.out.println("没有元素");
}
}
/*
* 初始化承装列表的滚动面板
*/
public JScrollPane InitlistJScrollPane() {
listJScrollPane = new JScrollPane();
listJScrollPane.setBounds(10, 10, 161, 570);
listJScrollPane.setColumnHeaderView(new JLabel("学生列表",
SwingConstants.CENTER));
listJScrollPane.setViewportView(InitJList());
return listJScrollPane;
}
/*
* 初始化Jlist
*/
public JList InitJList() {
System.out.println("Jlist初始化");
// 初始化一个List
IdNameList = new JList();
// 设置列表的选择模式为单选
IdNameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
final StuEbi ebi = StuEbiFactory.getStuEbi();
List<StudentModel> list = ebi.getAll();