简体   繁体   中英

ActionListener Anonymous class between two JPanel

I'm trying to get to panels to work with each other RECURSIVELY.

When I'm on the main GUI I have the first JPanel with a Button Add Client, Once clicked it brings me to the JPanel with a Form and then I recuperate those values, and send them away in a JTable in the first JPanel the Main GUI.

When I then try to insert a second record. I get a blank GUI. I'm not too sure what I am doing wrong. How can I implement multiple time the same action to repeat ? Which is Load up the form, enter the information, push it on the Table, and the process repeats as much as I need it.

This is the Add Client button declaration in the MAIN GUI

Button btn_AddClient = new Button("Add Client");
        btn_AddClient.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                panel.setVisible(false);
                contentPane.setVisible(false);
                setContentPane(contentPaneClient);
            }
        });
        btn_AddClient.setBounds(259, 12, 70, 22);
        contentPane.add(btn_AddClient); 

This is the Add Button of the Form in the second Panel

JButton btnAdd = new JButton("Add");
        btnAdd.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {


                setContentPane(contentPaneClient);
                panel.setVisible(true);
                contentPane.setVisible(true);
                contentPaneClient.setVisible(false);
                LigneJTab l = new LigneJTab(textFieldPrenomClient.getText(),textFieldNomClient.getText(), textFieldAdresseClient.getText(), chckbxHomme.isSelected(), Sport.FOOTBALL);
                myModel.addLine(l);
                setContentPane(contentPane);
            }
        });
        btnAdd.setBounds(263, 40, 117, 29);
        contentPaneClient.add(btnAdd);
JButton btnAdd = new JButton("Add");
    btnAdd.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {

            //setContentPane(contentPaneClient);
            //panel.setVisible(true);
            //contentPane.setVisible(true);
            //contentPaneClient.setVisible(false);

            LigneJTab l = new LigneJTab(textFieldPrenomClient.getText(),textFieldNomClient.getText(), textFieldAdresseClient.getText(), chckbxHomme.isSelected(), Sport.FOOTBALL);
            myModel.addLine(l);

            panel.setVisible(true);
            contentPane.setVisible(true);
            setContentPane(contentPane);

        }
    });
    btnAdd.setBounds(263, 40, 117, 29);
    contentPaneClient.add(btnAdd);

Commented the top part and added the setContentPane(contentPane); and that worked !

Thanks !

Another idea: You don't need to swap out the content panes to ask for data. A much more elegant way is using a modal dialog box. To make one, first create a dialog class:

public class MyDialog extends JDialog {
    public MyDialog(Frame parent) {
        super(parent);
        setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
        // add components to getContentPane()
        // to close dialog, use setVisible(false) in listeners
    }

    public OutputData getData() {
        OutputData data = new OutputData();
        show();
        // show only returns after a setVisible(false)
        data.field = textField.getText();
        // for example, repeat as many times as necessary
        return data;
    }
}

To invoke this dialog from the JFrame , use the following code:

MyDialog dialog = new MyDialog(this);
OutputData data = dialog.getData()
// now retrieve fields from data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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