2

How to load a JList in horizontal fashion?? Here is my code,I am trying to display the JListsimilar to the screen shot provided.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.awt.BorderLayout;
import java.io.File;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;

public class Test extends JFrame{

    private JList toolsList;
    private ArrayList<File> toolXmlList;

    public Test()
    {
    toolXmlList = new ArrayList<File>();
    toolXmlList = loadFiles();

    setVisible(true);
    setSize(300,300);
    setTitle("Test Jlist");
    createComponents();

    }

    public void createComponents()
    {
    toolsList = new JList();
    toolsList.setModel(displayDefaltTools());
    toolsList.setLayoutOrientation(javax.swing.JList.VERTICAL_WRAP);
    setLayout(new BorderLayout());
    add(toolsList,BorderLayout.CENTER);
    }

    /**
     * Creates a list model and add the tools to it
     *
     * @return DefaultListModel
     */
    public DefaultListModel displayDefaltTools() {
    DefaultListModel dlistModel = new DefaultListModel();
    String presentation = "";

    for (int i = 0; i < toolXmlList.size(); i++) {
        //System.out.println(idSet.get(i));
        presentation = presentation +  toolXmlList.get(i).getName() ;
        dlistModel.addElement(presentation);
        presentation = "";
    }
    return dlistModel;
    }

    public ArrayList loadFiles()
    {
    ArrayList<File> xmlFiles = new ArrayList<File>();
    File f = new File(".");
    File [] folList = f.listFiles();
    for(int i=0;i<folList.length;i++)
    {
        if(folList[i].getName().startsWith("Tool_Frag"))
        {
            File[] fileList=folList[i].listFiles();
            for(int j=0;j<fileList.length;j++)
            {
                System.out.println(fileList[j].getName());
                xmlFiles.add(fileList[j]);
            }
        }
    }
    return xmlFiles;
    }

    public static void main(String[] args)
    {
    new Test();
    }
}

I am trying to get a jlist in this manner,items displayed one next to another screenshot on layout of jlist

StanislavL
  • 56,971
  • 9
  • 68
  • 98
Balaram26
  • 1,349
  • 3
  • 15
  • 32

1 Answers1

5

You will have to do two things:

  1. Set the LayoutOrientation to JList.HORIZONTAL_WRAP or JList.VERTICAL_WRAP as per the documentation.

  2. Make the list wide enough that it can display more than one element per row. Use setVisibleRowCount() for this.

    Calling setPreferredSize() also works but can cause trouble when you use layout managers.

Alternatively, consider using a JTable if you must make sure a certain number of rows/columns (like all elements in a single line).

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • 2
    Using `setVisibleRowCount()` will leverage the renderer's preferred size; invoking `setPreferredSize()` is [problematic](http://stackoverflow.com/q/7229226/230513). – trashgod Dec 20 '12 at 08:52
  • second bullet is wrong (even if the tutorial spreads such ... crap ;): whatever the problem, setXXSize is _not_ the solution (to at least 2nd approximation). Instead, use a suitable LayoutManager. – kleopatra Dec 20 '12 at 10:31
  • Thanks for the reply. And is it possible to display different icons in JList without any text??? Just the icons alone. – Balaram26 Dec 20 '12 at 10:44
  • @Balaram26: Check the Swing trail for examples how to use a custom list item renderer. – Aaron Digulla Dec 20 '12 at 11:05
  • Thanks for the reply,i did figure out the way to do it :) – Balaram26 Dec 24 '12 at 16:32