繁体   English   中英

从另一个JFrame刷新Jlist

[英]Refresh a Jlist from another JFrame

我想在另一个JFrame中按下按钮时刷新JList。

所以我有一个管理员工的JFrame GuiBoss(添加,删除,更新)。当我按下按钮添加时,另一个Jframe打开,我创建了一个新员工。

//打开“add_form”,我提供有关新员工的详细信息。

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {                                       
    GuiBoss gb = new GuiBoss(contrb,boss);
    Add_form af = new Add_form(gb,contrb,boss);
    af.setVisible(true);

}

//刷新列表并添加新员工。

public void refresh(Employee e){
    System.out.println("I reach this point!");
    //if i print e.getName() it works, printing the right name that i give in the    "add_form"
    listModel.addElement(e);
    //listModel.clear(); //don't work either.

}

我的问题是,当我提交有关新员工的详细信息时,我从GuiBoss框架调用函数refresh(Employee e),在控制台上显示消息(“我到达这一点!”),listModel的大小发生变化,但它不会刷新的列表。 另外我必须说我为列表正确设置了模型。

//从表单中获取数据并从主框架调用刷新(Employee e)(“GuiBoss”)

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {                                       
    //String Id = txtID.getText();
    String UserName = txtName.getText();
    txtHour.setVisible(false);
    boolean b = false;
    if(rbtnYes.isSelected() == true){
        b = true;
    }
    if(rbtnNo.isSelected() == true){
        b = false;
    }
    if(rbtnYes.isSelected()==false && rbtnNo.isSelected() == false){
        System.out.println("Select the presence!");
    }
    else{
        txtOra.setVisible(true);
        String Hour = txtHour.getText();
        e = new Employee(UserName,b,Hour,boss);  //boss i get from main frame when i start this add new employee form
        contrb.addEmployee(e);
        gb.refresh(e);  //gb is of type GuiBoss were i have the function that does      
        // the refresh
    }  
}

如果你有任何想法,请告诉我。谢谢。

为什么不使用模态JDialog来收集有关新员工的信息,而不是弹出另一个框架。 关闭对话框后,您可以从对话框中提取详细信息并从当前帧中刷新列表。

这可以防止不必要地暴露部分API。

有关详细信息,请查看如何使用对话框

更新

假设您已正确设置模型,那么您的代码应该可以工作......根据此示例...

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestList03 {

    public static void main(String[] args) {
        new TestList03();
    }

    public TestList03() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private DefaultListModel model;

        public TestPane() {
            setLayout(new BorderLayout());
            model = new DefaultListModel();
            JList list = new JList(model);
            add(new JScrollPane(list));
            JButton btn = new JButton("Add");
            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    model.addElement("New Element");
                }
            });
            add(btn, BorderLayout.SOUTH);
        }

    }

}

这表明还有其他错误,你没有向我们展示......

更新了参考问题的可能修复

这基本上演示了将主面板的引用传递给子工厂,该工厂负责将值实际添加回主面板。 通常我会使用某种interface而不是暴露整个面板来简单地提供对单个方法的访问,但这是一个快速的例子。

它使用普通implements和内部类作为ActionListener来演示将“self”的引用传递给另一个类的两种最常用的方法。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestList03 {

    public static void main(String[] args) {
        new TestList03();
    }

    public TestList03() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel implements ActionListener {

        private DefaultListModel model;

        public TestPane() {
            setLayout(new BorderLayout());
            model = new DefaultListModel();
            JList list = new JList(model);
            add(new JScrollPane(list));

            JPanel buttons = new JPanel(new FlowLayout(FlowLayout.CENTER));

            JButton btn1 = new JButton("Add 1");
            btn1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    new Factory(TestPane.this, "Added by Button 1");
                }
            });
            buttons.add(btn1);

            JButton btn2 = new JButton("Add 2");
            btn2.addActionListener(this);
            buttons.add(btn2);

            add(buttons, BorderLayout.SOUTH);
        }

        public void addItem(String text) {
            model.addElement(text);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            new Factory(TestPane.this, "Added by Button 2");
        }
    }

    public class Factory {

        public Factory(TestPane testPane, String text) {
            testPane.addItem(text);
        }
    }
}

暂无
暂无

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

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