【问题标题】:Removing item from JList with JButton, but won't refresh使用 JButton 从 JList 中删除项目,但不会刷新
【发布时间】:2014-06-17 18:27:07
【问题描述】:

我已经搜索和搜索并找到了 800 种解决方案,但似乎没有一个可以解决我的问题。我正在使用 JButton 从 JList 中删除一个项目,然后我想在 actionPerformed 方法中刷新 GUI。但是像 repaint() 或 updateUI() 这样的东西并没有帮助。这是我的代码:

public class Watchlist3 extends JPanel {

public static ArrayList<String> stocks = new ArrayList<String>();

JButton addStock, removeStock, viewStock, updaterInterval;
JLabel stocksAdded, currentInterval, listTitle;
JList stocklist;
JScrollPane listScroller;

    public Watchlist3(JFrame frame) {
    super(new BorderLayout());

   //Adding some sample-components to the list
    stocks.add("PLUG");
    stocks.add("IDN");
    stocks.add("GOOG");

    //Create the components
    addStock = new JButton("Add Stock");
    addStock.setOpaque(true);
    addStock.setBackground(Color.RED);
    add(addStock, BorderLayout.LINE_START);

    removeStock = new JButton("Remove Stock");
    removeStock.setOpaque(true);
    removeStock.setBackground(Color.YELLOW);
    removeStock.putClientProperty("SENT_FRAME", frame);
    add(removeStock, BorderLayout.LINE_END);


    stocklist = new JList(stocks.toArray());
    stocklist.setOpaque(true);
    stocklist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    stocklist.setLayoutOrientation(JList.HORIZONTAL_WRAP);
    add(listScroller = new JScrollPane(stocklist), BorderLayout.CENTER);

removeStock.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                int index = stocklist.getSelectedIndex();
                if(index != -1){
                    stocks.remove(index);
                    System.out.println(stocks);

                    /* Here is where id like to refresh the gui! */
                }   
            } catch (Exception ex) {}
        }
    }); 
}

private static void createAndShowGUI() {
    //Create and set up the window.
    final JFrame frame = new JFrame("Watchlist");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    Watchlist3 newContentPane = new Watchlist3(frame);
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}


public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}
}

提前致谢

【问题讨论】:

    标签: java user-interface refresh jlist repaint


    【解决方案1】:

    您正在从 stock List 集合中删除,但没有更新 JList 模型。

    要更新列表模型,你需要把这个

    // Add this item to the list and refresh
    
    // convert stock list to Object array becuase seListData accept Object[]
    Object[] array = stocks.toArray(new Object[stocks.size()]);
    stocklist.setListData(array);
    listScroller.revalidate();
    listScroller.repaint();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-12
      • 2015-10-18
      相关资源
      最近更新 更多