4 Replies Latest reply on Mar 25, 2014 11:01 PM by TPD-Opitz

    At runtime add components in JPanel based on the xml file.


      At runtime,I want to add component in JPanel one by one,based on the xml file.

      For eg:


      <param comp=textfield>

      <param comp=textfield>

      <param comp=checkbox>

      <param comp=combobox>



      at runtime I have created the objects for the components and I showed those components in UI also.


      In order to save these components values (Ex: Text field value like user input) in Model by using get() and set() method I am facing problem to create the required no. of variables in the model. 


      Thanks in advance.

        • 1. Re: At runtime add components in JPanel based on the xml file.

          Looks like you you're inventing JavaFX a second time..

          What is the usecase of this requirement?

          Why don't you use JavaFX instead?




          • 2. Re: At runtime add components in JPanel based on the xml file.

            Or staying in the context of Swing, if you type "swing xml" into Google there are already numerous solutions available.

            • 3. Re: At runtime add components in JPanel based on the xml file.

              In my application, the XML file(Xml1) contains only the details about the predefined set of Component and its value. I am displaying those components in UI.


              If the user, add or remove the component and its value in the xml file (Xml1), it will get updated in the UI. Then i need to update those values in Model and i need to write the values in another xml file (Xml2).


              Whenever the user add or remove the components from the xml file (ie. Xml1), I am facing problem in updating the values in Model and in writing xml file (Xml2).

              • 4. Re: At runtime add components in JPanel based on the xml file.

                It feel as if there is something wrong i your design.


                The XML should either contain layout information or data, not both.


                So lets pretend that "comp=checkbox" is more a data type definition than a component request. In this case you example XML simply holds a list of rows that could be displayed in a JTable/JList. Unfortunately you have to write your own Table/List CellRenderer to show different controls in different rows.


                But the base line is:

                • read the XML using a XML frame work into a list of custom Objects
                • create a TableModel/ListModel from that List
                • Create a CellRenderer knowing how to act on the values in Type column.


                [EDIT] here is something to start with (the table is not editable since I did not enable it in the TableModel, a TableCellRenderer needs to be implemented for editing):

                import java.awt.Component;
                import java.text.MessageFormat;
                import java.util.Arrays;
                import java.util.List;
                import java.util.Random;
                import java.util.Vector;
                import javax.swing.JCheckBox;
                import javax.swing.JComboBox;
                import javax.swing.JOptionPane;
                import javax.swing.JScrollPane;
                import javax.swing.JTable;
                import javax.swing.JTextField;
                import javax.swing.table.DefaultTableCellRenderer;
                import javax.swing.table.DefaultTableModel;
                import javax.swing.table.TableModel;
                public class TableRendererTest {
                    private static final List<String> COMP_TYPES = Arrays.asList("textfield", "combobox", "checkbox");
                    private static Random random = new Random();
                    static class FakedDataFromXml {
                        private final List<String> content;
                        private final String comp;
                        private int selectedIndex = 0;
                        public FakedDataFromXml() {
                            comp = COMP_TYPES.get(random.nextInt(COMP_TYPES.size()));
                            if ("combobox".equals(comp)) {
                                int i = 0;
                                content =
                                        Arrays.asList(MessageFormat.format("{0}Choice{1}", comp, i++),
                                                MessageFormat.format("{0}Choice{1}", comp, i++),
                                                MessageFormat.format("{0}Choice{1}", comp, i++),
                                                MessageFormat.format("{0}Choice{1}", comp, i++));
                                selectedIndex = random.nextInt(content.size());
                            } else if ("checkbox".equals(comp)) {
                                content = Arrays.asList(String.valueOf(random.nextBoolean()));
                            } else {
                                content = Arrays.asList(MessageFormat.format("{0}Value", comp));
                        public String getDisplayContent() {
                            return content.get(selectedIndex);
                        public String[] getComboContent() {
                            return content.toArray(new String[0]);
                        public String getComp() {
                            return comp;
                    public static void main(String[] args) {
                        Vector<Vector<FakedDataFromXml>> dataList = new Vector<>();
                        for (int i = 0; i < 20; i++) {
                            dataList.add(new Vector<>(Arrays.asList(new FakedDataFromXml())));
                        TableModel tableModel = new DefaultTableModel(dataList,
                                new Vector<>(Arrays.asList("Header"))) {
                            public Class<?> getColumnClass(int columnIndex) {
                                return FakedDataFromXml.class;
                        JTable jTable = new JTable(tableModel);
                        jTable.setDefaultRenderer(FakedDataFromXml.class, new DefaultTableCellRenderer() {
                            public Component getTableCellRendererComponent(JTable table,
                                    Object value,
                                    boolean isSelected,
                                    boolean hasFocus,
                                    int row,
                                    int column) {
                                Component defaultTableCellRendererComponent =
                                        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                                Component tableCellRendererComponent;
                                switch (((FakedDataFromXml) value).getComp()) {
                                    case "textfield":
                                        tableCellRendererComponent = new JTextField(((FakedDataFromXml) value).getDisplayContent());
                                    case "checkbox":
                                        tableCellRendererComponent = new JCheckBox();
                                        ((JCheckBox) tableCellRendererComponent).setSelected(Boolean.valueOf(((FakedDataFromXml) value).getDisplayContent()));
                                    case "combobox":
                                        tableCellRendererComponent = new JComboBox<>(((FakedDataFromXml) value).getComboContent());
                                        ((JComboBox) tableCellRendererComponent).setSelectedItem(((FakedDataFromXml) value).getDisplayContent());
                                        throw new RuntimeException("unknown Component");
                                return tableCellRendererComponent;
                        JOptionPane.showMessageDialog(null, new JScrollPane(jTable));