簡體   English   中英

設置為新的DefaultListModel時JList不更新

[英]JList not updating when set to new 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方法,但它不會改變

您正在創建一個新的JList<String>並將引用復制到您的實例變量-但是您既沒有從框架中刪除舊的JList ,也沒有添加新的JList 只是更改變量不會這樣做。

而不是創建一個新的JList<String> ,為什么不替換現有模型呢? 刪除此行:

list_1=new JList<String>(listmodel_1);

您可能會發現它是可行的。 (您已經在下一行中設置了模型...)

基本上,您正在創建一個新的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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM