Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-26-2008, 01:52 PM
Member
 
Join Date: Jul 2008
Posts: 5
Keerti is on a distinguished road
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
Sponsored Links
  #2 (permalink)  
Old 07-27-2008, 01:44 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
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, 12:54 PM
Member
 
Join Date: Jul 2008
Posts: 5
Keerti is on a distinguished road
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, 07:48 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
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
Sponsored Links
Reply


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

vB 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 +3. The time now is 12:08 PM.


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