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

webview Hide or Replace “webpage not available” when offline
Java Swing or SWT method to keep a frame in the background
Java Array Index Out of Bounds Exception No matter the array length
How to remove special characters from with in a column of a csv file in java
SSLContext and PKCS12 keystore - works on JVM but not on Android
Kryo - How to keep the server running and waiting for requests
How to get Location with Wifi in Android?
Compare first two decimal points
How to open dialog window to edit user in jsf page
Spinner inside ActionBarCompact
Java log file parser on Windows 2008 is not able to use File.lastModified
How to turn an number into a roman numeral? [duplicate]
jsp populating object array
Possible to make an XML template for Android display different numbers of buttons?
Refreshing the table columns in jsp page on click of refresh button at each row using ajax/javascript
Best way to separate javascript and java in jsp

Categories

HOME
push-notification
fft
office365api
bookshelf.js
leon
sharepoint-designer
constraint-programming
add
gorm
floating-action-button
alignment
commonmark
dtrace
fallback
carthage
django-simple-history
clojurescript
opentracing
normalizr
excel-vba-mac
viewport
emulator
reverse-proxy
csrf-protection
vlsi
javacv
sparse-matrix
facebook-access-token
fgetcsv
rst2pdf
fluentvalidation
jaxb2
windowbuilder
procdump
uisplitview
pdb
winrt-xaml-toolkit
perlin-noise
openoffice.org
automake
espeak
neuroscience
theano.scan
leading-zero
particles.js
botbuilder
fakeiteasy
avconv
jquery-nestable
datastax-startup
jxcore
namecoin
python-cryptography
webdriverjs
chord-diagram
pycaffe
objective-c-swift-bridge
watchconnectivity
url-pattern
savon
yaws
citrus-pay
browser-bugs
iiviewdeckcontroller
key-management
graph-api-explorer
formatjs
gwidgets
cctv
wordpress-theme-customize
yorick
bundles
resty-gwt
jplaton
broken-links
genshi
django-tagging
telerik-scheduler
gamma
filtered-index
rescale
user-friendly
zend-tool
brewmp
backcolor
data-driven
virtual-functions
swing-app-framework
phonon
windows-live-messenger

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