簡體   English   中英

Java:無法將項目從本地JList添加到JList

[英]Java: Having trouble adding items to a JList from local JList

我在這里遇到一個問題,我在其中創建了一個actionListener,該監聽器旨在創建一個隨機的人類並將其添加到要顯示在JScrollPane上的JList中。 到目前為止,一切工作都很好,除了每當我單擊JButton添加新人員時,JList都不會添加到當前列表,而是每次都替換它,因此Jlist上只顯示一項。 我知道問題出在哪里,您將在actionevent行中立即看到它。 無論如何,感謝我的朋友們的幫助!

private static final JTextArea PlayerList = new JTextArea(30, 100);
private JList newbie;
private List<Human> members = new ArrayList<Human>();
private JTextArea Area;
private String[] listString;
private String[] newString;
private ArrayList<String> list = new ArrayList<String>();
private ArrayList<String> zz = new ArrayList<String>();


public JTabbedPaneFrame() throws FileNotFoundException 
{
    super("JTabbedPane Demo");


    JTabbedPane tabbedPane = new JTabbedPane(); 

    JPanel Gladiator = new JPanel();
    getContentPane().add(Gladiator); 

    /////////////Tabbed Pane 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"));

    Area = new JTextArea(20, 15);
    Area.setLineWrap(true);
    Area.setWrapStyleWord(false);

    final JList newbie = new JList();

    JScrollPane PlayerViewer = new JScrollPane(newbie);
    PlayerViewer.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    ListOfPlayers.add(PlayerViewer);


    Gladiator.add(ListOfPlayers);
    /////////////Vertical Box between Text and Tabbed Profile//////

    Box Randomer = Box.createVerticalBox();
    Randomer.setBorder(new TitledBorder("Randomize or Add Gladiator"));
    JButton AddIndividual = new JButton("Add a Player");
    Randomer.add(AddIndividual); 

    Gladiator.add(Randomer);



    AddIndividual.addActionListener(new ActionListener()
    {

        public void actionPerformed(ActionEvent event)
        {


                String x = "";
                String y = "";
                String z = "";
                String ee = "";
                ArrayList<String> listx = new ArrayList<String>();
                ArrayList<String> zzx = new ArrayList<String>();
                JList newbiex;
                Human temp;
                try {


                    temp = new Human();
                    x = temp.toString();
                    y = temp.getSurname();
                    z = temp.getFirstName();
                    listx.add(x);
                    ee = String.format(y + ", " + z );
                    zzx.add(ee);
                    listString = new String[zzx.size()];
                    listString = zzx.toArray(listString);

                    newbiex = new JList(zzx.toArray());
                    newbie.setModel(newbiex.getModel());

                    members.add(temp);


                    for(String W: listString) /////testing diff method here////
                    {
                          Area.append(W);
                    }

                    } 
                catch (FileNotFoundException e) 
                    {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    }




        }


    });





    add(tabbedPane); 



    /////////////Action Buttons////////////////////



}

}       

HumanRenderer類擴展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;
       }
    }

您將在ActionListener中創建一個新的JList newbiex ,而從未添加到JFrame 而是重新使用原始的JList newbie ,並將新的Human添加到其模型中。 使用一個可變的DefaultListModel

DefaultListModel<Human> model = new DefaultListModel<>();
JList<Human> newbie = new JList<>(model);

您將需要一個自定義單元格渲染器,以在JList顯示Human對象。 請參閱編寫自定義單元格渲染器


class HumanRenderer extends DefaultListRenderer {
   @Override
   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,在其中添加一個Human,並將初始模型的列表設置為新創建的列表的模型。

為簡化起見,您將生成一個包含最后添加的Human的新模型,並將其用作列表的新模型。

唯一要做的就是將新的Human添加到EXISTING模型中,因此您必須保留對該模型的引用。

您可以在Java教程中找到更多詳細信息: http : //docs.oracle.com/javase/tutorial/uiswing/components/list.html

暫無
暫無

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

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