[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

dynamically add new components to the Jpanel



Hello,

 

I want to dynamically add new components (say check boxes) to the Jpanel but the panel does not update the Jpanel to  show new component.

 

Here is sample code:

 

package alchat;

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

import javax.swing.*;

 

public class almod extends JApplet {

JButton JBReject = new JButton("Reject");

JButton JBAccept = new JButton("Accept ");

JButton JBSend = new JButton("Send");

JPanel jp = new JPanel();

Container cp;

JPanel JpButton = new JPanel();

 

public String getParameter(String key, String def) {

return isStandalone ? System.getProperty(key, def) :

(getParameter(key) != null ? getParameter(key) : def);

}

 

// Construct the applet

public almod() {

}

 

//Initialize the applet

public void init() {

 

System.out.println(" Hello , Testing for you testing for action : " ); try { uid = this.getParameter("userid", " "); } catch (Exception e) { e.printStackTrace(); } cp = getContentPane(); JPanel jp = new JPanel(); jp.setLayout( new FlowLayout() ); JBReject.addActionListener( new RejectHandler() ); JBAccept.addActionListener( new AcceptHandler() ); JBSend.addActionListener( new SendHandler() ); jp.add(JBReject); jp.add(JBAccept); jp.add(JBSend); cp.add(jp); // cp.validate();

 

try {

this.setSize(new Dimension(400,300));

}

catch(Exception e) {

e.printStackTrace();

}

}

 

public void start()

{

 

jp.invalidate();

jp.repaint();

jp.validate();

System.out.println(" Inside start ");

ci++;

for (int k=0 ;k< ci ; k++ )

{

jp.add(new JCheckBox("Applet!" + k ));

System.out.println( " count inedex: k:"+ k + " ci : " + ci );

}

}

public void stop()

{

System.out.println(" Inside stop");

}

public void destory()

{

System.out.println(" Inside destory ");

}

 

// Get Applet information

public String getAppletInfo() {

return "Applet Information, Moderator Apllet for this";

}

 

// Get parameter info

public String[][] getParameterInfo() {

String[][] pinfo =

{

{"userid", "String", ""},

};

return pinfo;

}

// static initializer for setting look & feel

static {

try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

}

catch (Exception e) {}

}

 

 

class SendHandler implements ActionListener {

public void actionPerformed(ActionEvent e){

if (e.getSource().equals(JBSend))

{

System.out.println(" ActionListener:actionPerformed () : Inside send ...");

ci++;

for (int k=0 ;k< ci ; k++ )

{

jp.add(new JCheckBox("Applet!" + k ));

System.out.println( " count inedex: k:"+ k + " ci : " + ci );

 

}

jp.revalidate();

JBAccept.setText("Here come :");

cp.validate();

}

}

}

 

}

 

So the Send button create checkbox and increase every time by one.

 

I want to create and delete check boxes on client side

on any event. But the panel does not update properly to show new components.

 

Can any body suggest me any thing.

 

Bye,

Shahzad

shahzad@shebaak.com