problem in showing multiple columns and rows in horizontal wrap as like as

I am trying to implement the ListView in my application as like as in windows.

But i am able to show single row bythis code snippet.

What should ido to show the multiple row ,multiple coloumns in JList.

import javax.swing.*;

import java.awt.*;

import javax.swing.event.ListSelectionEvent;

import javax.swing.event.ListSelectionListener;

publicclass JListExampleextends JFrame

{

privateclass Value

{

Value(String value, Icon image)

{

this.value = value;

this.image = image;

}

String value;

Icon image;

}

private Icon getIcon(String name)

{

returnnew ImageIcon(name);

}

public JListExample()

{

super("Simple JList Example");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final Value[] VALUES =

{

new Value("the road", getIcon("10.jpg")),

new Value("trees", getIcon("11.jpg")),

new Value("stooges", getIcon("12.jpg")),

new Value("bauld tires", getIcon("13.jpg")),

new Value("bvot tires", getIcon("14.jpg")),

new Value("volt tires", getIcon("15.jpg")),

new Value("send tires", getIcon("17.jpg")),

};

JList list =new JList(VALUES);

list.setLayoutOrientation(JList.HORIZONTAL_WRAP);

list.setVisibleRowCount(1);

list.setCellRenderer(new SimpleCellRenderer());

list.addListSelectionListener(new ListSelectionListener(){

publicvoid valueChanged(ListSelectionEvent evt){

if (evt.getValueIsAdjusting())

return;

System.out.println("Selected from " + evt.getFirstIndex()

+" to " + evt.getLastIndex());

}

});

JScrollPane ListViewer =new JScrollPane(list);

getContentPane().add(ListViewer);

setBounds(0,0,600,600);

//pack();

}

publicstaticvoid main(String[] args)

{

try

{

new JListExample().setVisible(true);

}

catch (Exception e)

{

e.printStackTrace(System.out);

}

}

class SimpleCellRendererextends JLabelimplements ListCellRenderer

{

public SimpleCellRenderer()

{

setOpaque(true);

}

public Component getListCellRendererComponent(JList list, Object value,int index,boolean isSelected,boolean cellHasFocus)

{

Value val = (Value)value;

setText(val.value);

setIcon(val.image);

//setBackground(isSelected ? Color.red : (index & 1) ==0 ?Color.cyan : Color.green);

//setForeground(isSelected ? Color.white : Color.black);

returnthis;

}

}

}

[code]

[/code]

[5561 byte] By [hack_javaa] at [2007-10-3 9:43:21]
# 1

The Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/components/list.html]How to Use Lists[/url] has a working example showing you how to use wrapping.

Also I suggest that you override the DefaultListCellRenderer instead of extending JLabel. This way you will still have the default background and highlighting:

public Component getListCellRendererComponent(

JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)

{

super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

// your code here

return this;

}

camickra at 2007-7-15 4:59:40 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thank you. It's work for me.
hack_javaa at 2007-7-15 4:59:40 > top of Java-index,Desktop,Core GUI APIs...
# 3

I know this is an old topic, but I found the solution and thought I'd share it! By default, a JList only has 1 visible row. To allow the number of rows to expand dynamically, throw this in.

this.imageList.setVisibleRowCount(-1);

My code looks like this. It centers each image and centers the text under the image and wraps images horizontally.

ListCellRenderer renderer = new ImageListCellRenderer();

this.imageList.setCellRenderer(renderer);

DefaultListModel listModel = new DefaultListModel();

this.imageList.setVisibleRowCount(-1);

this.imageList.setLayoutOrientation(JList.HORIZONTAL_WRAP);

this.imageList.setModel(listModel);

and the renderer

public class ImageListCellRenderer extends DefaultListCellRenderer {

public Component getListCellRendererComponent(JList list, Object value, int

index, boolean isSelected, boolean hasFocus) {

JLabel label = (JLabel)super.getListCellRendererComponent(list, value,

index, isSelected, hasFocus);

if (value instanceof File) {

File imageFile = (File)value;

String path = imageFile.getAbsolutePath();

Image image = Toolkit.getDefaultToolkit().getImage(path);

image = image.getScaledInstance(100, 100, Image.SCALE_DEFAULT);

ImageIcon imageIcon = new ImageIcon(image);

label.setIcon(imageIcon);

label.setText(path.substring(path.lastIndexOf(File.separatorChar) + 1));

label.setVerticalTextPosition(SwingConstants.BOTTOM);

label.setHorizontalAlignment(SwingConstants.CENTER);

label.setHorizontalTextPosition(SwingConstants.CENTER);

}

else {

label.setIcon(null);

}

return label;

}

}

I need to work on this to improve performance, but it works!

cgarvey80a at 2007-7-15 4:59:40 > top of Java-index,Desktop,Core GUI APIs...