将项添加到另一个类的现有jlist中 [英] Adding items to an already existing jlist from another class

查看:171
本文介绍了将项添加到另一个类的现有jlist中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用NetBeans IDE中的Desing模式创建的jList(名为JList1),我想使用一个辅助类将项添加到该列表中,该类解析一个大的xml列表并从中获取数据。我的问题是,我真的不明白如何做到这一点,我已经尝试了很多不同的代码,尝试了一个模型,但不能正确。我是java(也是编程)的新手,我不明白我是否做过像
String [] ar = {one,two,three };

JList Jlist1 = new JList(ar);


这创建了一个新的jList而不是使用我已创建的jList,不是吗?

I have a jList (named JList1) created using the Desing mode from NetBeans IDE, and I want to add items to that list using a secondary class which parses a big xml list and gets the data from it. My problem is that I dont really understand how to do this, I already tried a lot of different codes, tried with a model too, but cant get it right. I am new to java (and programming too), and I dont understand if I do something like
String[] ar = {"one", "two", "three"};
JList Jlist1 = new JList(ar);

this created a new jList instead of using my already created one, no ?

推荐答案

created using the Desing mode from NetBeans IDE,




    • maybe not good idea to be prisonier of code generated by

      将新项目添加到DefaultListModel

      add a new Item to DefaultListModel


      并且我想使用辅助类向该列表添加项目,
      解析一个大的xml列表并从中获取数据。

      and I want to add items to that list using a secondary class which parses a big xml list and gets the data from it.




      • 听起来像你有一个 Swing中的Concure ,必须在EDT上对已经可见的Swing GUI进行更新

        • sounds like as you have an issue with Concurency in Swing, updates to the already visible Swing GUI must be done on EDT

          使用 SwingWorker#publish()用于长期和艰苦的工作(解析一个大的xml列表并从中获取数据。)

          use SwingWorker#publish() for long and hard job (which parses a big xml list and gets the data from it.)

          例如,向DefaultListModel添加一个新项目

          for example, add a new Item to DefaultListModel

          import javax.swing.*;
          import java.awt.*;
          import java.awt.event.*;
          
          public class Testing extends JFrame {
          
              private static final long serialVersionUID = 1L;
              private DefaultListModel listModel = new DefaultListModel();
              private JList list = new JList(listModel);
              private int currentSelectedRow = 0;
              private int xX = 0;
          
              public Testing() {
                  setLocation(400, 300);
                  setDefaultCloseOperation(EXIT_ON_CLOSE);
                  for (int x = 0; x < 9; x++) {
                      listModel.addElement("" + x);
                      xX++;
                  }
                  JScrollPane sp = new JScrollPane(list);
                  add(sp, BorderLayout.CENTER);
                  JButton btn1 = new JButton("Reset Model CastingModel");
                  add(btn1, BorderLayout.NORTH);
                  btn1.addActionListener(new ActionListener() {
                      @Override
                      public void actionPerformed(ActionEvent ae) {
                          list.clearSelection();
                          DefaultListModel model = (DefaultListModel) list.getModel();
                          model.removeAllElements(); 
                          // note Swing GUI by default to freeze if is removed more that 
                          // 999 elemets from the JList or its underlaying XxxListModel, 
                          // to use repeatly Actions from SwingTimer on short period
                          for (int x = 0; x < 9; x++) {
                              model.addElement("" + (x + xX));
                              xX++;
                          }
                          list.setModel(model);
                      }
                  });
          
                  JButton btn2 = new JButton("Reset Model directly from Model");
                  add(btn2, BorderLayout.SOUTH);
                  btn2.addActionListener(new ActionListener() {
                      @Override
                      public void actionPerformed(ActionEvent ae) {
                          list.clearSelection();
                          listModel.removeAllElements();
                          for (int x = 0; x < 9; x++) {
                              listModel.addElement("" + (x + xX));
                              xX++;
                          }
                      }
                  });
                  pack();
              }
          
              public static void main(String[] args) {
                  SwingUtilities.invokeLater(new Runnable() {
                      @Override
                      public void run() {
                          new Testing().setVisible(true);
                      }
                  });
              }
          }
          

          这篇关于将项添加到另一个类的现有jlist中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