简体   繁体   中英

Communication between two JPanels

I have this "main" panel (let's call it ) with BorderLayout, and two panels ( and ) in it: )与BorderLayout,以及两个面板( ):

public class AAA extends JPanel {
    BBB pnlNorth = new BBB();
    CCC pnlCenter = new CCC();
    public AAA(){
        setLayout(new BorderLayout());
        add(pnlNorth,BorderLayout.NORTH);
        add(pnlCenter,BorderLayout.CENTER);        
    }
}

Panel is currently empty, with GridLayout. 目前为空,带有GridLayout。

My panel looks like this: 看起来像这样:

public class BBB extends JPanel {
    public BBB (){
        JLabel labNum = new JLabel("Number of items: ");
        JTextField txtNum = new JTextField();
        JButton cmdOK = new JButton("OK");
        txtNum.setColumns(5);
        cmdOK.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                /* ???????????? */
            }
        });
        add(labNum);
        add(txtNum);
        add(cmdOK);        
    }
}

When a user enters a number in txtNum and presses "OK", panel should be populated with appropriate number of rows for data input. 应填充适当数量的行以进行数据输入。 Each row should contain two text fields, two drop-down lists and a checkbox. It would be nice if all the items would be in a JScrollPane, if the user enters some large number.

: How should I implement the action listener in ? :我应该如何在实现动作监听器? I have no idea what number will be typed in by the user. Therefore, I don't know the precise number of rows in 's GridLayout (I just know it should have 5 columns). 的GridLayout中的确切行数(我只知道它应该有5列)。 Can I modify its layout from the listener in ? 的侦听器修改其布局吗? And how can I add components to the panel from the listener in the panel ? 的监听器向面板添加组件?

Of course, if you have better solution (without two separate panels), let me know :)

You may be thinking about this wrong. It's perhaps better to think not of two JPanel's that are communicating, but rather more simply two objects that are communicating, and they will communicate just the same as any other two objects -- via methods that affect state. This information can be pushed from one object to the other by having the one object call the methods of the other and publish its information to it, or it can be pulled from one object to another by using the observer design pattern such as can be achieved with one of the various listeners that are available. Myself, I like using a PropertyChangeListener for this. So the observed object will accept listeners that are notified once its state has been changed, and these observers will then call public methods of the observed to extract the changed information.

For example, please check out the code in this answer or perhaps even better the answer to this question .

I managed to do this. I simply passed the Center panel as a parameter in the North panel's constructor. It works perfectly. Thank you all for the answers :)

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