Make a cooler JList interface __list

Source: Internet
Author: User
Tags gettext

Make a cooler JList interface


Content:
1. The working principle of the drawing device.
2. Replace the appearance of JList and JComboBox with a custom drawing.
3. Let the new look respond to mouse events.

With the swing system's MVC design concept, it's easy to replace different skins for components. This article mainly takes jlist and JComboBox as an example to explain the principle and usage of listcellrenderer.


First, how the drawing works
whether it's jlist or jcombobox, they all use Listcellrenderer, Because the jcombobox itself is made up of a pull-down jlist and TextField. Here, they use the callback mechanism. A common way to

callback is to call methods in Class B in Class A. In Class A, you first register an instance reference of a class B (also can be multiple), and then call its internal method through that instance when you need to call it. Such mechanisms are useful in many design patterns, such as observer and so on. There are also AWT event mechanisms used in callback.

to implement callback, Class B is usually designed as an interface that can be callback. In JList's drawing, Swing provides a listcellrenderer interface,
public interface Listcellrenderer {
  Component getlistcellrenderercomponent (
  JList list,
  object value,
  int index,
& nbsp Boolean isselect Ed,
  Boolean cellhasf ocus);
}
This interface provides only one method, and as long as we implement the interface and pass its instance reference to JList, we can replace the jlist with a different style. See how Listcellrenderer Works,
. net/ Turbochen/images/renderer1.gif
Before drawing jlist each cell, it will go to call Getlistcellcomponent (), get a component, and
Draw the component in the correct position. Because Getlistcellcomponent () is returning component, we can almost extend any component to change the appearance of Jlist,jcombobox.

second, make their own drawing device
Now we want to have a list of students in JList, and the student's icon before each list. The following figure shows

Let's start by thinking about what components in swing can display both icons and text. JLabel. That's right. Let's use JLabel as the jlist, and look at my extended JLabel class, which implements the Listcellrenderer interface:
/* can display the icon of the Listcell drawing device * *
public class Iconlistitemrenderer extends JLabel implements Listcellrenderer
{
Private Border
Selectedb order = Borderfactory.createlineborder (color.blue,1),
Emptyborder = Borderfactory.createemptyborder (1,1,1,1);

Public Component Getlistcellrenderercomponent (
JList list,
Object value,
int index,
Boolean isselected,
Boolean cellhasfocus)
{

Iconlistitem item = (iconlistitem) value;
This.seticon (Item.geticon ());
This.settext (Item.gettext ());

if (isselected) SetBorder (Selectedborder);
else SetBorder (Emptyborder);
return this;
}
}
As you can see, the Getlistcellrenderercomponent method passes in several parameters, and we set the appearance of the JLabel by using several of the parameters it passes in: icons and text. In this class, we use a iconlistitem to receive the value that the caller passes over,
Iconlistitem item = (iconlistitem) value;
Iconlistitem is a different class that I have defined, which holds the value of each list item,
Import Java x.swing.*;
public class Iconlistitem
{
icon icon;
String text;
Public Iconlistitem (icon icon, String text)
{
This.icon = icon;
This.text = text;
}
Public icon GetIcon () {return icon;}
Public String GetText () {return text;}
public void SetIcon (icon icon) {This.icon = icon;}
public void SetText (String text) {this.text = text;}
}
In this way, I can use the GetIcon () and the GetText () method to get the value of each list item,
Iconlistitem item = (iconlistitem) value;
This.seticon (Item.geticon ());
This.settext (Item.gettext ());

So far, we can easily replace the appearance of JList in the following ways,
  JList list = new JList ();
  List.setcellrenderer (new Iconlistitemrenderer ());  file://Install our custom cellrenderer
  Defaultlistmodel Listmodel = new Defaultlistmodel ();
  List.setmodel (Listmodel);
  Iconlistitem item = new Iconlistitem (new ImageIcon (...), "John");
  Listmodel.addelement (item); //Add Item to list
  ...
Because JComboBox also has a drop-down list, so its list is drawn with listcellrenderer, so we can also give this iconlistitemrenderer to it with:
  JComboBox List = new JComboBox ();
  List.setrenderer (New Iconlistitemrenderer ()); //Install our custom Cellrenderer
  DefaultComboBoxModel Combomodel = new DefaultComboBoxModel ();
  List.setmodel (Combomodel);
  Iconlistitem item = new Iconlistitem (new ImageIcon (...), "John");
  Combomodel.addelement (item); //Add Item to list
  ...
Note that the Setrenderer () method is used when the JComboBox is installed, JList is in the Setcellrenderer () method, with a slightly different name.

What you see here is a list of icons that can be displayed, and let's look at a list of how the checkbox is implemented, as shown in the following example:

Code implementation:
Import javax.swing.*;
Import java.awt.*;
Import javax.swing.border.*;
Import java.awt.event.*;
/* can display the checkbox's Listcell drawing device * *
public class Checklistitemrenderer extends Jcheckbox implements Listcellrenderer
{
Public Component Getlistcellrenderercomponent (
JList list,
Object value,
int index,
Boolean isselected,
Boolean cellhasfocus)
{
Checklistitem item = (checklistitem) value;
This.setselected (Item.getcheck ());
This.settext (Item.gettext ());
This.setfont (List.getfont ());
This.setenabled (list.isenabled ());
return this;
}
}
Similarly, the value of each list item is stored in a checklistitem in this drawing:
public class Checklistitem
{
Boolean check;
String text;
Public Checklistitem (Boolean check, String text)
{
This.check = check;
This.text = text;
}
public Boolean Getcheck () {return check;}
public void SetCheck (Boolean _check) {check = _check;}
Public String GetText () {return text;}
public void SetText (String _text) {text = _text;}
}
The use of this drawing is the same as that of Iconlistitemrenderer, which is not much spoken.

Third, let the custom paint ring on mouse events
Using the above checklistitemrenderer, you will find that although the list can be displayed checkbox, but with the mouse click, no response! Now I'm going to solve the problem. To be clear, the listcellrenderer itself is simply returning one component to draw a cell and not react to the user's actions. We have to work on JList. JList has a Addmouselistener () method to install a mouse listener for itself, where I implement a mouseadapter and have it respond to mousepressed:
  Class Checklistmouselistener extends Mouseadapter
  {
  public void mousepressed (MouseEvent e) { br>   JList list = (JList) e.getsource ();
  INT index = List.locationtoindex (E.getpoint ());
  Checklistitem item = (Checklistitem) List.getmodel (). Getelementat (index);
  Item.setcheck (! Item.getcheck ());
  Rectangle rect = list.getcellbounds (index, index);
  List.repaint (rect);
 }
 } When
is used, use Addmouselistener (New Checklistmouselistener ()).

In many cases, in addition to the jlist that contain the checkbox, we need to add an action response to the homemade drawing, such as we want to implement an editable jlist, in addition to extending the JTextField and implementing Listcellrenderer, Also write a mouse listener and keyboard listener, when double-clicking, the jlist becomes editable, when the return, restore to the non-editable State. The specific implementation process, I will not be known, and left you to do the exercise.


The above, I wrote a demo program, the following is its demo screen,

You can Download/cellrenderer.rar "> Download the complete demo program from here."


The above article is the original of Turbo Chen, All rights reserved. If you want to reprint, please indicate the source.

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.