RE: Implementing JComboBox through if-then statements

Posted 17 July 2011 - 02:53 PM

Ok, to recap what I need help with: I had a piece of code written several months ago, in which I had a coding hiatus due to unrelated circumstances, of which I had posted a call for help, tips, and the like (Of which was too far beyond me and I STILL can't figure out those damn tutorials). I have decided to try once again and go for a human element. Let's try to stick to the basics, shall we?

Due to my current lack of understanding (still...) in using Combo Boxes, I would like to make this very simple: 1.) How to correctly set up JComboBox (Strings or Objects) so it can be later implemented when the correct field is selected, and 2.) How to take the previous step and apply it into a useful piece of code (if "X" is clicked, then run through "Y" steps). Simple as that!

I shall provide to those interested the piece of code as I would have it run ( I know it looks gross to the more advanced programmers, but bear with it as it is currently the concept that I desire, not the form) and feel free to pick at what I have provided and try to see what I'm aiming for. All beneficial criticisms, remarks, or questions are welcomed and encouraged.

I have this version currently written in a functioning form of BASIC, so I will also provide a copy of that as well to those interested, which was the original idea to be converted to Java. That piece is what I'm aiming for in functionality, but am lacking the Java tools in which to adequately meet that standard

Replies To: RE: Implementing JComboBox through if-then statements

Re: RE: Implementing JComboBox through if-then statements

Posted 17 July 2011 - 03:08 PM

I didn't look through all of the code you posted. You asked to keep it simple, so I'll ask you to do the same. If you're having trouble with a JComboBox, isolate that code and then describe the trouble you're having making it work. Start with a simple, small snippet until your confidence and understanding improve. Give exact error messages (cut and paste) or provide details on how the actual program's performance doesn't meet your expectations: explain what you want it to do and what is it actually doing.

Creating a JComboBox is simple enough. You can use the constructor to define the possible JComboBox selections:

Then, you can find which selection the user has made by adding a listener to the JComboBox to determine when a selection has been made or by asking the user to select an OK button or some other indicator to let the program know that it can process the selection. You then determine which selection the user made by this using the following methods (among other possibilities):

Re: RE: Implementing JComboBox through if-then statements

Posted 17 July 2011 - 03:11 PM

On line 4 you create an Array of JComboBoxes and then pass it to your JComboBox constructor, equComBox, on line 136. This is adding JComboBoxes to equComBox which isn't what you want, change your equationString type to String rather then JComboBox on line 4 like this:

Re: RE: Implementing JComboBox through if-then statements

Posted 17 July 2011 - 03:19 PM

@Greg: can do on the simple, I was just giving the entirety, however the portion I believe is the problem is this:

//Strings that are to be put into a JComboBox, they act as options for which to compute area via multiple methods
JComboBox[] equationString = {"Circumradius","Height","Sidelength","Point"};
//creates the drop down menu object
private JComboBox equComBox;
equComBox = new JComboBox(equationString);
equComBox.setBounds(120,10,150,20);
equComBox.setMaximumRowCount(5);
equComBox.setPreferredSize(new Dimension(512,512));
equComBox.addActionListener(this);
contentPane.add(equComBox);

Re: RE: Implementing JComboBox through if-then statements

Posted 17 July 2011 - 03:34 PM

Well in your ActionListener you get an IndexOutOfBoundsException thrown because you're trying to access index 4 of your String array but it only holds 4 elements, the highest index being 3. This is because arrays first index is 0, not 1. Now what exactly isn't working about your JComboBox? I fixed the above error and the JComboBox seems to work correctly although it just printed out 0.0

Forget that last sentence I found your problem, it lies in your actionPerformed() method. You grab the JComboBox that had the action event occur but you never get what item was ever selected. You can do this by Creating a String and setting it equal to the value returned by selected.getSelectedInput(); Here is an example:

Re: RE: Implementing JComboBox through if-then statements

Posted 17 July 2011 - 03:36 PM

Then from my first post, you might be able to see your problem:

//Strings that are to be put into a JComboBox, they act as options for which to
//compute area via multiple methods
JComboBox[] equationString = {"Circumradius","Height","Sidelength","Point"};
// ^ really? isn't it a String[]
//creates the drop down menu object
private JComboBox equComBox;
equComBox = new JComboBox(equationString);

Re: RE: Implementing JComboBox through if-then statements

Posted 17 July 2011 - 04:30 PM

GregBrannon, on 17 July 2011 - 05:36 PM, said:

Then from my first post, you might be able to see your problem:

//Strings that are to be put into a JComboBox, they act as options for which to
//compute area via multiple methods
JComboBox[] equationString = {"Circumradius","Height","Sidelength","Point"};
// ^ really? isn't it a String[]

Yeah, Giggly noticed that earlier and has been dealt with. The only reason that was there, and I apologize for the incorrect signals that it was sending, was to just mess around to see if the code my respond positively to an Object instead of a String. Which, of course, obviously doesn't work and I completely forgot to change that to in the original upload

Re: RE: Implementing JComboBox through if-then statements

Well in your ActionListener you get an IndexOutOfBoundsException thrown because you're trying to access index 4 of your String array but it only holds 4 elements, the highest index being 3.

I found your problem, it lies in your actionPerformed() method. You grab the JComboBox that had the action event occur but you never get what item was ever selected. You can do this by Creating a String and setting it equal to the value returned by selected.getSelectedInput(); Here is an example:

Re: RE: Implementing JComboBox through if-then statements

Uhhh, that seems to be the problem: I'm not sure what exactly I'm supposed to do with a list of data like that. I'll try to explain in terms of the way I wrote the original code in BASIC.

I have say 3 coordinated points that make up a triangle, their values are say, (0,0), (2,4), and(4,0). As the way the lists worked in BASIC, you were asked to input the two lists: the X-coordinate list and the Y-coordinate list. So the above coordinates would be shown in lists, x & y respectively, {0,2,4} and {0,4,0}. Then through either a For loop or a Summation, I would draw the corresponding data points from the list as I desired and manipulate them to form the equation I need to solve the area of the triangle.

Re: RE: Implementing JComboBox through if-then statements

Posted 17 July 2011 - 05:38 PM

Well you can extract the data using regular expressions (which I'm not too experienced with yet) or you can use a for loop that tests if the value is a number, if it is extract the number, if its not then skip the character. Here is a link on regex in java: