Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-26-2008, 12:52 PM
Member
 
Join Date: Jul 2008
Posts: 5
Rep Power: 0
Keerti is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 07-27-2008, 12:44 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,389
Rep Power: 3
hardwired is on a distinguished road
Default
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());
    }
}
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-28-2008, 11:54 AM
Member
 
Join Date: Jul 2008
Posts: 5
Rep Power: 0
Keerti is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-28-2008, 06:48 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,389
Rep Power: 3
hardwired is on a distinguished road
Default
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);
    }
}
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +2. The time now is 06:08 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org