I have this "main" panel (let's call it AAA) with BorderLayout, and two panels (BBB and CCC) in it:
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 CCC is currently empty, with GridLayout.
My panel BBB 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 CCC 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.
My question: How should I implement the action listener in BBB? I have no idea what number will be typed in by the user. Therefore, I don't know the precise number of rows in CCC's GridLayout (I just know it should have 5 columns). Can I modify its layout from the listener in BBB? And how can I add components to the panel CCC from the listener in the panel BBB?
Of course, if you have better solution (without two separate panels), let me know :)