简体   繁体   English

新的Jframe关闭时更新Jlist

[英]Update Jlist when new Jframe closing

I've tried to find some kind of information about my problem with no success, maybe Stackowerflow is to big or I'm using the wrong keywords. 我试图找到有关我的问题的某种信息,但没有成功,可能是Stackowerflow太大,或者我使用了错误的关键字。

Anyway, my problem is that I'm kind a new to Java migrating from the sharp world. 无论如何,我的问题是,我是从尖端世界迁移到Java的新手。 I've an project in develop a simple servicedesk software. 我有一个项目正在开发一个简单的Servicedesk软件。 I've made a JFrame as Startscreen with some buttons and a JList that will display the cases. 我已经制作了一个JFrame作为Startscreen,其中包含一些按钮和一个JList来显示案例。 One of the buttons on the start frame is to create a new case. 起始帧上的按钮之一是创建新案例。 That button will open a new Jframe where the user can enter information needed and a button save: that will save the information in a list. 该按钮将打开一个新的Jframe,用户可以在其中输入所需的信息和一个按钮save:将信息保存在列表中。 The list will be handled in a different class like below. 该列表将在不同的类中进行处理,如下所示。

Start Jframe opens case Jframe that will save in a list in the listclass close itself and return to Start jframe. 启动Jframe将打开将保存在listclass列表中的Jcase案例,并关闭自身并返回到Start jframe。 When the user returns to the start Jfram and I want the Jlist in the start Jframe to refresh itself and display the new saved case - which I dont know how to do. 当用户返回到开始Jfram时,我希望开始Jframe中的Jlist刷新并显示新的已保存的大小写-我不知道该怎么做。

I guess I to do some event writing in the start Jframe that has to respond when the case Jframe is closing but I dont know how. 我想我要在启动Jframe中写一些事件,当Jframe关闭时,它必须响应,但是我不知道如何。

It's kind a hard to explain, but I don't have the reputation to upload images. 这很难解释,但是我没有上载图像的声誉。

I think you might want to consider creating a new custom dialog box that displays when you select the button. 我认为您可能要考虑创建一个新的自定义对话框,该对话框在您选择按钮时显示。 Here's a sample piece of code I keep handy for reference. 这是我方便参考的示例代码。 The main items here are the static method that displays the dialog, and the fact that the dialog is modal, so execution "pauses" until you close the dialog, allowing you to then capture the dialog's saved data and return it from the static method that displays the dialog. 这里的主要项目是显示对话框的静态方法,并且该对话框是模式对话框,因此执行会“暂停”直到关闭对话框,然后您才能捕获对话框的已保存数据并从静态方法返回显示对话框。 Use this as a template and modify as needed. 将此用作模板并根据需要进行修改。 More specifically, "response" is the value returned from the method. 更具体地说,“响应”是从方法返回的值。 In reality, "response" will not be a simple boolean, ( I just used that to test the logic), but your listClass that contains all the information you collected from the dialog's input controls. 实际上,“响应”不是一个简单的布尔值(我只是用它来测试逻辑),而是包含从对话框的输入控件中收集的所有信息的listClass。 The call to getUserInput() is what you'll want to do from your main JFrame code to start the ball rolling. 您希望从主JFrame代码开始对getUserInput()进行调用,以开始进行滚动。 The actionPerformed() method is where you'll grab the data from the dialog box controls and populate the class that contains the return info. 在actionPerformed()方法中,您将从对话框控件中获取数据,并填充包含返回信息的类。

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class ConfirmDialog extends JDialog implements ActionListener
{   
        private boolean response = false;
        private JButton btnOK = new JButton("OK");
        private JButton btnCancel = new JButton("Cancel");
        private JPanel contentPane = new JPanel();

        public static boolean getUserInput()
        {
                    return new ConfirmDialog().showDialog();

        }

        private boolean showDialog()
        {
                    setVisible(true);
                    //next line executes only after dialog is disposed,                 since dialog is modal.
                    return response;
        }

        private ConfirmDialog()
        {
                         setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                    btnOK.setActionCommand("OK");
                    btnCancel.setActionCommand("Cancel");
                    btnOK.addActionListener(this);
                    btnCancel.addActionListener(this);
                    contentPane.add(btnOK);
                    contentPane.add(btnCancel);
                    setContentPane(contentPane);
                    setModal(true);
                    pack();

        }
        public void actionPerformed(ActionEvent e)
        {
                    if(e.getActionCommand().equals(btnOK.getActionCommand()))
                    {
                                response = true;
                    }
                    setVisible(false);
                    dispose();
        }

        /**
         * @param args
         */
        public static void main(String[] args) {
                    // TODO Auto-generated method stub
                    System.out.println(getUserInput());

        }

 }

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

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