Jlistview en java

Description

Une simple JListView, en effet après recherche il ne semble pas exister ce control avec les éléments de base de Java.

English :
A simple JListView, after many research it seems that this usefull and common control does not exist with basics element from Java.

Source / Exemple :


import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;

import javax.swing.DefaultListCellRenderer;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListModel;

import javax.swing.event.ListDataListener;

public class JListView extends JList{
	private void genericParameter(int width, int height){
		this.setFixedCellHeight(height);
        this.setFixedCellWidth(width);
		this.setLayoutOrientation(JList.HORIZONTAL_WRAP);
		this.setVisibleRowCount(-1);
	}

	public JListView(){genericParameter(120,65);}
	public JListView(ObjectListViewModel obj){super(obj);genericParameter(120,65);}

	public JListView(Dimension cellDimension){genericParameter(cellDimension.width, cellDimension.height);}
	public JListView(ObjectListViewModel obj, Dimension cellDimension){super(obj);genericParameter(cellDimension.width, cellDimension.height);}

	public JListView(int cellWidth, int cellHeight){genericParameter(cellWidth, cellHeight);}
	public JListView(ObjectListViewModel obj, int cellWidth, int cellHeight){super(obj);genericParameter(cellWidth, cellHeight);}
}

class ObjectListViewModel implements ListModel{
	private ObjectListViewCollection collection;

	public ObjectListViewModel(ObjectListViewCollection collection){this.collection = collection;}
	public Object getElementAt(int index){return(collection.getObjectListView()[index]);}
	public ObjectListViewCollection getObjectListViewCollection(){return this.collection;}
	public int getSize(){return(collection.getObjectListView().length);}
	public void addListDataListener(ListDataListener l){}
	public void removeListDataListener(ListDataListener l){}
}

class ObjectListViewCollection{
	private ObjectListView[] obj;

	public ObjectListViewCollection(ObjectListView[] obj){this.obj = obj;}
	public ObjectListViewCollection(){obj = null;}

	public void setObjectListView(ObjectListView[] obj){this.obj = obj;}
	public ObjectListView[] getObjectListView(){return obj;}
}

class ObjectListView{
	private String text;
	private Icon ico;

	private void genericParameter(String text){
		this.setText(text);
	}

	public ObjectListView(){
		this.ico=null;
		this.setText("");
	}

	public ObjectListView(String text, Icon ico){
		this.setIcon(ico);
		this.setText(text);
	}

	public ObjectListView(String text, String ico){
		this.setPathIcon(ico);
		this.setText(text);
	}

	public ObjectListView(String text, ImageIcon ico){
		this.setImageIcon(ico);
		this.setText(text);
	}

	public String toString(){return this.text;}
	public String getText(){return this.toString();}
	public Icon getIcon(){return this.ico;}

	public void setText(String text){this.text=text;}
	public void setIcon(Icon ico){this.ico=ico;}
	public void setPathIcon(String path){this.ico = (Icon) (new ImageIcon(path));}
	public void setImageIcon(ImageIcon ico){this.ico = (Icon) ico;}
}

class jListViewRenderer extends DefaultListCellRenderer{
	private Color internalColor;

	public jListViewRenderer(){this.internalColor=this.getBackgroundColor();}
	public jListViewRenderer(Color color){this.internalColor=color;}
	public void setBackgroundColor(Color color){this.internalColor=color;}
	public Color getBackgroundColor(){return this.internalColor;}

	//The object value contains only objects of type ObjectListView
	@Override
	public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus){
		JPanel cellule = new JPanel();
		ObjectListView obj = (ObjectListView) value;
		//If you remove this, you get blue color on full item
		cellule.setBackground(this.internalColor);
		cellule.setLayout(new BorderLayout());

		JPanel labelPanel = new JPanel();
		labelPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
		labelPanel.setBackground(this.internalColor);
		JLabel lbl = (JLabel) super.getListCellRendererComponent(list, obj.getText(), index, isSelected, cellHasFocus);
		labelPanel.add(lbl);
		
		JPanel iconPanel = new JPanel();
		iconPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
		iconPanel.setBackground(this.internalColor);
		iconPanel.add(new JLabel(obj.getIcon()));

		cellule.add(iconPanel, BorderLayout.CENTER);
		cellule.add(labelPanel, BorderLayout.SOUTH);

		return cellule;
	}
}

Conclusion :


Simple dérivé de JList, elle permet d'obtenir une version fidèle du control List View disponible dans la majorité des langages.

Inclus dans le zip :
Le code source de la JListView
Un exemple d'utilisation qui envoi à l'objet :
-une icone (sous forme java Icon)
-une image (sous forme java ImageIcon)
-un chemin (sous forme java String)

Cette classe ayant été réalisée pour mes propres besoins, et étant suffisante pour moi, elle ne sera pas mise à jour...

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.