简体   繁体   English

从另一个JFrame刷新Jlist

[英]Refresh a Jlist from another JFrame

i want to refresh a JList when i push a button in another JFrame. 我想在另一个JFrame中按下按钮时刷新JList。

So i have a JFrame GuiBoss that manages employees (add,delete,update).When i press the button add, another Jframe opens, in wich i create a new employee. 所以我有一个管理员工的JFrame GuiBoss(添加,删除,更新)。当我按下按钮添加时,另一个Jframe打开,我创建了一个新员工。

//Open the "add_form" where i give details about a new employee. //打开“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);

}

//refresh the list with the new employee added. //刷新列表并添加新员工。

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.

}

My problem is that when i submit the details about the new employee i call the function refresh(Employee e) from the GuiBoss frame , the message ("I reach this point!") shows up on the console, the size of the listModel changes, but the list it doesen't refresh. 我的问题是,当我提交有关新员工的详细信息时,我从GuiBoss框架调用函数refresh(Employee e),在控制台上显示消息(“我到达这一点!”),listModel的大小发生变化,但它不会刷新的列表。 Also i must say that i set the model properly for the list. 另外我必须说我为列表正确设置了模型。

//take data from form and call refresh(Employee e) from the main frame("GuiBoss") //从表单中获取数据并从主框架调用刷新(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
    }  
}

Please let me know if u have any ideeas.Thanks. 如果你有任何想法,请告诉我。谢谢。

Instead of popping up another frame, why not use a modal JDialog to collect the information about the new employee. 为什么不使用模态JDialog来收集有关新员工的信息,而不是弹出另一个框架。 When the dialog is closed, you can then extract the details from the dialog and refresh the list from within the current frame. 关闭对话框后,您可以从对话框中提取详细信息并从当前帧中刷新列表。

This prevents the need to expose portions of your API unnecessarily. 这可以防止不必要地暴露部分API。

Check out How to use Dialogs for details. 有关详细信息,请查看如何使用对话框

Updated 更新

Assuming you've set the model correctly, then your code should work...as per this example... 假设您已正确设置模型,那么您的代码应该可以工作......根据此示例...

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

    }

}

That would suggest that there is something else wrong that you're not showing us... 这表明还有其他错误,你没有向我们展示......

Updated with possible fix for reference issues 更新了参考问题的可能修复

This basically demonstrates passing a reference of the main panel to a sub factory that is responsible for actually adding the value back into the main panel. 这基本上演示了将主面板的引用传递给子工厂,该工厂负责将值实际添加回主面板。 Normally I'd use a interface of some kind instead of exposing the entire panel to simply provide access to a single method, but this was a quick example. 通常我会使用某种interface而不是暴露整个面板来简单地提供对单个方法的访问,但这是一个快速的例子。

It uses both a normal implements and inner class as ActionListener to demonstrate the two most common means for passing a reference of "self" to another class. 它使用普通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