设置为新的DefaultListModel时,JList不会更新

时间:2013-08-07 06:58:11

标签: java swing jlist defaultlistmodel

public class MainMenu extends JFrame {
    private JPanel panel,file_list_panel;
    private JPanel contentPane;
    private JMenuBar menuBar;
    private JMenu mnNewMenu1,mnNewMenu2,mnNewMenu3;
    private JMenuItem mt1,mt2,mt3;
    private JPanel right,left ,bottom;
    private JSplitPane spver ,sphor;
    private JTabbedPane tabbedPane;
    private JLabel label;
    private JList<String> list_1;
    private JScrollPane jscroll_list;
    private DefaultListModel listmodel_1 = new DefaultListModel();


    public MainMenu() {




    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    setBounds(0,0,screenSize.width, screenSize.height);
    setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
    int intValue = Integer.parseInt( "EEEEEE",16);
    Color aColor = new Color( intValue );
    UIManager.put("TabbedPane.background", new Color(230, 216, 174));
    UIManager.put("TabbedPane.selected", Color.WHITE); 
    UIManager.put("TabbedPane.contentAreaColor",aColor ); 
    UIManager.put("TabbedPane.focus", Color.WHITE); 

    setTitle("Main Menu");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //setBounds(100, 100, 848, 680);
    //setLocation(10, 10);
    setLocationRelativeTo(null); // set jFrame alignment to center

    //parent(first) panel 
    contentPane = new JPanel();
    contentPane.setBackground(Color.BLACK);
    contentPane.setBorder(new EmptyBorder(2, 2, 2, 2));
    setContentPane(contentPane);
    contentPane.setLayout(new BorderLayout(0, 0));

    //Main Menu BAR
    menuBar = new JMenuBar();
    menuBar.setFont(new Font("Tekton Pro Ext", Font.PLAIN, 11));
    setJMenuBar(menuBar);

    //Menu 1
    mnNewMenu1 = new JMenu("New menu");
    menuBar.add(mnNewMenu1);
    //Menu 1 MenuItem 1

    mt1 = new JMenuItem("Browse New Project");
    mt1.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
        BrowseScreen frame = new BrowseScreen();
        frame.setVisible(true);
        }


    });


    mnNewMenu1.add(mt1);




    //second panel
    panel = new JPanel();
    contentPane.add(panel, BorderLayout.CENTER);
    panel.setLayout(new BorderLayout(0, 0));

    spver =new  JSplitPane(JSplitPane.VERTICAL_SPLIT);
    spver.setDividerLocation(500);
    spver.setEnabled(false);

    bottom=new JPanel();
    sphor =new  JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    sphor.setEnabled(false);
    spver.setTopComponent(sphor);
    spver.setBottomComponent(bottom);
    bottom.setLayout(null);
    panel.add(spver);

    sphor.setDividerLocation(180);
    left =new JPanel();
    right =new JPanel();
    sphor.setRightComponent(right);
    right.setLayout(new GridLayout(0, 1, 0, 0));

    tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

    JPanel tab1 = new JPanel();
    tabbedPane.addTab("fffa", tab1);
    tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

    JPanel tab2 = new JPanel();
    tabbedPane.addTab("TAB1", tab2);
    tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);



    right.add(tabbedPane);
    sphor.setLeftComponent(left);
    left.setLayout(new BorderLayout(0, 0));

    file_list_panel= new JPanel();
    file_list_panel.setBackground(Color.BLACK);
    file_list_panel.setForeground(Color.WHITE);

    label = new JLabel("Java Files of Project");
    label.setBackground(Color.BLACK);
    label.setForeground(Color.WHITE);
    label.setFont(new Font("Garamond", Font.BOLD, 14));
    file_list_panel.add(label);
    left.add(file_list_panel, BorderLayout.NORTH);


    list_1 = new JList<String>(listmodel_1);



    jscroll_list = new JScrollPane(list_1);
    left.add(jscroll_list, BorderLayout.CENTER);


    }
    public  void setList(Vector<String> files){
        listmodel_1.removeAllElements();
        list_1.removeAll();
        for(int i=0;i<files.size();i++)
            listmodel_1. addElement(files.elementAt(i));
        list_1.setModel(listmodel_1);
        this.invalidate();
        this.validate();
        this.repaint();
    }

}

在调用setVisible(false)之前从浏览窗口调用setList; 即。当浏览窗口消失时调用此方法..它在方法中执行所有操作但不在MainMenu中更新它     public void setFileList(){             MainMenu mm = new MainMenu();             mm.setList(java_files);         }

list_1 -JList listmodel_1 = DefaultListModel

我尝试刷新帧但在将新列表添加到主窗口后不刷新...

更新JList之前我浏览另一个窗口中的文件然后将其设置为setVisible(false),然后MainMenu获得焦点并调用setList方法但不改变

2 个答案:

答案 0 :(得分:5)

您正在创建一个新的JList<String>并复制对您的实例变量的引用 - 但您既不从框架中删除旧的JList,也不会添加新的JList<String>。只是更改变量不会那样做。

为什么不替换现有的模型,而不是创建新的list_1=new JList<String>(listmodel_1); ?删除这一行:

{{1}}

你可能会觉得它很有效。 (您已经在后续行中设置了模型......)

答案 1 :(得分:4)

基本上,您正在创建新的JList并将模型与其关联,但这对屏幕上的JList没有任何影响

public  void setList(Vector<String> files){
    // Good...
    listmodel_1.removeAllElements();
    // Not required, has nothing to do with the items in the list
    //list_1.removeAll();
    // Good
    for(int i=0;i<files.size();i++)
        listmodel_1. addElement(files.elementAt(i));
    // This here is your problem
    //list_1=new JList<String>(listmodel_1);
    // Because I have no idea what model list_1 is actually using...
    list_1.setModel(listmodel_1);
    //list_1.setSelectedIndex(0);
    //this.invalidate();
    //this.validate();
    //this.repaint();
}
相关问题