简体   繁体   English

使用JButton从JList中删除项目,但不会刷新

[英]Removing item from JList with JButton, but won't refresh

I've searched and searched and found like 800 solutions, but none of 'em seems to work with my problem. 我已经搜索了800多个解决方案,但是'em似乎都不适合我的问题。 I'm removing an item from a JList using a JButton, and then i want to refresh the GUI in the actionPerformed method. 我正在使用JButton从JList中删除一个项目,然后要在actionPerformed方法中刷新GUI。 But things like repaint() or updateUI() hasn't helped. 但是像repaint()或updateUI()这样的事情并没有帮助。 Here is my code: 这是我的代码:

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

Thanks in advance 提前致谢

You are removing from stocks List collection but not updating the JList model. 您要从库存清单集合中删除,但不更新JList模型。

To update list model you need put this 要更新列表模型,您需要将此

// 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM