*
The moose likes JSF and the fly likes Adding components dynamically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Adding components dynamically" Watch "Adding components dynamically" New topic
Author

Adding components dynamically

surya sudhakar
Greenhorn

Joined: Jun 11, 2002
Posts: 18
Hi All,
My application demands addition of UI components dynamically at runtime.
I heard that it is possible to add the components at runtime.
Can anybody please tell me how can I achieve this?

Thanks in advance.
Josh Juneau
Ranch Hand

Joined: Jun 16, 2004
Posts: 86
I am not sure what you mean by dynamically adding components at runtime. It is certainly possible to "hide" or "unhide" components at runtime via the "rendered" attribute. This attribute can be bound to a backing-bean boolean property.

For instance, you can have a backing-bean method which performs some kind of calculation, and based upon the outcome it can set your property to true or false...which would then "render or unrender" the component onscreen.

Josh Juneau


Database Administrator/Application Developer
Varun Khanna
Ranch Hand

Joined: May 30, 2002
Posts: 1400
The entire component tree is present on the server and the component tree can be manipulated, you can create new components as children of other components.


- Varun
Venkateswaran VelRajan
Greenhorn

Joined: Feb 22, 2005
Posts: 3
In my application I have added RadioButton at Run time into a panel.
I have tried to get the MetaInformation of my DataBase with tables
Papers With Fields ... PaperId,PaperName
Presentation With Fields ... PresId,PresName
Rules With Fields ... RuleId,Rule

The RadioButtons will be listed depends upon the number of fields in the table selected from the ComboBox.
I hope the code will helps u.

My Code follows...

import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.FlowLayout;
import java.awt.Dimension;

import javax.swing.ButtonGroup;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JComboBox;
//import javax.swing.JCheckBox;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;

import java.sql.DatabaseMetaData;
import java.sql.ResultSetMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;

