Results 1 to 6 of 6
Thread: Multiple JComboBox
- 07-28-2012, 04:05 AM #1
Member
- Join Date
- Jul 2012
- Posts
- 3
- Rep Power
- 0
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
P.S my driver class is working fine I have it already completedJava 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) { } } }
- 07-28-2012, 04:21 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: Multiple JComboBox
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.I just can't figure out how I can use the JComboBox to determine what forumula to use
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.
-----
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.Java Code:public class TemperatureConversion extends JFrame {
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.
- 07-28-2012, 04:31 AM #3
Member
- Join Date
- Jul 2012
- Posts
- 3
- Rep Power
- 0
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.
- 07-28-2012, 04:54 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: Multiple JComboBox
What, exactly, about this approach don't you understand? How to find "if Celsius && Kelvin selected"? Something else?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.
- 07-28-2012, 04:57 AM #5
Member
- Join Date
- Jul 2012
- Posts
- 3
- Rep Power
- 0
Re: Multiple JComboBox
I'm having a hard time to code an if statement with the JComboBox. Not really sure how to code this?
- 07-28-2012, 05:05 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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
-
One process open multiple threads on multiple CPUs
By kfcnhl in forum Threads and SynchronizationReplies: 2Last Post: 12-12-2011, 08:27 AM -
multiple Data from Jsp that having same name of multiple input tage to servlet
By rahul9323.2007@gmail.com in forum Advanced JavaReplies: 0Last Post: 07-29-2011, 01:00 PM -
problem w/ multiple jcombobox
By garagumo in forum Advanced JavaReplies: 0Last Post: 05-06-2011, 06:47 AM -
Running multiple threads on multiple CPU cores?
By Dosta in forum Threads and SynchronizationReplies: 2Last Post: 09-19-2010, 03:48 PM -
Activate JComboBox 1 when object is selected in JComboBox 2...
By bahumbaba in forum AWT / SwingReplies: 2Last Post: 12-10-2009, 01:58 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks