Java Cell Renderer

时间:2015-10-21 23:48:31

标签: java swing jlist

我有一个Item类,这个Item类也可以包含其他项。我想显示这些项目(项目的名称),我已经通过定义DefaultItemCellRenderer来完成它。但是,正如我所提到的,项目可以容纳其他项目,这就是问题所在。怎么能让它不溢出?

项目

import java.util.HashMap;

public class Item{
    /*

    */
    public enum Status{
        COMPLETED, PENDING, CLOSED, CANCELLED, OTHER
    }


    /*

    */
    private Status status;
    /*
        String to store Item name
    */
    private String name;

    /*
        String to store barcode if applicable
    */
    private String barCodeNo;

    /*
        int for local ID, this ID is local to each business (optional)
    */
    private int localId;

    /*
        double holds price of this Item
    */
    private double price;

    /*
        String to provide the category of this Item (optional)
    */
    private String category;

    /*
        String holding the description of this Item (optional)
    */
    private String description;

    /*

    */
    private Item[] addons;

    public Item(String name, String barCodeNo, int localId, Status status){
        this.name = name;
        this.status = status;
        this.barCodeNo = barCodeNo;
        this.localId = localId;
    }
    public Item(String name, String barCodeNo){
        this(name, barCodeNo, -1, Status.OTHER);
    }
    public Item(String name, Status status){
        this(name, null, -1, status);
    }
    public Item(String name){
        this(name, null, -1, Status.OTHER);
    }
    public Item(Item item){
        this.name = item.name;
        this.barCodeNo = item.getBarCodeNumber();
        this.localId = item.getLocalID();
        this.status = item.getStatus();
        this.price = item.getPrice();
        this.category = item.getCategory();
        this.description = item.getDescription();
        this.addons = item.getAddOns();
    }
    public void setName(String name){
        this.name = name;
    }
    public void setBarCodeNumber(String barCodeNo){
        this.barCodeNo = barCodeNo;
    }
    public void setLocalID(int localId){
        this.localId = localId;
    }
    public void setPrice(double price){
        this.price = price;
    }
    public void setCategory(String category){
        this.category = category;
    }
    public void setDescription(String description){ 
        this.description = description;
    }
    public void setStatus(Item.Status status){
        this.status = status;
    }
    public void putAddOns(Item[] addons){
        this.addons = addons;
    }

    public String getName(){
        return this.name;
    }
    public String getBarCodeNumber(){
        return this.barCodeNo;
    }
    public int getLocalID(){
        return this.localId;
    }
    public double getPrice(){
        return this.price;
    }
    public String getCategory(){
        return this.category;
    }
    public String getDescription(){
        return this.description;
    }
    public Item.Status getStatus(){
        return this.status;
    }
    public Item[] getAddOns(){
        return this.addons;
    }

    public String toString(){
        return "name="+this.name+", barCodeNumber"
        +this.barCodeNo+", localId="+this.localId
        +", price="+this.price+", description="+this.description;
    }
}

渲染

 import javax.swing.ListCellRenderer;
    import javax.swing.JList;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JList;
    import javax.swing.DefaultListModel;
    import javax.swing.SwingConstants;
    import javax.swing.BorderFactory;

    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Component;
    import java.awt.Font;
    import java.awt.Color;
    import java.awt.Dimension;

    import java.util.HashMap;

    public class DefaultItemCellRenderer extends JPanel implements ListCellRenderer{
        private Font font;
        private JLabel label;
        private JList<Item> mods;
        private DefaultListModel <Item> model;

        public DefaultItemCellRenderer(){
            GridBagConstraints gbc = new GridBagConstraints();
            this.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));
            this.setLayout(new GridBagLayout());


            this.label = new JLabel();


            this.label.setHorizontalAlignment(SwingConstants.LEFT);
            this.label.setHorizontalAlignment(JLabel.LEFT);
            this.label.setVerticalAlignment(JLabel.CENTER);
            this.label.setFont(new Font("Arial", Font.PLAIN, 17));
            this.label.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MIN_VALUE));

            this.model = new DefaultListModel<Item>();

            this.mods = new JList<Item>(model);
            /*
                Either one of these two fail

                this.mods.setCellRenderer(this);

                this.mods.setCellRenderer(new DefaultItemCellRenderer());
            */
            this.mods.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
            this.mods.setBackground(new Color(255, 255, 255, 0));
            this.mods.setFont(new Font("Arial", Font.PLAIN, 12));
            this.mods.setOpaque(false);


            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            this.add(this.label, gbc);
            this.add(this.mods, gbc);
        }
        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean hasFocus){
            Item item = ((Item)value);
            label.setText(item.getName());

            Item[] addons = item.getAddOns();
            if(addons != null){
                for(Item i : addons){
                    model.addElement(i);
                }
            }


            if(Item.Status.PENDING == item.getStatus()){
                Color pending = new Color(255, 150, 25);
                label.setForeground(pending);
                mods.setForeground(pending);
            }
            else{
                Color other = new Color(0, 0, 0);
                label.setForeground(other);
                mods.setForeground(other);
            }

            if(isSelected) {
                this.setBackground(new Color(115, 235, 255));
            }
            else{
                this.setBackground(new Color(255, 255, 255));
            }



            return this;
        }
    }

0 个答案:

没有答案