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