Question by Shazain Ali: Using a combo box in java?
I had a question above a program I’m writing. It asks the user a question with the response being clicked in a radio button. Based on what is picked a combo box is shown giving the user options to chose from. (if yes one combo box is shown, if no another is shown) I am stuck as to how to write this code in java.
Best answer:
Answer by Rvna
Think of having a JFrame with a combobox. Also another JFrame with another combobox. Then call setVisible() with any of these JFrame objects
Add your own answer in the comments!
import javax.swing.*;
import java.awt.*;
public class Surgeries extends JPanel
{
public final double LIPOSUCTION = 2000.00;//cost of the surgeries
public final double TUMMY_TUCK = 1000.00;
public final double BREAST_AUG = 6000.00;
public final double NOSE_JOB = 500.00;
public final double BUTT_LIFT = 1000.00;
public final double DAILY_RATE = 1000.00;
private JPanel panel;
private JLabel DaysLabel;
private JTextField DaysTextField;//to allow the user to enter the days in the hospital
private JComboBox surgeryList;
private ImageIcon facepuzzel;
private JLabel picLabel;
private String[] surg = {” Pick A Procedure”,”Liposuction”, “Tummy Tuck”, “Breast Augmentation”,
“Nose Job”, “Butt Lift”};
//Constructor
public Surgeries()
{
panel = new JPanel();
add(panel);
//create layout with the surgeries listed
setLayout(new GridLayout(1,1));
/**
ImageIcon facepuzzel = new ImageIcon(“H:/Last final/image/facepuzzel.jpg”);
JLabel picLabel = new JLabel();
picLabel.setIcon(facepuzzel);**/
DaysLabel = new JLabel(“How many days was the patient in the hospital?”);
DaysTextField = new JTextField(“0”);
DaysTextField.setToolTipText(“Days in The Hospital!”);
DaysTextField.setColumns(4);
surgeryList = new JComboBox(surg);
surgeryList.setToolTipText(“Pick One!”);
panel.add(surgeryList);
panel.add(DaysLabel);
panel.add(DaysTextField);
//panel.add(picLabel);
//picLabel.setMinimumSize(new Dimension(20, 30));
panel.setBorder(BorderFactory.createLineBorder(Color.red));//sets borders on JPanel
panel.setBackground(Color.BLACK);
panel.setForeground(Color.red);
surgeryList.setBorder(BorderFactory.createLineBorder(Color.red));//sets borders on JPanel
surgeryList.setBackground(Color.BLACK);
surgeryList.setForeground(Color.red);
//surgeryList.setFocusable(false);
DaysLabel.setBorder(BorderFactory.createLineBorder(Color.red));//sets borders on JPanel
DaysLabel.setBackground(Color.BLACK);
DaysLabel.setForeground(Color.red);
DaysTextField.setBorder(BorderFactory.createLineBorder(Color.red));//sets borders on JPanel
DaysTextField.setBackground(Color.BLACK);
DaysTextField.setForeground(Color.red);
}
//get the prices of the surgery provided
public double getSurgeryCost()
{
double SurgeryCost = 0.0;
double days;
String input1, input2;
//convert the daystext to a number
input1 = DaysTextField.getText();
days = Double.parseDouble(input1);
//get the selected surgery and calculate price
input2 = (String)surgeryList.getSelectedItem();
if(input2.equals(“Liposuction”))
{
SurgeryCost = LIPOSUCTION;
}
else if(input2.equals(“Tummy Tuck”))
{
SurgeryCost = TUMMY_TUCK;
}
else if(input2.equals(“Breast Augmentation”))
{
SurgeryCost = BREAST_AUG;
}
else if(input2.equals(“Nose Job”))
{
SurgeryCost = NOSE_JOB;
}
else if(input2.equals(“Butt Lift”))
{
SurgeryCost = BUTT_LIFT;
}
SurgeryCost = SurgeryCost + days*DAILY_RATE;
return SurgeryCost;
}
}
etSelectedItem() <-----This tells you what is selected in the combox.