I would like to make a cell render with JPanel
so they look like this:
It would be composed of a JPanel
, another JPanel
inside that would have the background changed and two JTextField
or JLabel
I tried to understand how CellRender
works but nobody explains it 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 the design of the cell? in CustomContactCellRender()
or getListCellRendererComponent
?
If someone has a tip also accepted
EDIT 2
I think you're right ... how would you name the names and add% to JList
?