How can you get a JList to simple display a one word string.

I know the method of JList.setListData(Object[]);
I have an object[], but it is multiple lines of information and it looks weird in the list display.

How can I get an Object[] to simply display the first line of the array?

Dani AI

Generated

had mult-line entries and wanted the JList to show only a short label; correctly suggested considering layout or a different model. A lightweight, non-destructive approach is to keep your full objects but give the JList a renderer that displays only the first line (or first word) of each item. That way the underlying data stays intact and selections still return the full object for detail views.

A compact renderer example (override the default JLabel renderer so you do not alter the stored objects):

import javax.swing.*;
import javax.swing.DefaultListCellRenderer;
import java.awt.Component;

class FirstLineRenderer extends DefaultListCellRenderer {
    @Override
    public Component getListCellRendererComponent(JList<?> list,
                                                  Object value,
                                                  int index,
                                                  boolean isSelected,
                                                  boolean cellHasFocus) {
        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        if (value == null) {
            setText("");
            setToolTipText(null);
            return this;
        }
        String s = value.toString();
        int nl = s.indexOf('\n');
        if (nl >= 0) s = s.substring(0, nl);
        setText(s.trim());
        setToolTipText(value.toString()); // hover shows full info
        return this;
    }
}

Attach it with myList.setCellRenderer(new FirstLineRenderer());. Alternatives: build a separate array/list of summary strings for display while keeping full objects in a parallel collection, or provide a short-label method on your objects and render that. Watch for HTML-formatted strings (JLabel treats "<html>" specially) and different newline conventions ("\r\n" vs "\n"). For implementation patterns and APIs, see the Swing lists tutorial and the JList API for details on renderers and models: How to Use Lists and JList API.

Recommended Answers

All 2 Replies

You mean display only one element of the array?
You can create an array that has only one element, the element you want to display. But that would make the JList useless since that is not its purpose. Maybe you can change the way you add the JList to the frame, make it a bit wider.
Also if you look at the API there is an example on how to add scrolling to the list.

If you had already thought of that, sorry but that was my thought too.

Another thing would be to make a custom ListModel. Again check the API of the JList

Im not to sure I guess about the actual use of the JList.
It is scrollable now, but it just looks weird.

I looked up how to make the ListModel thing but I had trouble already incorporating that into what I had already made.

So as for now I guess I'll just keep it displaying the entire photo specs.

Thanks javaAddict.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.