Vinn Clint
asked on
Using JcheckBox in Java swing components
I need to replace the JList with 7 JCheckboxes. Run the code and see what I mean. Thanks
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class Pizzeria extends JFrame {
protected final String SIZE_OPTIONS[]={"Small","Medium","Large","Extra Large"};
protected final double SIZE_PRICE[]={7,9,11,14};
protected final String DRINK_OPTIONS[]={"Soda","Tea","Bottled Water","Tap Water"};
protected final double DRINK_PRICE[]={2,1.5,1.25,0};
String tops[]={"Mushrooms","Onions","Cheese","Green Peppers","Pepperoni","Black Olives","Sausage"};
private JCheckBox peppCheckBox, mushCheckBox;
protected double pizzaCost,drinksCost,toppingsCost;
public Pizzeria()
{
super("Pizza App");
setDefaultCloseOperation(EXIT_ON_CLOSE);
createAndDisplayUI();
}
//The UI is designed as follows. Components are added vertically one below aonohter on the
//main panel. The 1st one to be added is a panel with 3 components (size, drinks and toppings)
//which are horizontal. The 2nd one to be added to main panel is the buttons panel consisting
//of 2 buttons.
private void createAndDisplayUI()
{
//horizontal panel to hold the 3 panels for size, drinks and toppings
JPanel horizontal=new JPanel();
horizontal.setLayout(new GridLayout(1, 3));
JPanel sizePanel=new JPanel();
sizePanel.setLayout(new BoxLayout(sizePanel,BoxLayout.Y_AXIS));
sizePanel.setBorder(BorderFactory.createTitledBorder("Pizza Size"));
ButtonGroup group=new ButtonGroup();
JRadioButton rb=null;
//listener for size options selection, will set the basic cost of pizza
//depending on size
ActionListener sizeListener =new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setPizzaCost(e.getActionCommand());
}
};
//create radio buttons for different sizes and group them and link them
//with listener
for(int i=0;i<SIZE_OPTIONS.length;i++)
{
rb=new JRadioButton(SIZE_OPTIONS[i]+"( $"+SIZE_PRICE[i]+" )");
sizePanel.add(rb);
group.add(rb);
rb.addActionListener(sizeListener);
}
rb.setSelected(true); //make the last radio button as selected
pizzaCost=SIZE_PRICE[3]; //since the last button is selected the cost is set appropriatel
//create a panel for drinks options grouped with border and set teh action listener
JPanel drinksPanel=new JPanel();
drinksPanel.setLayout(new BoxLayout(drinksPanel,BoxLayout.Y_AXIS));
drinksPanel.setBorder(BorderFactory.createTitledBorder("Drinks "));
group=new ButtonGroup();
//the actionlistener to drinks option will call the method to set
//drinks cost
ActionListener drinkListener =new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setDrinksCost(e.getActionCommand());
}
};
String price;
for(int i=0;i<DRINK_OPTIONS.length;i++)
{
if(DRINK_PRICE[i]==0)
price="No Charge";
else
price="$"+DRINK_PRICE[i];
rb=new JRadioButton(DRINK_OPTIONS[i]+"( "+price+" )");
drinksPanel.add(rb);
group.add(rb);
rb.addActionListener(drinkListener);
}
rb.setSelected(true);
//create a panel for the topppings list with scroll pane
String toppings[]={"Mushrooms","Onions","Cheese","Green Peppers","Pepperoni","Black Olives","Sausage"};
JPanel toppingsPanel=new JPanel();
JList toppingsList=new JList(toppings);
toppingsList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
toppingsList.setVisibleRowCount(6);
JScrollPane toppingScroll=new JScrollPane(toppingsList);
toppingsPanel.add(toppingScroll);
toppingsPanel.setBorder(BorderFactory.createTitledBorder("Toppings"));
//add list selection listenr to teh jlist object to set the toppings cost whenever
//selection changes
toppingsList.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
toppingsCost=toppingsList.getSelectedIndices().length * 1;
}
});
// String toppingz[]={"Mushrooms","Cheese","Pepperoni","Sausage"};
// JPanel panel = new JPanel();
// for (String topping : toppingz){
// JCheckBox ct = new JCheckBox(topping);
// panel.add(ct);
//
// }
//create the buttons panel
JPanel buttonsPanel=new JPanel();
JButton exitBut,costBut;
buttonsPanel.add(costBut=new JButton("Calculate Price"));
buttonsPanel.add(exitBut=new JButton("Exit"));
//now start laying out the panels appropirately
horizontal.add(sizePanel);
horizontal.add(drinksPanel);
horizontal.add(toppingsPanel);
//horizontal.add(panel);
Container mainPanel=getContentPane();
mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS));
mainPanel.add(horizontal);
mainPanel.add(buttonsPanel);
//set up the action listeners for the 2 buttons
costBut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showCost();
}
});
exitBut.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
setSize(600,250);
setVisible(true);
}
private void setDrinksCost(String drink)
{
for(int i=0;i<DRINK_OPTIONS.length;i++)
{
if(drink.startsWith(DRINK_OPTIONS[i] ))
{
drinksCost=DRINK_PRICE[i];
return;
}
}
}
private void setPizzaCost(String size)
{
for(int i=0;i<SIZE_OPTIONS.length;i++)
{
if(size.startsWith(SIZE_OPTIONS[i]))
{
pizzaCost=SIZE_PRICE[i];
return;
}
}
}
private void showCost()
{
double total=pizzaCost+drinksCost+toppingsCost;
JOptionPane.showMessageDialog(this,"The total cost is "+total);
}
public static void main(String[] args) {
Pizzeria u =new Pizzeria();
}
}
There wouldn't seem to be much problem adding an array of checkboxes though . . . smthg like (needs tidying)
String[] tops = {"Mushrooms","Straw","Bacon","Toms","Biscuits","Glue","Tin"};
for(int x=0;x<tops.length;x++){checkPanel.add(new JCheckBox(tops[x]));}
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Seems to run alright to me using CTRL-click on the list.