[Java] Advanced Swing-JList

An interface design is widely used in Java, which makes the performance of Java's Swing components very flexible.

How is Jlist designed? In fact: Jlist separates graphics and data. T can be an array of strings in JList.

String[] text={
    
    A,B,C,D};
JList<String> list=new JList<String>(text);

If you want to add list elements freely, you can get a DefaultListModel, which implements the ListModel interface

DefaultListModel<String> model=new DefaultListModel<>();

Use addElement() to add elements, and also notify JList to redraw the list.

To change the appearance of JList, you need to set the cell renderer, which needs to inherit JComponent and implement the ListCellRenderer interface

Every time a cell is drawn, the public Component
getListCellRendererComponent(JList<? extends Object> list, Object value,
int index, boolean isSelected, boolean cellHasFocus) method is called, and the
Component object is returned , and the cell is drawn using the paintComponent() method After drawing the grid, call
getPreferredSize() to return the size of the component.

There is a simple way to create a JLabel object and return it in the public Component
getListCellRendererComponent(JList<? extends Object> list, Object value,
int index, boolean isSelected, boolean cellHasFocus) method, so that we don’t need to draw with a brush (Graphics object) Up. This is equivalent to adding tags to the list.

JList does not have a scroll bar, and we also need to customize it ourselves.

I have to admit that this is very complicated, but this design allows us to freely set the style of the list. You can even customize its style for each cell. This may require more code to be implemented in other languages.

An example is given below

package JList;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;

import javax.swing.DefaultListModel;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;

public class JListTest {
    
    

	public static void main(String[] args) 
	{
    
    
		AreaListFrame frame=new AreaListFrame("Area");
		EventQueue.invokeLater(()->{
    
    
			frame.setVisible(true);
		});
	}
	
}
class AreaListFrame extends JFrame
{
    
    
	private JList<String> areaList;
	public AreaListFrame(String title)
	{
    
    
		super(title);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLayout(new BorderLayout());
		JPanel cpanel=new JPanel();
		DefaultListModel<String> model=new DefaultListModel<>();
		model.addElement("安徽");		
		model.addElement("湖南");
		model.addElement("山东");
		model.addElement("东北");
		model.addElement("辽宁");
		model.addElement("湖北");
		model.addElement("河北");
		model.addElement("河南");
		model.addElement("江苏");
		model.addElement("浙江");
		model.addElement("新疆");
		areaList=new JList<>(model);
		areaList.setCellRenderer(new MyRenderer());
		//后来加入元素,列表自动重新绘制
		model.addElement("黑龙江");
		cpanel.add(new JScrollPane(areaList),BorderLayout.CENTER);
		add(cpanel);
		pack();
	}
	class MyRenderer extends JComponent implements ListCellRenderer<String>
	{
    
    
		private Font font=new Font(Font.SANS_SERIF,Font.PLAIN,18);
		private String text;
		private int index;
		private Color evenColor=Color.LIGHT_GRAY;
		private Color oddColor=new Color(34, 134, 235);
		private Color selectedColor=Color.RED;
		private Color background;
		@Override
		public Component getListCellRendererComponent(JList<? extends String> list, String value, int index,
				boolean isSelected, boolean cellHasFocus)
		{
    
    
			text=value;
			
			
			list.setSelectionBackground(selectedColor);
			this.index=index;
			//根据是否选中得到动态的颜色
			ibackground = isSelected ? selectedColor : index%2==0? evenColor:oddColor;
			return this;
		}
		public void  paintComponent(Graphics g)
		{
    
    
			FontMetrics fm=g.getFontMetrics(font);
			
			g.setColor(background);
			g.fillRect(0, 0, getWidth(), getHeight());
			g.setColor(Color.GREEN);
			g.drawString(text,0 ,fm.getAscent());
		}
		@Override
		public Dimension getPreferredSize()
		{
    
    
			Graphics g=getGraphics();
			FontMetrics fm=g.getFontMetrics(font);
			return new Dimension(fm.stringWidth(text),fm.getHeight());
		}
		
		/*public Component getListCellRendererComponent(JList<? extends String> list, String value, int index,
				boolean isSelected, boolean cellHasFocus)
		{
			JLabel j1=new JLabel(value);
			
			j1.setOpaque(true);
			evenColor=Color.LIGHT_GRAY;
			oddColor=new Color(34, 134, 235);
			if(index%2==0) j1.setBackground(evenColor);
			else j1.setBackground(oddColor);
			
			j1.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,16));
			if(isSelected) j1.setBackground(Color.YELLOW);
			
			return j1 ;
		}
		*/
	}
	
}

Guess you like

Origin blog.csdn.net/m0_47202518/article/details/108301520