【问题标题】:Java: Having trouble adding items to a JList from local JListJava:从本地 JList 向 JList 添加项目时遇到问题
【发布时间】:2013-05-19 04:36:58
【问题描述】:

我在这里遇到了一个问题,我创建了一个 actionListener,它旨在创建一个随机人类并将其添加到 JList 以显示在 JScrollPane 上。到目前为止一切都很好,除了每当我单击 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;
       }
    }

【问题讨论】:

  • 如果你想让java 的人阅读你的代码,camelCase 表示法也很有帮助

标签: java swing jlist


【解决方案1】:

您正在您的ActionListener 中创建一个新的JList newbiex,您从未将其添加到JFrame。而是重新使用原始 JList newbie 并将新的 Human 添加到其模型中。使用可变的DefaultListModel

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

您将需要一个自定义单元格渲染器来显示JList 中的Human 对象。见Writing a Custom Cell Renderer


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;
   }
}

【讨论】:

  • 但是如何为我的单独对象创建自定义单元格渲染器?我对此的经验为零,我试过看一些例子,但我不清楚。
  • 好吧,现在,我创建了该渲染器类(在原始帖子中对其进行了更新),但是它迫使我将“DefaultListRenderer”更改为“DefaultListCellRenderer”,我现在如何在我的循环?谢谢,很抱歉在这里有点迷路
【解决方案2】:

你在这里做了很多奇怪的事情。

您似乎每次单击时都会创建一个 JList,在其中添加一个 Human 并将您的初始模型列表设置为新创建的列表模型。

为了简化您生成一个包含最后添加的 Human 的新模型并将其用作列表的新模型。

唯一要做的就是将您的新人类添加到现有模型中,因此您必须保留对该模型的引用。

您将在 Java 教程中找到更多详细信息:http://docs.oracle.com/javase/tutorial/uiswing/components/list.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-03
    • 2015-10-22
    • 1970-01-01
    • 2011-07-09
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多