Results 1 to 6 of 6
  1. #1
    LiLGuy2004 is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Multiple JComboBox

    I'm trying to build an Application. and basically I'm trying to use 2 JComboBox and a JButton, the Jbutton is basically submitting the application and waiting for a response. The JComboBox is just letting me know which of 3+ formulas I going to use to convert a number. I'm stuck at the ActionListener method, I just can't figure out how I can use the JComboBox to determine what forumula to use. Please help.


    This is what I have so far

    Java Code:
    import java.util.Formatter;
    import java.util.Scanner;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.text.JTextComponent;
    
    import FileRetriver.ButtonClickHandler;
    
    public class TemperatureConversion extends JFrame
    {
    	private JTextComponent display;
    	private FlowLayout layout;
    	private JTextField textField;
    	private JComboBox convertTo;
    	private JComboBox convertFrom;
    	private JButton covert;
    	private JLabel question;
    	private JLabel converted;
    	
    	private static final String[] ConvertFrom = { "Fahrenheit" "Celsius" "Kelvin" }; // ComboBox Drop Down List for ConvertFrom
    	private static final String[] ConvertTo = { "Fahrenheit" "Celsius" "Kelvin" }; // ComboBox Drop Down List for ConvertTo
    	
    	public TemperatureConversion()
    	{
    		super( "TemperatureConversionTester" );
    		layout = new FlowLayout();
    		
    		// Create and Add JLabel Question
    		question = new JLabel("Enter a Temperature");
    		add( question );
    		
    		// Create and add Text Field
    		textField = new JTextField ();
    		add( textField );
    		
    		// Create and add Combo Box (Drop Down List)
    		convertTo = new JComboBox( ConvertFrom ); // Set up JComboBox Convert From
    		convertTo.setMaximumRowCount( 2 );
    		
    		add( convertTo );
    		
    		convertFrom = new JComboBox( ConvertTo ); // Set up JComboBox Convert to
    		convertFrom.setMaximumRowCount( 2 );
    		
    		add( convertFrom );
    		
    		// Create and add JLabel converted
    		converted = new JLabel();
    		add( converted );
    		
    		// register Event Handlers
    		ButtonClickHandler handler = new ButtonClickHandler();
    		convert.addActionListener( handler );
    		
    	} // End of TemperatureConversion constructor
    	
    	public class ButtonClickHandler implements ActionListener
    	{
    		public void actionPreformed( ActionEvent event)
    		{
    			
    		}
    	}
    }
    P.S my driver class is working fine I have it already completed

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Multiple JComboBox

    I just can't figure out how I can use the JComboBox to determine what forumula to use
    In fact you will have to use both combo boxes. The selected index of one will tell you the units you should use to interpret the value entered by the user, the selected index of the other will tell you the units you should use when you set the value of the output label.

    One approach would be to break up the conversion into two parts, analogous to the combo boxes. That is, take the user entered value and use your knowledge of the intended units to convert it some some arbitrary units (Kelvin, say). Then look at the other combo box and use its selected value to convert the temperature in Kelvin to the desired output value.

    -----

    Java Code:
    public class TemperatureConversion extends JFrame
    {
    What you have written "feels" more like a panel than a frame. That is, other than setting a title you do nothing that is frame like. It would be better to extend JPanel and add the resulting panel to a JFrame instance in your driver class. This panel would then become a reusable software element you could include in other software.

    It's a good idea not to give variables more scope than they need. (The devil sends bugs to make use of variables' unused scope!) If the layout, eg, is only used in the constructor declare it there. You can always broaden the scope of a variable if it becomes necessary.

  3. #3
    LiLGuy2004 is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Re: Multiple JComboBox

    Thanks, Ill changed that to JPanel, never thought about going that route. Ok the biggest problem I guess i'm having is trying to find the best way to code this in the handler method. I just don't really know which way I should do this. Using a "If Celsius && Kelvin selected, then using this certain formula, to convert to Kelvin.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Multiple JComboBox

    I just don't really know which way I should do this. Using a "If Celsius && Kelvin selected, then using this certain formula, to convert to Kelvin.
    What, exactly, about this approach don't you understand? How to find "if Celsius && Kelvin selected"? Something else?

  5. #5
    LiLGuy2004 is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Re: Multiple JComboBox

    I'm having a hard time to code an if statement with the JComboBox. Not really sure how to code this?

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Multiple JComboBox

    JComboBox has a method, getSelectedIndex() which will help.

    (It also has a getSelectedItem() method, so you could also look at that to decide if "Kelvin" or whatever is selected.)

Similar Threads

  1. One process open multiple threads on multiple CPUs
    By kfcnhl in forum Threads and Synchronization
    Replies: 2
    Last Post: 12-12-2011, 08:27 AM
  2. multiple Data from Jsp that having same name of multiple input tage to servlet
    By rahul9323.2007@gmail.com in forum Advanced Java
    Replies: 0
    Last Post: 07-29-2011, 01:00 PM
  3. problem w/ multiple jcombobox
    By garagumo in forum Advanced Java
    Replies: 0
    Last Post: 05-06-2011, 06:47 AM
  4. Running multiple threads on multiple CPU cores?
    By Dosta in forum Threads and Synchronization
    Replies: 2
    Last Post: 09-19-2010, 03:48 PM
  5. Replies: 2
    Last Post: 12-10-2009, 01:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •