Swing custom jlist

Source: Internet
Author: User
Tags listsource

Swing custom jlist

First, explain

Each entry in the middle is a cell. Here we use jpanel to display the cell, so we can add buttons, labels, and checkbox on the panel... Do what you want.

Click remove to delete the current row.

Complete engineering http://download.csdn.net/detail/jasper_success/4488517

Key code

/*** Custom jlist. Each cell is a component, which can be defined at will, for example, A jpanel is used to display * baselist demo core class * @ classname baselist * @ author jet * @ date 2012-8-7 */public class baselist extends jcomponent {Private Static final long serialversionuid = 1l; // list source private listsource source; Public listsource getsource () {return source ;} /*** set the baselist source * @ Param source listsource type parameter */Public void setsource (listsource s Ource) {If (Source = NULL) {return;} else {This. source. removesourcerefreshlistener (this); this. source = NULL;} This. source = source; this. source. addsourcerefreshlistener (this); this. refreshdata () ;}// display control defines an interface private listcelliface celliface;/*** sets the control to be displayed * @ Param cell */Public void setcelliface (listcelliface cell) {This. celliface = cell;} // all controls call this variable private list when operating on the current item <jcomponent> totalcell = N EW arraylist <jcomponent> (); // The selected color is private color selectcolor = new color (252,233,161); Public color getselectcolor () {return selectcolor;} public void setselectcolor (color selectcolor) {This. selectcolor = selectcolor;} // the color of the mouse. Private color passcolor = new color (196,227,248); Public color getpasscolor () {return passcolor;} public void setpasscolor (color passcolor) {This. passcolor = passcolor ;}// The selected index private int selectindex =-1;/*** run this method when selecting a row * @ Param selectindex */Public void setselectindex (INT selectindex) {for (INT I = 0; I <totalcell. size (); I ++) {// All items have no background totalcell. get (I ). setopaque (false); totalcell. get (I ). setbackground (null); If (celliface! = NULL) (listcelliface) totalcell. get (I )). setselect (false);} If (selectindex <totalcell. size () & selectindex>-1) {// set the background totalcell for the selected item. 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;} // itself private baselist blist = This; Public baselist () {This. setlayout (New boxlayout (this, boxlayout. y_axis); Source = new listsource ();}/*** refresh data */Public void refreshdata () {If (source. getallcell (). size ()! = 0) {// sort collections. sort (source. getallcell (), source. getcomparator ();} This. removeall (); this. totalcell. clear (); For (INT I = 0; I <source. getallcell (). size (); I ++) {jcomponent cell = NULL; If (celliface! = NULL) {try {celliface = celliface. getclass (). newinstance ();} catch (exception e) {e. printstacktrace () ;}}if (celliface = NULL) {Cell = new jlabel (source. getallcell (). get (I ). tostring (); cell. setmaximumsize (new dimension (screenutil. getscreewidth (), 30); cell. setpreferredsize (new dimension (0, 30); cell. setopaque (false); // cell. setbackground (color. orange);} else {Cell = celliface. getlistcell (this, sour CE. getallcell (). get (I);} // Add a mouse listener to the entire cell to listen to the event cell. addmouselistener (New mouseadapter () {// click public void mouseclicked (mouseevent e) {for (INT I = 0; I <totalcell. size (); I ++) {If (E. getsource (). equals (totalcell. get (I) {// the currently selected blist. setselectindex (I); break ;}}// move the cursor to public void mouseentered (mouseevent e) {for (INT I = 0; I <totalcell. size (); I ++) {if (I! = Blist. getselectindex () {// unselected if (E. getsource (). equals (totalcell. get (I) {totalcell. get (I ). setopaque (true); totalcell. get (I ). setbackground (blist. getpasscolor (); break ;}}// remove public void mouseexited (mouseevent e) {jcomponent JC = (jcomponent) E. getsource (); If (blist. getselectindex () <totalcell. size () {If (blist. getselectindex ()! =-1) {If (! JC. equals (totalcell. get (blist. getselectindex () {// unselected JC. setopaque (false); JC. setbackground (null) ;}} else {JC. setopaque (false); JC. setbackground (null) ;}}}); // Add the cell to totalcell one by one, and assign this to totalcell here. totalcell. add (cell); this. add (cell);} If (blist. getselectindex ()! =-1 & blist. getselectindex () <source. getallcell (). size () {// set the background totalcell for the selected item. get (blist. getselectindex ()). setbackground (blist. getselectcolor ();} This. revalidate (); this. repaint ();}/*** source data update * @ Param event */Public void sourcerefreshevent (list event) {This. refreshdata ();}}

Http://download.csdn.net/detail/jasper_success/4488517

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.