0

我一直在为我制作的对象创建一个 defaultlistcellrender,但到目前为止,将对象添加到 JList 非常困难。我附上了任何建议的代码。谢谢!

public class JTabbedPaneTester extends JFrame 
{
    private List<Human> members = new ArrayList<Human>();
    private JList newbie = new JList();
    private DefaultListModel model = new DefaultListModel();

    public JTabbedPaneTester() throws FileNotFoundException
    {
        super("JTabbedPane Demo");
        JTabbedPane tabbedPane = new JTabbedPane(); 
        JPanel gladiator = new JPanel();
        getContentPane().add(gladiator); 
        tabbedPane.addTab("Gladiator", null, Gladiator, "");
        Box listOfPlayers = Box.createVerticalBox();
        listOfPlayers.add(Box.createRigidArea(new Dimension(100,100)));
        listOfPlayers.setBorder(new TitledBorder("List of Players"));
        JScrollPane playerViewer = new JScrollPane();
        playerViewer.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        listOfPlayers.add(playerViewer);
        JButton AddIndividual = new JButton("Add a Player");
        listOfPlayers.add(addIndividual);
        gladiator.add(listOfPlayers);
    final HumanListModel modelx = new HumanListModel();
    final JTable newbiex = new JTable(modelx);
    newbiex.setDefaultRenderer(Human.class, new HumanRenderer());
    playerViewer.setViewportView(newbiex);

    addIndividual.addActionListener(new ActionListener()
    {

        public void actionPerformed(ActionEvent event)
        {
                    Human temp;
                try {
                    temp = new Human();     
                    modelx.addHuman(temp);
                        } 
                    catch (FileNotFoundException e) 
                        {
                            e.printStackTrace();
                        }       
                    }
        });
        add(tabbedPane); 
    }
}       

这是这里有人很好地帮助我的渲染器

class HumanRenderer extends DefaultListCellRenderer

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


              JLabel label = new JLabel(); 
              if (value != null) 
              {
                 Human human = (Human) value;
                 label.setText(human.getSurname() + ", " + human.getFirstName());
              }

              return label;
           }
        }
4

2 回答 2

2

您需要将对象添加到模型而不是 jlist 面板。添加您用于组件的。尝试从 jlist 获取模型并使用模型的 addElement 。

于 2013-05-27T04:04:54.070 回答
0

您正在使用DefaultListModelwhich 使用DefaultListCellRenderer. 我在您的代码中没有看到任何实际使用您的HumanRenderer. 您必须编写自己的模型。

public class HumanListModel extends DefaultListModel
{
    private ArrayList<Human> data;

    public HumanListModel()
    {
        super();
        data = new ArrayList<Human>();
    }

    public void addHuman(Human h)
    {
       // add new human to the model
       data.add(h);
       fireTableStructureChanged();
    }

    public void removeHuman(Human h)
    {
        data.remove(h);
        fireTableStructureChanged();
    }

    @Override
    public int getColumnCount()
    {
        // the number of columns you want to display
        return 1;
    }

    @Override
    public int getRowCount()
    {
        return data.size();
    }

    @Override
    public Object getValueAt(int row, int col)
    {
        return (row < data.size()) ? data.get(row) : null;
    }

    @Override
    public String getColumnName(int col)
    {
        return "Human";
    }

    @Override
    public Class getColumnClass(int col)
    {
        return Human.class;
    }
}

对于您JTable,您只需设置HumanListModel并定义您的渲染器。之后对数据的所有更改都应直接在模型中进行。尔格使用:model.addHuman()model.removeHuman()。他们触发必要的事件,JTable以便重新绘制。

HumanListModel model = new HumanListModel();
JTable newbies = new JTable(model);
newbies.setDefaultRenderer(Human.class, new HumanRenderer());

我希望它有效...

于 2013-05-27T04:35:42.340 回答