add child in parent constructor [message #897331] |
Mon, 23 July 2012 15:41  |
Eclipse User |
|
|
|
Hi,
I have a problem, I want to add child in parent constructor but it don't work!
For example (in swing),
In my main I have, MyPanel panel = new MyPanel();
And MyPanel contains some composite like,
public MyPanel() {
textField = new JTextField();
add(textField);
textField.setColumns(10);
}
If child is not in constructor (so just after line, MyPanel panel = new MyPanel(); for example) it works but not if I put child in my contructor!
Thanks to your help
|
|
|
|
Re: add child in parent constructor [message #897447 is a reply to message #897331] |
Tue, 24 July 2012 07:48   |
Eclipse User |
|
|
|
For example, if I use wizard "Application Window" in Swing designer:
I have the code:
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(21, 11, 373, 226);
frame.getContentPane().add(panel);
panel.setLayout(null);
JButton btnNewButton = new JButton("New button");
btnNewButton.setBounds(124, 107, 89, 23);
panel.add(btnNewButton);
}
But if I want to customize my panel and I add child in constructor like:
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
MyPanel panel = new MyPanel();
panel.setBounds(10, 11, 270, 194);
frame.getContentPane().add(panel);
panel.setLayout(null);
}
And MyPanel constructor (who extends JPanel):
public MyPanel(){
super();
JButton btnNewButton = new JButton("New button");
btnNewButton.setBounds(89, 60, 89, 23);
add(btnNewButton);
}
In designer my button does not appear on tree and I can not select it!
Thanks to your answer
|
|
|
|
Re: add child in parent constructor [message #898642 is a reply to message #897447] |
Fri, 27 July 2012 01:35   |
Eclipse User |
|
|
|
user Kyle wrote on Tue, 24 July 2012 03:48In designer my button does not appear on tree and I can not select it!
Everything is working as expected. That button should not appear in the tree of the App Window as it is a component private to MyPanel (doing so would require breaking encapsulation which is not allowed). It should only show up in the tree of MyPanel.
|
|
|
|
Powered by
FUDForum. Page generated in 0.07208 seconds