新 Jframe 关闭时更新 Jlist [英] Update Jlist when new Jframe closing

查看:27
本文介绍了新 Jframe 关闭时更新 Jlist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到有关我的问题的某种信息,但没有成功,也许 Stackowerflow 太大了,或者我使用了错误的关键字.

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.

无论如何,我的问题是我对从这个尖锐的世界迁移的 Java 有点陌生.我有一个开发简单服务台软件的项目.我制作了一个 JFrame 作为 Startscreen,其中包含一些按钮和一个将显示案例的 JList.开始帧上的按钮之一是创建一个新案例.该按钮将打开一个新的 Jframe,用户可以在其中输入所需的信息和一个按钮保存:将信息保存在列表中.该列表将在不同的类中处理,如下所示.

Anyway, my problem is that I'm kind a new to Java migrating from the sharp world. I've an project in develop a simple servicedesk software. I've made a JFrame as Startscreen with some buttons and a JList that will display the cases. 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. The list will be handled in a different class like below.

Start Jframe 打开 case Jframe,它将保存在列表类的列表中,关闭自身并返回 Start jframe.当用户返回到起始 Jframe 并且我希望起始 Jframe 中的 Jlist 自行刷新并显示新保存的案例时 - 我不知道该怎么做.

Start Jframe opens case Jframe that will save in a list in the listclass close itself and return to 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.

我想我要在开始 Jframe 中编写一些事件,以便在 Jframe 关闭的情况下做出响应,但我不知道如何做.

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.

这有点难以解释,但我没有上传图片的声誉.

It's kind a hard to explain, but I don't have the reputation to upload images.

推荐答案

我认为您可能需要考虑创建一个新的自定义对话框,该对话框在您选择按钮时显示.这是我随身携带的一段示例代码以供参考.这里的主要项目是显示对话框的静态方法,以及对话框是模态的,因此执行暂停"直到您关闭对话框,然后允许您捕获对话框的保存数据并从静态方法返回它显示对话框.将此用作模板并根据需要进行修改.更具体地说,响应"是从方法返回的值.实际上,响应"不会是一个简单的布尔值(我只是用它来测试逻辑),而是包含您从对话框的输入控件收集的所有信息的 listClass.对 getUserInput() 的调用是您要从主 JFrame 代码执行以开始滚动的操作.actionPerformed() 方法是您从对话框控件中获取数据并填充包含返回信息的类的地方.

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. The call to getUserInput() is what you'll want to do from your main JFrame code to start the ball rolling. The actionPerformed() method is where you'll grab the data from the dialog box controls and populate the class that contains the return info.

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

        }

 }

这篇关于新 Jframe 关闭时更新 Jlist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