Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Java ListView

  Asked By: Muaz    Date: May 27    Category: Java    Views: 1533
  

I want to create a swing ListView like the Visual Basic Listview.
Before I go any further, does a ListView already exist?

I create this class:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

class prueba extends JFrame{

JList l = new JList();
JScrollPane s = new JScrollPane(l);
private DefaultListModel dlm = new DefaultListModel();

prueba(){

dlm.addElement (new JLabel("hoal", new ImageIcon("myImagen.gif"),
JLabel.CENTER));
dlm.addElement (new JLabel("pepe", new ImageIcon("myImagen.gif"),
JLabel.CENTER));
dlm.addElement (new JLabel("fancis", new ImageIcon("myImagen.gif"),
JLabel.CENTER));
dlm.addElement (new JLabel("fancis", new ImageIcon("myImagen.gif"),
JLabel.CENTER));
dlm.addElement (new JLabel("fancis", new ImageIcon("myImagen.gif"),
JLabel.CENTER));

l.setModel(dlm);
l.setCellRenderer (new MiRendererDeLista());
l.setLayoutOrientation (JList.HORIZONTAL_WRAP);

this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
getContentPane().add (s);
pack();
}
public static void main(String[] a){
prueba p= new prueba();
p.setVisible(true);
}

class MiRendererDeLista extends JLabel implements ListCellRenderer {

public Component getListCellRendererComponent(JList lista,
Object valor,int indice, boolean seleccionado, boolean conFoco ) {
setText( ((JLabel)valor).getText() );
setIcon( ((JLabel)valor).getIcon() );
setHorizontalTextPosition(JLabel.CENTER);
setVerticalTextPosition(JLabel.BOTTOM);
setFocusable(true);
if ( seleccionado ) {
// .. en el caso de un item marcado (rojo/blanco)
System.out.println ("seleccionado");
setBackground( Color.pink );
setForeground( Color.GRAY );
}else {
// .. en el caso de un item no marcado ( gris/negro)
System.out.println ("No seleccionado");
setBackground( Color.lightGray );
setForeground( Color.black );
}
return( this );
}
}
}

But l.setLayoutOrientation (JList.HORIZONTAL_WRAP); seems don't run.
And JLabel can't paint ours background.

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on Java ListView Or get search suggestion and latest updates.




Tagged: