RE: Implementing JComboBox through if-then statements

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 979 Views - Last Post: 18 July 2011 - 04:23 PM Rate Topic: -----

#1 BloodyInari  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 16-November 09

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.


This is the GUI


public class Graphic extends JFrame implements ActionListener
{
	//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"};
	
	//the Label objects
	private JLabel methodBox;
	private JLabel varBox;
	private JLabel numBox;
	private JLabel XCoordinateBox;
	private JLabel YCoordinateBox;
	private JLabel areaBox;
	private JLabel areaAnswerBox;
	
	//the Field objects
	private JTextField numField;
	private JTextField varField;
	private JTextField XCoordinateField;
	private JTextField YCoordinateField;
	
	//the Button objects
	private JButton Compute;
	private JButton Clear;
	
	//creates the drop down menu object
	private JComboBox equComBox;
	
    //variables, used later to convert strings into values
	private double calcVar;
	private double calcNum;
	private double areaDisp;
	
	
	// creates the GUI and all the objects within
	public Graphic()
	{
		Container contentPane = getContentPane();
		
		 	methodBox = new JLabel("Methods:");
			methodBox.setBounds(10,10,100,20);
			methodBox.setForeground(Color.black);
			methodBox.setHorizontalAlignment(0);
			methodBox.setFont(new Font("serif", Font.PLAIN, 14));
			methodBox.setBorder(BorderFactory.createLineBorder(Color.black));
			methodBox.setVisible(true);
			contentPane.add(methodBox);
			
			numBox = new JLabel("Number of Sides:");
			numBox.setBounds(10,40,100,20);
			numBox.setForeground(Color.black);
			numBox.setHorizontalAlignment(0);
			numBox.setFont(new Font("serif", Font.PLAIN, 13));
			numBox.setBorder(BorderFactory.createLineBorder(Color.black));
			numBox.setVisible(true);
			contentPane.add(numBox);
			
			varBox = new JLabel("Variable");
			varBox.setBounds(10,70,100,20);
			varBox.setForeground(Color.black);
			varBox.setHorizontalAlignment(0);
			varBox.setFont(new Font("serif", Font.PLAIN, 14));
			varBox.setBorder(BorderFactory.createLineBorder(Color.black));
			varBox.setVisible(true);
			contentPane.add(varBox);
			
			XCoordinateBox = new JLabel("X-Coordinates:");
			XCoordinateBox.setBounds(10,100,125,20);
			XCoordinateBox.setForeground(Color.black);
			XCoordinateBox.setHorizontalAlignment(0);
			XCoordinateBox.setFont(new Font("serif", Font.PLAIN, 14));
			XCoordinateBox.setBorder(BorderFactory.createLineBorder(Color.black));
			XCoordinateBox.setVisible(false);
			contentPane.add(XCoordinateBox);
			
			YCoordinateBox = new JLabel("Y-Coordinates:");
			YCoordinateBox.setBounds(140,100,130,20);
			YCoordinateBox.setForeground(Color.black);
			YCoordinateBox.setHorizontalAlignment(0);
			YCoordinateBox.setFont(new Font("serif", Font.PLAIN, 14));
			YCoordinateBox.setBorder(BorderFactory.createLineBorder(Color.black));
			YCoordinateBox.setVisible(false);
			contentPane.add(YCoordinateBox);
			
			areaBox = new JLabel("Area =");
			areaBox.setBounds(10,220,100,20);
			areaBox.setForeground(Color.black);
			areaBox.setHorizontalAlignment(0);
			areaBox.setFont(new Font("serif", Font.PLAIN, 14));
			areaBox.setBorder(BorderFactory.createLineBorder(Color.black));
			areaBox.setVisible(true);
			contentPane.add(areaBox);
			
			areaAnswerBox = new JLabel("");
			areaAnswerBox.setBounds(120,220,150,20);
			areaAnswerBox.setForeground(Color.black);
			areaAnswerBox.setHorizontalAlignment(0);
			areaAnswerBox.setFont(new Font("serif", Font.BOLD, 14));
			areaAnswerBox.setBorder(BorderFactory.createLineBorder(Color.black));
			areaAnswerBox.setVisible(true);
			contentPane.add(areaAnswerBox);
			
			numField = new JTextField();
			numField.setBounds(120,40,150,20);
			numField.addActionListener(this);
			contentPane.add(numField);
			
			varField = new JTextField();
			varField.setBounds(120,70,150,20);
			varField.addActionListener(this);
			contentPane.add(varField);
			
			XCoordinateField = new JTextField();
			XCoordinateField.setBounds(10,125,125,20);
			XCoordinateField.addActionListener(this);
			XCoordinateField.setVisible(false);
			contentPane.add(XCoordinateField);
			
			YCoordinateField = new JTextField();
			YCoordinateField.setBounds(140,125,130,20);
			YCoordinateField.addActionListener(this);
			YCoordinateField.setVisible(false);
			contentPane.add(YCoordinateField);
			
			Compute = new JButton("Compute");
			Compute.setBounds(15,155,90,20);
			Compute.setFont(new Font("serif", Font.PLAIN, 14));
			Compute.addActionListener(this);
			contentPane.add(Compute);
			
			Clear = new JButton("Clear");
			Clear.setBounds(15,185,90,20);
			Clear.setFont(new Font("serif", Font.PLAIN, 14));
			Clear.addActionListener(this);
			contentPane.add(Clear);
			
	        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);
	        
	        
	}
	
	public static void main(String args[])
	{
		Graphic frame = new Graphic();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setTitle("Area Solver: Basic Geometry Made Easy");
		frame.setBackground(Color.white);
		frame.setSize(700,350);
		frame.setLocation(20,30);
		frame.setLayout(null);
		frame.setResizable(true);
		frame.setVisible(true);
		
	}

	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() instanceof JComboBox)
		{
			JComboBox selected = (JComboBox)(e.getSource());
			//first menu option, for solving area by Circumradius method
			if(equationString[1] == selected)
			{
				System.out.println("first option selected");
				
				numBox.setVisible(true);
				numField.setVisible(true);
				varBox.setVisible(true);
				varField.setVisible(true);
				XCoordinateBox.setVisible(false);
				XCoordinateField.setVisible(false);
				YCoordinateBox.setVisible(false);
				YCoordinateField.setVisible(false);
				
				areaDisp = Equation.AreaCircumradius(calcNum,calcVar);
			
			}
			//second menu option, for solving area by Height method
			if(equationString[2] == selected)
			{
				System.out.println("second option selected");
				
				double modDet = calcNum%2;
				
				numBox.setVisible(true);
				numField.setVisible(true);
				varBox.setVisible(true);
				varField.setVisible(true);
				XCoordinateBox.setVisible(false);
				XCoordinateField.setVisible(false);
				YCoordinateBox.setVisible(false);
				YCoordinateField.setVisible(false);
				
				//states that if N (the number of sides) is less than 3, then display the below 
				//instead of calculating an answer
				if(calcNum>3)
					{
						areaAnswerBox.setText("Non-valid Value");
					}
				//if N is perfectly divisible by two, then if it is divided by its modulus the result will be zero
				//a simple way of determining if N is even or odd
				else if(modDet == 0)
					{
						areaDisp = Equation.AreaHeightEven(calcNum,calcVar);
					}
				//if N is not perfectly divisible by two, then the dividing of N by its modulus result in a non-zero value
				else if(modDet != 0)
					{
						areaDisp = Equation.AreaHeightOdd(calcNum,calcVar);
					}
			}
			
			//third menu option, for solving area by the Side-length method
			if(equationString[3] == selected)
			{
				System.out.println("third option selected");
				
				numBox.setVisible(true);
				numField.setVisible(true);
				varBox.setVisible(true);
				varField.setVisible(true);
				XCoordinateBox.setVisible(false);
				XCoordinateField.setVisible(false);
				YCoordinateBox.setVisible(false);
				YCoordinateField.setVisible(false);
				
				areaDisp = Equation.AreaSidelength(calcNum,calcVar);
				
			}
			
			//fourth and last menu option, used to solve the area of an irregular polygon via the (X,Y) coordinate
			//points that constitute of its shape in the coordinate plane
			if(equationString[4] == selected)
			{
				System.out.println("fourth option selected");
				
				numBox.setVisible(false);
				numField.setVisible(false);
				varBox.setVisible(false);
				varField.setVisible(false); 
				XCoordinateBox.setVisible(true);
				XCoordinateField.setVisible(true);
				YCoordinateBox.setVisible(true);
				YCoordinateField.setVisible(true);
				
			}	
				
		}
		
		if(e.getSource() instanceof JButton)
		{
		
			JButton clicked = (JButton)(e.getSource());
			//when clicked, program computes Area through preselected method
			if(clicked == Compute)
			{
				System.out.println("Compute button clicked");
				
				calcNum = Double.parseDouble(numField.getText());
				calcVar = Double.parseDouble(varField.getText());
				
				areaAnswerBox.setText("" + areaDisp);
				
			}
			//clears all fields and the AreaAnswerBox when Clear button is clicked
			if(clicked == Clear)
			{
				System.out.println("Clear button clicked");
				
				varField.setText("");
				numField.setText("");
				XCoordinateField.setText("");
				YCoordinateField.setText("");
				areaAnswerBox.setText("");
			}
		}
	}
}



And these are the equations being used


public class Equation 
{
	public static double AreaSidelength(double N, double S)
	{
		double AreaSidelength = (N*Math.pow(S,2)/(4*Math.tan(N/(Math.PI))));
		return AreaSidelength;
		
	}
	public static double AreaHeightEven(double N, double H)
	{
		double AreaHeightEven = ((N*Math.pow(H,2)*Math.tan((Math.PI)/N))/4);
		return AreaHeightEven;
		
	}
	public static double AreaHeightOdd(double N, double H)
	{
		double AreaHeightOdd = ((N*Math.pow(H,2)*Math.pow(Math.tan((Math.PI)/(2*N)),2))/(Math.tan((Math.PI)/N)));
		return AreaHeightOdd;
		
	}
	public static double AreaCircumradius(double N, double R)
	{
		double AreaCircumradius = ((1/2)*N*Math.pow(R,2)*Math.sin((2*Math.PI)/N));
		return AreaCircumradius;
	}
	

}




***Final Note**************************************************************************

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

******************************************************************************

This post has been edited by BloodyInari: 17 July 2011 - 03:21 PM


Is This A Good Question/Topic? 0
  • +

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

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

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:

String[] selections = { "This", "That", "And the other thing" };
JComboBox myComboBox = new 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):

String userSelection = myComboBox.getSelectedItem().toString();


or

int userSelectedIndex = myComboBox.getSelectedIndex();
String userSelection = selections[ userSelectedIndex ];


So try it, and let us know what you're having trouble with.
Was This Post Helpful? 0
  • +
  • -

#3 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

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:

String[] equationString = {"Circumradius","Height","Sidelength","Point"};


Was This Post Helpful? 1
  • +
  • -

#4 BloodyInari  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 16-November 09

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);



and then this

public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() instanceof JComboBox)
		{
			JComboBox selected = (JComboBox)(e.getSource());
			//first menu option, for solving area by Circumradius method
			if(equationString[1] == selected)
			{
				System.out.println("first option selected");
				
				numBox.setVisible(true);
				numField.setVisible(true);
				varBox.setVisible(true);
				varField.setVisible(true);
				XCoordinateBox.setVisible(false);
				XCoordinateField.setVisible(false);
				YCoordinateBox.setVisible(false);
				YCoordinateField.setVisible(false);
				
				areaDisp = Equation.AreaCircumradius(calcNum,calcVar);
			
			}
			//second menu option, for solving area by Height method
			if(equationString[2] == selected)
			{
				System.out.println("second option selected");
				
				double modDet = calcNum%2;
				
				numBox.setVisible(true);
				numField.setVisible(true);
				varBox.setVisible(true);
				varField.setVisible(true);
				XCoordinateBox.setVisible(false);
				XCoordinateField.setVisible(false);
				YCoordinateBox.setVisible(false);
				YCoordinateField.setVisible(false);
				
				//states that if N (the number of sides) is less than 3, then display the below 
				//instead of calculating an answer
				if(calcNum>3)
					{
						areaAnswerBox.setText("Non-valid Value");
					}
				//if N is perfectly divisible by two, then if it is divided by its modulus the result will be zero
				//a simple way of determining if N is even or odd
				else if(modDet == 0)
					{
						areaDisp = Equation.AreaHeightEven(calcNum,calcVar);
					}
				//if N is not perfectly divisible by two, then the dividing of N by its modulus result in a non-zero value
				else if(modDet != 0)
					{
						areaDisp = Equation.AreaHeightOdd(calcNum,calcVar);
					}
			}
			
			//third menu option, for solving area by the Side-length method
			if(equationString[3] == selected)
			{
				System.out.println("third option selected");
				
				numBox.setVisible(true);
				numField.setVisible(true);
				varBox.setVisible(true);
				varField.setVisible(true);
				XCoordinateBox.setVisible(false);
				XCoordinateField.setVisible(false);
				YCoordinateBox.setVisible(false);
				YCoordinateField.setVisible(false);
				
				areaDisp = Equation.AreaSidelength(calcNum,calcVar);
				
			}
			
			//fourth and last menu option, used to solve the area of an irregular polygon via the (X,Y) coordinate
			//points that constitute of its shape in the coordinate plane
			if(equationString[4] == selected)
			{
				System.out.println("fourth option selected");
				
				numBox.setVisible(false);
				numField.setVisible(false);
				varBox.setVisible(false);
				varField.setVisible(false); 
				XCoordinateBox.setVisible(true);
				XCoordinateField.setVisible(true);
				YCoordinateBox.setVisible(true);
				YCoordinateField.setVisible(true);
				
			}	
				
		}
		
		if(e.getSource() instanceof JButton)
		{
		
			JButton clicked = (JButton)(e.getSource());
			//when clicked, program computes Area through preselected method
			if(clicked == Compute)
			{
				System.out.println("Compute button clicked");
				
				calcNum = Double.parseDouble(numField.getText());
				calcVar = Double.parseDouble(varField.getText());
				
				areaAnswerBox.setText("" + areaDisp);
				
			}
			//clears all fields and the AreaAnswerBox when Clear button is clicked
			if(clicked == Clear)
			{
				System.out.println("Clear button clicked");
				
				varField.setText("");
				numField.setText("");
				XCoordinateField.setText("");
				YCoordinateField.setText("");
				areaAnswerBox.setText("");
			}
		}
	}



@Giggly: oh yeah, that. That was there when I was seeing if making it an object would do ANYTHING at all, apparently it didn't and just made more errors. (was originally a String *changes*)
Was This Post Helpful? 0
  • +
  • -

#5 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

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:

JComboBox selected = (JComboBox)(e.getSource());
String stringSelected = (String) selected.getSelectedItem();

//first menu option, for solving area by Circumradius method
if(equationString[0].equals(stringSelected)) {
         //Do something
}


This post has been edited by giggly kisses: 17 July 2011 - 04:20 PM

Was This Post Helpful? 1
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

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);



And I didn't look at your second listing. Too much code.

This post has been edited by GregBrannon: 17 July 2011 - 03:37 PM

Was This Post Helpful? 0
  • +
  • -

#7 BloodyInari  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 16-November 09

Re: RE: Implementing JComboBox through if-then statements

Posted 17 July 2011 - 04:30 PM

View PostGregBrannon, 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

This post has been edited by BloodyInari: 17 July 2011 - 04:31 PM

Was This Post Helpful? 0
  • +
  • -

#8 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Re: RE: Implementing JComboBox through if-then statements

Posted 17 July 2011 - 04:35 PM

Did you try the solution I had in my last post? I tried it on my computer and the solution worked for me.
Was This Post Helpful? 0
  • +
  • -

#9 BloodyInari  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 16-November 09

Re: RE: Implementing JComboBox through if-then statements

Posted 17 July 2011 - 04:35 PM

View Postgiggly kisses, on 17 July 2011 - 05:34 PM, said:

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:

JComboBox selected = (JComboBox)(e.getSource());
String stringSelected = (String) selected.getSelectedItem();

//first menu option, for solving area by Circumradius method
if(equationString[0].equals(stringSelected)) {
         //Do something
}



Ahh, I see now! :genius:

So that was all I needed.....hmm....Thanks a bunch!
(BTW, completely forgot that arrays start at zero, I've been spending too much time with matrices....)

Now to connect the mathematics with the JCB and JButtons! :bananaman:

This post has been edited by BloodyInari: 17 July 2011 - 04:37 PM

Was This Post Helpful? 0
  • +
  • -

#10 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Re: RE: Implementing JComboBox through if-then statements

Posted 17 July 2011 - 04:44 PM

Awesome, glad it worked. Good luck with the rest of the project! :)
Was This Post Helpful? 0
  • +
  • -

#11 BloodyInari  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 16-November 09

Re: RE: Implementing JComboBox through if-then statements

Posted 17 July 2011 - 05:00 PM

No! THANK YOU!! This is great, several months worth of suffering alleviated in hour! :clap:

And now, to continue the self-inflicted torture! :dead:

Perchance you know how to take brackets of data ( example: {x1,x2,x3,...},{y1,y2,y3,...}) and manipulate them into a list respectively from the JTextFields and be stored as such for later use?

That is the way option #4 is used to manipulate (x,y) coordinate data into an area equation and I'm at a loss.....
Was This Post Helpful? 0
  • +
  • -

#12 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Re: RE: Implementing JComboBox through if-then statements

Posted 17 July 2011 - 05:13 PM

So you want to take text in a JTextField in brackets and store them?What do you want to store it in? A file, Array, etc?

This post has been edited by giggly kisses: 17 July 2011 - 05:19 PM

Was This Post Helpful? 0
  • +
  • -

#13 BloodyInari  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 16-November 09

Re: RE: Implementing JComboBox through if-then statements

Posted 17 July 2011 - 05:24 PM

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.

For example: http://en.wikipedia.org/wiki/Polygon
Scroll down until you see the summation that I'm basing off of.

Please see the attached file if necessary.
Attached File  Area Program.txt (5.88K)
Number of downloads: 43

This post has been edited by BloodyInari: 17 July 2011 - 05:25 PM

Was This Post Helpful? 0
  • +
  • -

#14 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

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:

http://download.orac...ex/Pattern.html

If you're going the for loop way then I would suggest using the split() method from the String class to put the list into an array.
Was This Post Helpful? 0
  • +
  • -

#15 BloodyInari  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 16-November 09

Re: RE: Implementing JComboBox through if-then statements

Posted 17 July 2011 - 08:11 PM

Please explain how I would use the split() method. I can go either way with how I design this portion of the code and a For loop would work just fine for what I need.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2