0% found this document useful (0 votes)
16 views

Programme Java Combobox

Uploaded by

bibliohashoka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Programme Java Combobox

Uploaded by

bibliohashoka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

comboBox.

java

1 package lab6;
2
3 import java.awt.BorderLayout;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6
7 import javax.swing.JButton;
8 import javax.swing.JCheckBox;
9 import javax.swing.JComboBox;
10 import javax.swing.JFrame;
11 import javax.swing.JLabel;
12 import javax.swing.JPanel;
13
14 public class comboBox {
15 JFrame aFrame ;
16 JPanel p1;
17 JLabel l1 ;
18 String choices [] = { "Kabsa","Pizza","Burger" } ;
19 JComboBox cb ;
20 JCheckBox ch1 , ch2 , ch3 , ch4 ;
21 JButton b1 ;
22
23
24 public comboBox () {
25 aFrame = new JFrame ("ex2");
26 p1 = new JPanel() ;
27 l1 = new JLabel("Nothing yet") ;
28 b1 = new JButton("Submit") ;
29 cb = new JComboBox (choices);
30 ch1 = new JCheckBox("Red") ;
31 ch2 = new JCheckBox("Blue") ;
32 ch3 = new JCheckBox("Green") ;
33 ch4 = new JCheckBox("Yellow") ;
34
35 cb.setSelectedIndex(1); // this will change the default selection to the second
one since it has the index 1
36
37 b1.addActionListener(new ActionListener() {
38
39 @Override
40 public void actionPerformed(ActionEvent e) {
41 // TODO Auto-generated method stub
42 String text = "You like to eat " + choices [cb.getSelectedIndex()] + "
" ;
43 if (ch1.isSelected() || ch2.isSelected() || ch3.isSelected()||
ch4.isSelected())
44 text = text + " and you fav colors are the folwoing : " ;
45 if (ch1.isSelected()){
46 text = text + ch1.getText() + ", " ;
47 }
48 if (ch2.isSelected()){
49 text = text + ch2.getText() + ", " ;
50 }
51 if (ch3.isSelected()){
52 text = text + ch3.getText() + ", " ;
53 }
54 if (ch4.isSelected()){
55 text = text + ch4.getText() + ". " ;
56 }

Page 1
comboBox.java

57 l1.setText(text);
58 }
59 });
60
61 aFrame.add(l1, BorderLayout.NORTH) ;
62 p1.add(cb) ;
63 p1.add(ch1);
64 p1.add(ch2);
65 p1.add(ch3);
66 p1.add(ch4);
67 aFrame.add(p1 , BorderLayout.CENTER);
68 aFrame.add(b1, BorderLayout.SOUTH) ;
69
70 aFrame.pack();
71 aFrame.setVisible(true);
72 aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
73 }
74 public static void main (String [] args) {
75 comboBox obj = new comboBox() ;
76 }
77
78 }
79

Page 2

You might also like