2 Replies Latest reply on Jun 21, 2013 5:44 AM by 952174

    SwiXML - JList

    952174

      Hi,

       

      We are trying to build GUI using SwiXml. So we need to build List box using String array.

      There is <list> tag to be used to generate a List Box. But some how, it's not showing the elements in the list box.

      We are suspecting the id attribute of list tag.

      <list id="list"/>

       

      In the constructor of java class, we are trying to initialize the list variable as follows

          String[] data = new String[]{"Bird", "Cat", "Dog", "Rabbit", "Pig"};
          public JList list;

           list = new JList(data);

       

      Question: What could be the value for id attribute? It should reference to variable or a method.

       

      Similarly combobox is working as per expectation using initclass attribute as follows.

      <combobox initclass="ComboModel" Action="DO_SELECT"/>

       

      public class ComboModel extends DefaultComboBoxModel{

        /**

         * Constructs a ComboModelobject.

         */

       

        public ComboModel() {

            super(new Object[]{"Bird", "Cat", "Dog", "Rabbit", "Pig"});

        }

      }

       

      Unable to identify where iam doing wrong. It would be really helpful, if you can suggest any pointer on this.

       

      Thanks in advance,

      Samba.

        • 1. Re: SwiXML - JList
          wolfpaulus

          You need to implement a model class like so:

           

          import javax.swing.DefaultListModel;

           

           

          public class Model extends DefaultListModel<String> {

            /**

            * Constructs a DefaultComboBoxModel object.

            */

            public Model() {

              for (String s : new String[] {"Bird", "Cat", "Dog", "Rabbit", "Pig"} ) {

            this.addElement(s);

            }

            }

          }

           

          The you have your main class:

           

          import org.swixml.SwingEngine;

           

           

          import javax.swing.JList;

          import java.awt.event.WindowAdapter;

          import java.awt.event.WindowEvent;

          import javax.swing.event.ListSelectionListener;

          import javax.swing.event.ListSelectionEvent;

           

           

          public class HelloList extends WindowAdapter {

           

            private JList mList; /*instantiated by swixml when rendering the UI */

           

           

            private HelloList() throws Exception {

            new SwingEngine( this ).render( "./hellolist.xml" ).setVisible( true );

            System.out.println( mList.size() );

            mList.addListSelectionListener( new ListSelectionListener() {

            public void valueChanged( final ListSelectionEvent e) {

              System.out.println( mList.getSelectedValue() );

            }

            });

            }

           

           

            /**

              * Invoked when a window is in the process of being closed.

              * The close operation can be overridden at this point.

              */

             public void windowClosing(final WindowEvent e) {

               super.windowClosing(e);

               System.exit(0);

             }

           

            /** Makes the class bootable */

            public static void main( final String[] args ) throws Exception {

            new HelloList();

            }

          }

           

          And finally, the XML descriptor:

           

          <?xml version="1.0" encoding="UTF-8"?>

          <frame 

            xmlns="http://www.swixml.org/2007/SwixmlTags"

            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

            xsi:schemaLocation="http://www.swixml.org/2007/SwixmlTags http://www.swixml.org/2007/swixml.xsd"

            title="Swixml: List Demo" size="400,260" defaultCloseOperation="JFrame.EXIT_ON_CLOSE">

           

           

            <panel constraints="BorderLayout.CENTER">

            <scrollpane>

              <list id="mList" initclass="Model" Font="ARIAL-BOLD-14" VisibleRowCount="3"/>

            </scrollpane>

            </panel>

          </frame>

           

                   

          • 2. Re: SwiXML - JList
            952174

            Thanks for the information. It's working as per expectation.

            -Samba