簡體   English   中英

無法將對象添加到JList以顯示在滾動窗格上

[英]Having trouble adding objects to JList to display on scrollpane

我已經為我所創建的對象創建了一個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;
           }
        }

您需要將對象添加到模型而不是jlist面板。 添加您用於組件的。 嘗試從jlist獲取模型並使用模型的addElement。

您正在使用DefaultListModel ,它使用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並定義渲染器。 之后,您應該在模型上直接對數據進行所有更改。 Ergo使用: model.addHuman()model.removeHuman() 它們觸發JTable偵聽的必要事件以便重新繪制。

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

我希望它有效......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM