Results 1 to 4 of 4

Thread: JComboBoxes

  1. #1
    Keerti is offline Member
    Join Date
    Jul 2008
    Posts
    5
    Rep Power
    0

    Default JComboBoxes

    Hi all
    I am working on a UI which has 2 tabs.In the class for the first tab I declare and initialize an array of JComboBoxes.
    public class tabone{
    protected JComboBox[] combolist = new JComboBox[3];
    public addComponents(){
    for(int i=0;i<4;i++)
    {
    this.combolist[i] = new JComboBox();
    }
    //set name for JcomboBoxes
    combolist[0].setName("combo1");
    combolist[1].setName("combo2");
    combolist[2].setName("combo3");
    combolist[3].setName("combo4");
    }
    }

    In the class for the second Tab I want to access the last two JComboBoxes.
    These two claases are in the same package.
    public class tabtwo extends tabone{
    String name = combolist[2].getName();
    This line throws up an error:Exception in thread "main" java.lang.NullPointerException

    Can someone please tell why i am getting this error.
    or
    How should I access the JComboBoxes in the class for tabtwo?

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import javax.swing.JComboBox;
    
    public class ComboTalk {
        public static void main(String[] args) {
            TabOne tabOne = new TabOne();
            TabTwo tabTwo = new TabTwo(tabOne);
            tabTwo.test(2);
        }
    }
    
    class TabOne {
        JComboBox[] combos;
    
        public TabOne() {
           String[] names = { "one", "two", "three" };
           combos = new JComboBox[names.length];
           for(int i = 0; i < names.length; i++) {
               combos[i] = new JComboBox();
               combos[i].setName(names[i]);
           }
        }
    
        public JComboBox getComboBox(int index) {
            return combos[index];
        }
    }
    
    class TabTwo {
        TabOne tabOne;
    
        public TabTwo(TabOne tabOne) {
            this.tabOne = tabOne;
        }
    
        public void test(int index) {
            JComboBox comboBox = tabOne.getComboBox(index);
            System.out.printf("combos[%d] name = %s%n",
                               index, comboBox.getName());
        }
    }

  3. #3
    Keerti is offline Member
    Join Date
    Jul 2008
    Posts
    5
    Rep Power
    0

    Default

    Hi
    Thanks for the solution Hardwired.It works.:)
    In continuation of the same UI program ..I have a few more Queries.
    ComboBox[2] is present in tab2. The list for this combobox2 depends on the
    value selected in combobox[1] which is present in tab1.
    I have added an action listener on combobox1 .when the user selects the value, I get the list to be loaded for combobox2 in the form of a vector.
    But I am not able to put this in the list for combobox2.that is i am not able to view it in the UI.The list for combobox2 remains blank

    here is what I do:

    listforcombo---is the vector that has to be loaded into combobox2
    listforcombo = listofindevars.findependentValues(frameText,var_na me,frameText,(String) dependentVars.elementAt(i));

    ComboBoxModel aModel = new DefaultComboBoxModel( listforcombo );
    combolist[2].setModel(aModel);

    Please help me in resolving this problem.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ComboTalk {
        public static void main(String[] args) {
            TabOne tabOne = new TabOne();
            TabTwo tabTwo = new TabTwo(tabOne);
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(5,5,5,5);
            panel.add(tabOne.getComboBox(), gbc);
            panel.add(tabTwo.comboBox, gbc);
            JOptionPane.showMessageDialog(null, panel, "", -1);
        }
    }
    
    class TabOne {
        JComboBox comboBox;
    
        public TabOne() {
           String[] items = { "one", "two", "three" };
           comboBox = new JComboBox(items);
           
        }
    
        public JComboBox getComboBox() {
            return comboBox;
        }
    }
    
    class TabTwo implements ActionListener {
        TabOne tabOne;
        JComboBox comboBox;
        String[][] items = {
            { "one 1",   "one 2",   "one 3"   },
            { "two 1",   "two 2",   "two 3"   },
            { "three 1", "three 2", "three 3" }
        };
    
        public TabTwo(TabOne tabOne) {
            this.tabOne = tabOne;
            int index = tabOne.getComboBox().getSelectedIndex(); 
            comboBox = new JComboBox(items[index]);
            tabOne.getComboBox().addActionListener(this);
        }
    
        private void updateComboBox(int index) {
            comboBox.removeAllItems();
            for(int i = 0; i < items[index].length; i++) {
                comboBox.addItem(items[index][i]);
            }
        }
    
        public void actionPerformed(ActionEvent e) {
            JComboBox comboBox = tabOne.getComboBox();
            int index = comboBox.getSelectedIndex();
            System.out.printf("index = %d%n", index);
            updateComboBox(index);
        }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •