資源簡介
自定義的JList,像android的ListView,可以點擊按鈕后馬上刪除當前條,并即時顯示最新數據,有點數據驅動UI的感覺。Swing自定義控件必備。

代碼片段和文件信息
package?demo;
import?java.awt.Color;
import?java.awt.Dimension;
import?java.awt.event.MouseAdapter;
import?java.awt.event.MouseEvent;
import?java.util.ArrayList;
import?java.util.Collections;
import?java.util.EventListener;
import?java.util.List;
import?javax.swing.BoxLayout;
import?javax.swing.JComponent;
import?javax.swing.JLabel;
/**
?*?自定義JList每個cell都是一個component這個component就可以任意定義了比如用一個JPanel來展示
?*?baseList?Demo?核心類
?*?@ClassName?baseList
?*?@author?Jet
?*?@date?2012-8-7
?*/
public?class?baseList?extends?JComponent{
private?static?final?long?serialVersionUID?=?1L;
//?list的源
private?ListSource?source;
public?ListSource?getSource()?{
return?source;
}
/**
?*?設置baseList的源
?*?@param?source?ListSource類型參數
?*/
public?void?setSource(ListSource?source)?{
if?(source?==?null)?{
return;
}?else?{
this.source.removeSourceRefreshListener(this);
this.source?=?null;
}
this.source?=?source;
this.source.addSourceRefreshListener(this);
this.refreshData();
}
//?顯示控件?定義一個接口
private?ListCellIface?celliface;
/**
?*?設置要顯示的控件
?*?@param?cell
?*/
public?void?setCellIface(ListCellIface?cell)?{
this.celliface?=?cell;
}
//?所有的控件?在對當前項操作時調用此變量
private?List?totalcell?=?new?ArrayList();
//?選中的顏色
private?Color?selectColor?=?new?Color(252?233?161);
public?Color?getSelectColor()?{
return?selectColor;
}
public?void?setSelectColor(Color?selectColor)?{
this.selectColor?=?selectColor;
}
//?鼠標經過的顏色
private?Color?passColor?=?new?Color(196?227?248);
public?Color?getPassColor()?{
return?passColor;
}
public?void?setPassColor(Color?passColor)?{
this.passColor?=?passColor;
}
//?選中的索引
private?int?selectIndex?=?-1;
/**
?*?選中某一行時執行此方法
?*?@param?selectIndex
?*/
public?void?setSelectIndex(int?selectIndex)?{
for?(int?i?=?0;?i? //所有項設置無背景
totalcell.get(i).setOpaque(false);
totalcell.get(i).setBackground(null);
if?(celliface?!=?null)
((ListCellIface)?totalcell.get(i)).setSelect(false);
}
if?(selectIndex??-1)?{
//為選中項設置背景
totalcell.get(selectIndex).setOpaque(true);
totalcell.get(selectIndex).setBackground(blist.getSelectColor());
if?(celliface?!=?null)
((ListCellIface)?totalcell.get(selectIndex)).setSelect(true);
}
this.selectIndex?=?selectIndex;
}
public?int?getSelectIndex()?{
return?selectIndex;
}
//?自身
private?baseList?blist?=?this;
public?baseList()?{
this.setLayout(new?BoxLayout(this?BoxLayout.Y_AXIS));
source?=?new?ListSource();
}
/**
?*?刷新數據
?*/
public?void?refreshData()?{
if?(source.getAllCell().size()?!=?0)?{
//排序
Collections.sort(source.getAllCell()?source.getComparator());
}
this.removeAll();
this.totalcell.clear();
for?(int?i?=?0;?i? JComponent?cell?=?null;
if?
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????301??2012-08-07?14:08??MyListDemo\.classpath
?????文件????????386??2012-08-07?14:08??MyListDemo\.project
?????文件????????598??2012-08-07?14:08??MyListDemo\.settings\org.eclipse.jdt.core.prefs
?????文件???????1798??2012-08-09?20:31??MyListDemo\bin\demo\ba
?????文件???????4502??2012-08-09?20:31??MyListDemo\bin\demo\ba
?????文件???????1467??2012-08-07?14:18??MyListDemo\bin\demo\ba
?????文件???????1092??2012-08-09?20:36??MyListDemo\bin\demo\CellPanel$1.class
?????文件???????2136??2012-08-09?20:36??MyListDemo\bin\demo\CellPanel.class
?????文件????????223??2012-08-07?14:18??MyListDemo\bin\demo\ListCellIface.class
?????文件???????2189??2012-08-07?14:19??MyListDemo\bin\demo\ListSource.class
?????文件???????2903??2012-08-09?20:36??MyListDemo\bin\demo\Mainfr
?????文件????????786??2012-08-07?14:19??MyListDemo\bin\demo\ScreenUtil.class
?????文件???????5355??2012-08-09?20:31??MyListDemo\src\demo\ba
?????文件???????1043??2012-08-07?14:18??MyListDemo\src\demo\ba
?????文件???????1626??2012-08-09?20:36??MyListDemo\src\demo\CellPanel.java
?????文件????????333??2012-08-07?14:18??MyListDemo\src\demo\ListCellIface.java
?????文件???????1516??2012-08-07?14:19??MyListDemo\src\demo\ListSource.java
?????文件???????2063??2012-08-09?20:36??MyListDemo\src\demo\Mainfr
?????文件????????641??2012-08-07?14:19??MyListDemo\src\demo\ScreenUtil.java
?????目錄??????????0??2012-08-09?20:38??MyListDemo\bin\demo
?????目錄??????????0??2012-08-09?20:38??MyListDemo\src\demo
?????目錄??????????0??2012-08-09?20:38??MyListDemo\.settings
?????目錄??????????0??2012-08-09?20:38??MyListDemo\bin
?????目錄??????????0??2012-08-09?20:38??MyListDemo\src
?????目錄??????????0??2012-08-09?20:38??MyListDemo
-----------?---------??----------?-----??----
????????????????30958????????????????????25
評論
共有 條評論