1
I’d like to make a Cor yield with JPanel
so that they would be as follows:
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
?
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 aComboBox
, but it doesn’t matter). You can put logic both in the constructor (CustomContactCellRender
) how much in the method that recovers the component (getListCellRendererComponent
).– Anthony Accioly
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).
– Anthony Accioly