4 Replies Latest reply: Mar 25, 2014 6:01 PM by TPD-Opitz RSS

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

    3Skumar

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

      For eg:

      <Root>

      <param comp=textfield>

      <param comp=textfield>

      <param comp=checkbox>

      <param comp=combobox>

      </Root>

       

      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.
          TPD-Opitz

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

          What is the usecase of this requirement?

          Why don't you use JavaFX instead?

           

          bye

          TPD

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

            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.
              3Skumar

              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.
                TPD-Opitz

                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() {
                            super();
                            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"))) {
                            @Override
                            public Class<?> getColumnClass(int columnIndex) {
                                return FakedDataFromXml.class;
                            }
                        };
                        JTable jTable = new JTable(tableModel);
                        jTable.setRowHeight(23);
                        jTable.setDefaultRenderer(FakedDataFromXml.class, new DefaultTableCellRenderer() {
                            @Override
                            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());
                                        break;
                                    case "checkbox":
                                        tableCellRendererComponent = new JCheckBox();
                                        ((JCheckBox) tableCellRendererComponent).setSelected(Boolean.valueOf(((FakedDataFromXml) value).getDisplayContent()));
                                        break;
                                    case "combobox":
                                        tableCellRendererComponent = new JComboBox<>(((FakedDataFromXml) value).getComboContent());
                                        ((JComboBox) tableCellRendererComponent).setSelectedItem(((FakedDataFromXml) value).getDisplayContent());
                                        break;
                                    default:
                                        throw new RuntimeException("unknown Component");
                                }
                                tableCellRendererComponent.setBackground(defaultTableCellRendererComponent.getBackground());
                                tableCellRendererComponent.setForeground(defaultTableCellRendererComponent.getForeground());
                                return tableCellRendererComponent;
                            }
                        });
                        JOptionPane.showMessageDialog(null, new JScrollPane(jTable));
                    }
                }
                
                

                 

                bye

                TPD