public class TableNameDetails extends JFrame implements ActionListener,ItemListener
{
JPanel SelectionPanel;
JComboBox TableNames_Cmb;
JRadioButton []Radios;
ButtonGroup RadioGroup;
JScrollPane ScrollPanel;
//JCheckBox []Checks;
JLabel []Lbls;
JLabel Lbl_Cmb;
JButton Ok_But;
Connection Conn;
Statement Stmt;
boolean No_More_Table;

TableNameDetails()
{
super("Getting Additional Table info");

if(init_DB())
{
System.out.println("DB Connected Successfully");
}
else
{
System.out.println(" Error in Connecting DB ");
}
Lbl_Cmb=new JLabel("Select the table to add");

TableNames_Cmb=new JComboBox();
build_Cmb();
TableNames_Cmb.addItemListener(this);

SelectionPanel=new JPanel();//new FlowLayout());
Radios=null;

Ok_But=new JButton("OK");
Ok_But.addActionListener(this);

getContentPane().setLayout(new FlowLayout());
getContentPane().add(Lbl_Cmb);
getContentPane().add(TableNames_Cmb);

ScrollPanel=new JScrollPane(SelectionPanel);
ScrollPanel.setPreferredSize(new Dimension(200,200));
getContentPane().add(ScrollPanel);
getContentPane().add(Ok_But);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setSize(225,310);
setVisible(true);

}
public void actionPerformed(ActionEvent ae)
{
//System.out.println(" Selected Information "+ RadioGroup.getSelection().getSelectedObjects());
if(RadioGroup==null)
System.out.println("RadioGroup NULL");
if(RadioGroup.getSelection()==null)
System.out.println("RadioGroup Model NULL");
if(RadioGroup.getSelection().getActionCommand()==null)
System.out.println("Action Command NULL");

System.out.println(" Selected Information "+ RadioGroup.getSelection().getActionCommand());

}
// handle JComboBox event
public void itemStateChanged( ItemEvent event )
{
// determine whether check box selected
if ( event.getStateChange() == ItemEvent.SELECTED )
{
/*
To avoid the selection of chkBox & Radio Button with same Index
*/
//Object Source=ie.getItem();
//System.out.println(" Bool result :"+(Source instanceof JComboBox));
System.out.println(" Selected table : "+TableNames_Cmb.getSelectedItem());
constructSelectionList();
}
}
private boolean init_DB()
{
boolean result=true;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(java.lang.ClassNotFoundException e)
{
System.err.print("ClassNotFoundException: Can\'t load DB Driver file");
System.err.println(e.getMessage());
result=false;
}
try
{
Conn = DriverManager.getConnection("jdbc dbc:km");
Stmt = Conn.createStatement();
}
catch (SQLException e)
{
System.out.println("Exception raised while loading DB init \n :");
e.printStackTrace();
result=false;
}
return result;
}
private void build_Cmb()
{
DatabaseMetaData dbmd = null;
ResultSet allTables;
String table_name;
String[] tableTypes = { "TABLE" };
try
{
dbmd = Conn.getMetaData();
allTables = dbmd.getTables(null,null,null,tableTypes);

/*getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types) */

while(allTables.next())
{
table_name = allTables.getString("TABLE_NAME");
TableNames_Cmb.addItem( table_name );
}
if(TableNames_Cmb.getItemCount()<1)
{
TableNames_Cmb.addItem("All tables Selected");
No_More_Table=true;
}
else
No_More_Table=false;
}
catch (SQLException e)
{
System.out.println("Exception raised while Getting Meta Info \n :");
e.printStackTrace();
No_More_Table=false;
}
}
private void constructSelectionList()
{
/*
Get the number of fields and allocate the selection boxes
Create the Required selection Boxes
Put them all in the SelectionPanel
*/

ResultSetMetaData rsmd=null;
ResultSet currTableRS;
String table_name;
int ColumnCount=0;
String fields[]=null;

if(No_More_Table)
return;
table_name=(String)TableNames_Cmb.getSelectedItem();


try
{
currTableRS = Stmt.executeQuery("SELECT * FROM "+ table_name);

rsmd = currTableRS.getMetaData();
ColumnCount = rsmd.getColumnCount();
fields=new String[ColumnCount];

for(int Col = 1; Col <= ColumnCount; Col++)
{
fields[Col-1]=rsmd.getColumnLabel(Col);
System.out.println("Field "+Col+" : "+fields[Col-1]);
}
}
catch (SQLException e)
{
System.out.println("Exception raised while Getting Meta Info \n :");
e.printStackTrace();
return;
}

/*
Creating the RadioButtons from the fields[] & put them all in ButtonGroup & SelectionPanel
*/
SelectionPanel.removeAll();
RadioGroup=new ButtonGroup();
Radios=new JRadioButton[ColumnCount];

if(RadioGroup==null)
{
System.out.println("RadioGroup Still Null");
}
for(int Col = 0; Col < ColumnCount; Col++)
{
Radios[Col]=new JRadioButton(fields[Col]);
Radios[Col].setActionCommand(fields[Col]);
RadioGroup.add(Radios[Col]);
if(Col == 0)
Radios[Col].setSelected(true);

System.out.println(" Radio Button "+(Col+1)+" Constucted");
}
for(int Col = 0; Col < ColumnCount; Col++)
{
SelectionPanel.add(Radios[Col]);
}
SelectionPanel.updateUI();
//houseKeepCode();

}
public static void main(String arg[])
{
new TableNameDetails();
}
}
Gary VanMatre
Greenhorn

Joined: Sep 26, 2003
Posts: 6
Originally posted by surya sudhakar:
Hi All,
My application demands addition of UI components dynamically at runtime.
I heard that it is possible to add the components at runtime.
Can anybody please tell me how can I achieve this?

Thanks in advance.


You might take a look at the example code posted for a Shale enhancement. It would provide an example of what you are describing.

[Shale] Enhancement to the Shale subview tag library component.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding components dynamically
 
Similar Threads
JInternal frame not getting updated
Netbeans 5 : add component using addLayoutComponent() problem
resizing a bean and its components dynamically
printing the string
binding and el expression in datatable