Jlist Custom Cellrender

Asked

Viewed 320 times

1

I’d like to make a Cor yield with JPanel so that they would be as follows: inserir a descrição da imagem aqui

It would consist of a JPanel, another JPanel inside that would have changed the background and two JTextField or JLabel

I tried to understand how the CellRender but nobody explains right

public class CustomContactCellRender extends JPanel implements ListCellRenderer<Object>{

    private static Contact_Info cinfo;
    JLabel name;
    JLabel msg;

    public CustomContactCellRender() {

        setOpaque(true);
        setLayout(new BoxLayout(this , BoxLayout.Y_AXIS));

        JPanel overallPanel = new JPanel();
        overallPanel.setLayout(new BoxLayout(overallPanel , BoxLayout.X_AXIS));
        overallPanel.setPreferredSize(new Dimension(40,400));
        add(overallPanel);

        JPanel firstPanel = new JPanel();
        firstPanel.setPreferredSize(new Dimension(40,40));
        firstPanel.setLayout(new BorderLayout());
        overallPanel.add(firstPanel);

        JPanel statusPanel = new JPanel();
        statusPanel.setPreferredSize(new Dimension(37,37));
        statusPanel.setLayout(new BorderLayout());

        firstPanel.add(statusPanel , BorderLayout.CENTER);
        firstPanel.add(Box.createRigidArea(new Dimension(3,40)) , BorderLayout.LINE_START);
        firstPanel.add(Box.createRigidArea(new Dimension(3,40)) , BorderLayout.LINE_END);
        firstPanel.add(Box.createRigidArea(new Dimension(40,3)) , BorderLayout.PAGE_END);
        firstPanel.add(Box.createRigidArea(new Dimension(40,3)) , BorderLayout.PAGE_START);

        JPanel photoPanel = new JPanel();
        photoPanel.setPreferredSize(new Dimension(32,32));
        photoPanel.setLayout(null);
        statusPanel.add(photoPanel);

        JPanel secondPanel = new JPanel();
        secondPanel.setLayout(new BoxLayout(secondPanel , BoxLayout.Y_AXIS));
        overallPanel.add(secondPanel);

        secondPanel.add(Box.createRigidArea(new Dimension(3,this.getWidth())));

        name = new JLabel();
        name.setPreferredSize(new Dimension(100,15));
        secondPanel.add(name);

        secondPanel.add(Box.createRigidArea(new Dimension(4,this.getWidth())));

        msg = new JLabel();
        msg.setPreferredSize(new Dimension(this.getWidth(),15));
        secondPanel.add(msg);

        secondPanel.add(Box.createRigidArea(new Dimension(4,this.getWidth())));
    };

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

        cinfo = Contact_Info.getInstance();

        name.setText(cinfo.getFirst_name(1));

        return this;
    }

EDIT: Updated Code

Where is Cell’s design? in CustomContactCellRender() or in getListCellRendererComponent?

If anyone has a tip I also accept

EDIT 2

I think you’re right... how I would name and add to JList?

  • 1

    Hello Lucas, if no one qualifies first tomorrow I write a full answer. That said, see that there is an example of ListCellRenderer here (being applied to a ComboBox, but it doesn’t matter). You can put logic both in the constructor (CustomContactCellRender) how much in the method that recovers the component (getListCellRendererComponent).

  • 1

    The constructor is the best place to place fixed logic, i.e. that does not vary according to the value (e.g., the panel layout); the method should do the rest (e.g., set the image to be displayed according to the current item; draw a border if the cell is in focus, etc).

1 answer

1


Lucas, making my comments a response as promised.

Yes you are on the right track. There is material about ListCellRender in the official Java tutorial. You can find the sample source code here (it customizes a JComboBox, but there is not much difference from practice).

About where to put each thing:

  1. CustomContactCellRender() is the builder of redenderer, you can put everything fixed here; the layout of your dashboard, its components and everything else that is relevant.

  2. getListCellRendererComponentis the method that will return the component for each item. Fine adjustments and display of model values should all be done in this method, because only in it you will have access to the list object (value), element position (index), etc..

On how to set the name

Always remember the separation between model and redenderer. What feeds the list with values is the ListModel (whether you started the combo with an array or Vector the java built a model anonymous who inherits from Abstractlistmodel for you).

Well, if you have one ListModel of objects of the type Contact, the values of Renderer should also deal with Contact (you can specify the type in the declaration: implements ListCellRenderer<Contact>).

Once that’s done the only thing redenderer should do is feed its component with the received values:

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

    name.setText(value.getNome());
    msg.setText(value.getMensagem()); 

    return this;
}

Of course in a real application you should also have some visual indicative of focus and selection; for example, change the color of the background or draw a border):

if (isSelected) {
    setBackground(list.getSelectionBackground());
    setForeground(list.getSelectionForeground());
} else {
    setBackground(list.getBackground());
    setForeground(list.getForeground());
}

if (cellHasFocus) {
   setBorder(UIManager.getBorder("List.focusCellHighlightBorder"));
} else
   setBorder(new EmptyBorder(1, 1, 1, 1));
} 

To adjust this type of detail it is good to take a look at the source code of the class Defaultlistcellrenderer (This varies greatly depending on the JDK version).

The whole working together:

Contact[] myContacts = new Contact[] {
    new Contact("Hans Solo", "Do not call him Harrison Ford"),
    new Contact("Jabba the Hutt", "Not Pizza Hut")
};
JList<Contact> list = new JList<>(myContacts);
list.setCellRenderer(new CustomContactCellRender());
  • Thank you very much. Would you have any means of contact? if you have any further questions?

  • I’m happy to help. I usually keep an eye on the site. In my profile you find my blog (and it is also not very difficult to find me in other networks ;).

  • I sent a face message with an error that is returning

  • Hi Lucas, I didn’t get it. Don’t want to create a new question here at Sopt? Hugs

  • @Antony Accioly http://pastebin.com/aTLyUi5S this is the class Customcell would take a check?

  • Hello Lucas, all right? At first glance I found nothing wrong with your code (apart from the absence of code to handle selected items and Focus). Don’t you want to open a question by pointing out the error you’re getting and the problem you’re having? Hugs

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.