Update Jlist when new Jframe closing

元气小坏坏 提交于 2019-12-12 05:49:16

问题


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.

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 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.

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.


回答1:


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

        }

 }


来源:https://stackoverflow.com/questions/30121667/update-jlist-when-new-jframe-closing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!