java


isSelected() Checkbox error


I'm making some kind of calculating program where user can input the number of drinks that they want to order and a button will calculate the amount of money they need to pay. If they checked the delivery box, there will be an addition of $30 for delivery cost. i'm using isSelected to check if they box if ticked or not but it's not working. the error that i get is "the method isSelected() is undefined for the type Checkbox".
import java.awt.*;
import java.awt.event.*;
public class hwextend{
static Frame frm = new Frame("Action Event");
static Checkbox ckb1 = new Checkbox("Delivery");
static Button btn = new Button("Calculate");
static TextField textfield1 = new TextField();
static TextField textfield2 = new TextField();
static TextField textfield3 = new TextField();
static TextField textfield4 = new TextField();
static TextField textfield5 = new TextField();
public static void main(String[] args) {
// TODO Auto-generated method stub
GridLayout grid = new GridLayout (7,3);
btn.addActionListener(new ActListener());
frm.setLayout(grid);
frm.setSize(400,200);
frm.setBackground(Color.pink);
Label lab1 = new Label ("Tea Series");
Font font1 = new Font(null,Font.BOLD,12);
lab1.setFont(font1);
frm.add(lab1);
frm.add(new Label(""));
frm.add(new Label(""));
frm.add(new Label("Black Tea"));
frm.add(new Label("$70"));
frm.add(textfield1);
frm.add(new Label("Green Tea"));
frm.add(new Label("$70"));
frm.add(textfield2);
Label lab2 = new Label ("Special Flavor Series");
lab2.setFont(font1);
frm.add(lab2);
frm.add(new Label(""));
frm.add(new Label(""));
frm.add(new Label("Alpine Tea"));
frm.add(new Label("$80"));
frm.add(textfield3);
frm.add(new Label("Stewed Oolong Tea"));
frm.add(new Label("$80"));
frm.add(textfield4);
frm.add(btn);
frm.add(ckb1);
frm.add(textfield5);
frm.addWindowListener(new WindowAdapter()
{
public void windowClosing (WindowEvent Event) {
System.exit (0);
}
}
);
frm.setVisible(true);
}
static class ActListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
int a = Integer.parseInt(textfield1.getText().toString());
int b = Integer.parseInt(textfield2.getText().toString());
int c = Integer.parseInt(textfield3.getText().toString());
int d = Integer.parseInt(textfield4.getText().toString());
if (ckb1).isSelected()){
textfield5.setText(String.valueOf((a+b)*70+(c+b)*80)+30);
}
else {
textfield5.setText(String.valueOf((a+b)*70+(c+b)*80));
}
}
}
}
Please use ckb1.getState() instead of ckb1.isselected()
Please find the updated code working fine
static Frame frm = new Frame("Action Event");
static Checkbox ckb1 = new Checkbox("Delivery");
static Button btn = new Button("Calculate");
static TextField textfield1 = new TextField();
static TextField textfield2 = new TextField();
static TextField textfield3 = new TextField();
static TextField textfield4 = new TextField();
static TextField textfield5 = new TextField();
public static void main(String[] args) {
// TODO Auto-generated method stub
GridLayout grid = new GridLayout (7,3);
btn.addActionListener(new ActListener());
frm.setLayout(grid);
frm.setSize(400,200);
frm.setBackground(Color.pink);
Label lab1 = new Label ("Tea Series");
Font font1 = new Font(null,Font.BOLD,12);
lab1.setFont(font1);
frm.add(lab1);
frm.add(new Label(""));
frm.add(new Label(""));
frm.add(new Label("Black Tea"));
frm.add(new Label("$70"));
frm.add(textfield1);
frm.add(new Label("Green Tea"));
frm.add(new Label("$70"));
frm.add(textfield2);
Label lab2 = new Label ("Special Flavor Series");
lab2.setFont(font1);
frm.add(lab2);
frm.add(new Label(""));
frm.add(new Label(""));
frm.add(new Label("Alpine Tea"));
frm.add(new Label("$80"));
frm.add(textfield3);
frm.add(new Label("Stewed Oolong Tea"));
frm.add(new Label("$80"));
frm.add(textfield4);
frm.add(btn);
frm.add(ckb1);
frm.add(textfield5);
frm.addWindowListener(new WindowAdapter()
{
public void windowClosing (WindowEvent Event) {
System.exit (0);
}
}
);
frm.setVisible(true);
}
static class ActListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
int a = Integer.parseInt(textfield1.getText().toString());
int b = Integer.parseInt(textfield2.getText().toString());
int c = Integer.parseInt(textfield3.getText().toString());
int d = Integer.parseInt(textfield4.getText().toString());
if (ckb1.getState()){
int res=((a+b)*70+(c+b)*80)+30;
textfield5.setText(String.valueOf(res));
}
else {
int res=((a+b)*70+(c+b)*80);
textfield5.setText(String.valueOf(res));
}
}
}
you were concatinating final answer i.e delivery charge
for sample i have attached the output
and i have modified the code and find the screenshot for modified code
The compiler doesn't lie. In AWT there is no isSelected() method for CheckBox
You're looking for ckb1.getState()
https://docs.oracle.com/javase/7/docs/api/java/awt/Checkbox.html
looking at the API, it seems the Checkbox object doesn't have an isSelected method. Perhaps what you are looking for is getState(). It determines if the checkbox is in the on or off state.
try doing this:
ckb1.addItemListener(new ItemListener() {
#Override
public void itemStateChanged(ItemEvent e) {
if(e.getState()) {//checkbox has been selected
//do something...
} else {//checkbox has been deselected
//do something...
};
}
});
you first need to add the item listener and then get the state
getState() will return a boolean value and you can use the if-else to do your job :)
Swing and AWT checkboxes are not the same, while JCheckBox does actually have isSelected() method, the AWT counterpart does not.
Anyway, I'd suggest to use event for state change tracking instead of accessing an UI component directly, e.g.:
checkbox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
System.out.printf("%s\n", e.getStateChange() == 1 ? "checked" : "unchecked");
}
});

