在按钮事件中刷新GUI中的JList

时间:2015-08-19 18:14:50

标签: java swing user-interface

我已经在线查看了如何在添加到模型后更新JList。有人说它是自动完成的,有人说你必须手动完成它。我已经试过了两次,但我还没有取得多大成功。我试图用addElement方法更新模型,然后通过。我已经浏览了代码,一切都正确通过。 GUI不刷新。

package com.user.tutorial;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.BorderLayout;

import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.DefaultListModel;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.Box;
import javax.swing.JMenu;
import javax.swing.JTextPane;
import javax.swing.ListSelectionModel;

import java.awt.Component;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.BoxLayout;
import javax.swing.JList;
import javax.swing.AbstractListModel;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.SystemColor;
import java.awt.Insets;
import java.util.List;

public class Window {

    private JFrame frame;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Window window = new Window();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
public Window() {
    initialize();
}

/**
 * Initialize the contents of the frame. This is where Window Builder
 * will generate its code.
 */
public void addTo(JPanel displayPanel, Component contentToAdd)
{
    displayPanel.add(contentToAdd);
}

public void initialize() {

    Dimension sidePanelSize = new Dimension(180, 540);

    frame = new JFrame();
    frame.setBounds(100, 100, 480, 540);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JMenuBar menuBar = new JMenuBar();
    frame.setJMenuBar(menuBar);

    JMenu mnFile = new JMenu("File");
    menuBar.add(mnFile);

    JMenuItem mntmLoad = new JMenuItem("Load");
    mnFile.add(mntmLoad);

    JMenuItem mntmSave = new JMenuItem("Save");
    mnFile.add(mntmSave);

    JMenuItem mntmExit = new JMenuItem("Exit");
    mnFile.add(mntmExit);

    final JPanel itemPanel = new JPanel();
    itemPanel.setPreferredSize(sidePanelSize);
    itemPanel.setBackground(Color.WHITE);
    itemPanel.setLayout(new BorderLayout());

    DefaultListModel<String> model = new DefaultListModel<>();
    for (String items : new String [] {"Case", "Motherboard", "CPU", "GPU", "PSU", "RAM", "HDD"})
        model.addElement(items);

    final JList<String> itemList = new JList<>();
    itemList.setModel(model);

    itemList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    itemList.setFont(new Font("SegoeUI", Font.BOLD, 11));
    itemPanel.add(itemList, BorderLayout.WEST);
    frame.getContentPane().add(itemPanel, BorderLayout.WEST);

    final JList<String> addToList = new JList<>(new DefaultListModel<String>());

    JPanel buttonContainer = new JPanel();
    JPanel buttonList = new JPanel();
    GridBagConstraints c = new GridBagConstraints();

    buttonList.setLayout(new BoxLayout(buttonList, BoxLayout.Y_AXIS));
    buttonContainer.setLayout(new GridBagLayout());
    buttonContainer.setBackground(new Color(238, 238, 238));

    final JPanel displayPanel = new JPanel();
    displayPanel.setPreferredSize(sidePanelSize);
    displayPanel.setBackground(Color.WHITE);
    frame.getContentPane().add(displayPanel, BorderLayout.EAST);    

             // Right here is where I do the button action. The `displayModel` is the model of the `JList` I'm updating.

        JButton addButton = new JButton(">>");
        addButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                List<String> selected = itemList.getSelectedValuesList();
                DefaultListModel<String> displayModel = (DefaultListModel<String>) addToList.getModel();

                for (String item: selected) {
                    displayModel.addElement(item);
                }
            }
        });
        buttonList.add(addButton);


        JButton deleteButton = new JButton("<<");
        deleteButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        buttonList.add(deleteButton);
        c.gridx = 0;
        c.gridy = 0;
        buttonContainer.add(buttonList, c);
        frame.getContentPane().add(buttonContainer, BorderLayout.CENTER);
    }
}

我不确定出了什么问题。我查过了文档。我猜我错过了一些简单的东西。谢谢你的帮助。

2 个答案:

答案 0 :(得分:0)

  

似乎所有事情都已通过但是没有对GUI进行更改。

你读过点&#34; 3&#34;我对可能出现问题的建议?

答案 1 :(得分:0)

@camickr,@ .FredK感谢您的帮助。这些建议帮助很大。事实证明,在某些时候,在更改某些内容时,我将JList的分配删除到该特定面板。它正在更新,但从未分配给面板。