Discussion Locked

Runtime

0 helpful
1 reply
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

i am preparing one application where i want to add jpanel to contentpane on button click.(that is actionperformed event) can you please anwser this query or solve it.

thanks
Ashish

1 reply

by okujava

here it is:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class PanTest extends JFrame{
public PanTest(){
setBounds(200,200,200,300);
setDefaultCloseOperation(3);
getContentPane().setLayout(new FlowLayout());
JButton button = new JButton("Click me!");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(100,100));
panel.setBackground(Color.red);
getContentPane().add(panel);
validate();
}
});
getContentPane().add(button);
show();
}
public static void main(String args[]){
new PanTest();
}
}

hope it helps,

cheers

s.okujava

This discussion is closed to new replies.