Related Links

EC2 UserData not working with Java SDK
JavaFX 8 set TextArea size not full
Spring security causing AngularJS to send GET requests instead of POST requests
Getting warning “The following references to java symbols could not be resolved.” when running Fortify maven plugin
Working with LLVM from Java
BufferedReader does not copy files
Generating intermediate vertices
Validating the Date format using selenium Webdriver
Get first number in comma separated string
javafx mac dmg deploy install location on Applications
How to set encoding of SOAP responses in CXF client?
Rotating tick mark labels to 45 degrees in JFreechart
Joining Array[Bytes] using Spark foreachPartition and ListBuffer
Thread running in debug mode, but doesn't work in real time
When maven dependency Version is whole number, exception while parsing
why Uiautomator crashes in getUiDevices()?

Categories

HOME
vim
osgi
gremlin
reserved
json.net
react-redux
youtube-api-v3
rdf
fsm
google-project-tango
swagger-ui
circular-dependency
visual-studio-cordova
hapi
decomposition
decimal
carthage
jsprit
django-simple-history
opentracing
google-cloud-speech
google-pagespeed
plunker
ml
errorlevel
css-animations
bootstrap-tour
fgetcsv
maquette
dbclient
facebook-apps
windowbuilder
gitignore
fabric8
iframe-resizer
ecto
textmate
jmonkeyengine
simplexml
jquery-bootgrid
vapor
webix-treetable
android-fingerprint-api
glew
sencha-touch-2.3
btrace
logfiles
long-polling
jxcore
windows-iot-core-10
sqldf
nbconvert
dwscript
log4c
paypal-express
moveit
pickadate
yaws
apachebench
ptrace
energy
spim
prerequisites
0xdbe
service-accounts
sysinternals
sortedlist
system32
clipperlib
block-device
camanjs
java.util.concurrent
asp.net-web-api-odata
internet-connection
eol
jquery-layout
system.net.webexception
valuechangelistener
concurrent-collections
mbr
html5-notifications
c18
sitemesh
ftps
buster.js
trusted
netdna-api
whoosh
gnu-prolog
window-management
joyent
psi
f#-powerpack
rfc1123
file-comparison
yslow
sproutcore-2
thunderbird-lightning
fixed-width
substrings
brewmp
kdbg
javap
yagni
procedural-music
geneva-server
data-acquisition
paul-graham
ntvdm.exe

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App