alrav79
asked on
Help on an applet
I'm trying to make an applet with checkboxes and it would have many different comibinations of if statements to get this done for the appropriate boxes checked, but I am faced with two problems. First, the if statements get really long because I have to indicate not only which box is checked, but also which one is not for it to work properly (at least from my way of doing it). Is there a way to reduce this?
Also, since the statements are really long, so is my CheckBoxListener class. Is there a way to make this a seprate class in order to make my applet class shorter and easier to read? Here's my code. By the way, any other pionters would be much obliged.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class WarApplet extends JApplet {
JCheckBox englandButton = new JCheckBox("England");
JCheckBox franceButton = new JCheckBox("France");
JCheckBox russiaButton = new JCheckBox("Russia");
JCheckBox unitedStatesButton = new JCheckBox("United States");
JTextArea txt = new JTextArea("Please choose a country.",10, 10);
public void init() {
englandButton.setSelected( false);
franceButton.setSelected(f alse);
russiaButton.setSelected(f alse);
unitedStatesButton.setSele cted(false );
Container cp = getContentPane();
JPanel checkPanel = new JPanel();
checkPanel.setLayout(new GridLayout(0,1));
checkPanel.add(englandButt on);
checkPanel.add(franceButto n);
checkPanel.add(russiaButto n);
checkPanel.add(unitedState sButton);
cp.add(checkPanel, BorderLayout.WEST);
txt.setLineWrap(true);
txt.setWrapStyleWord(true) ;
txt.setEditable(false);
cp.add(txt, BorderLayout.CENTER);
englandButton.addItemListe ner(new CheckBoxListener());
franceButton.addItemListen er(new CheckBoxListener());
russiaButton.addItemListen er(new CheckBoxListener());
unitedStatesButton.addItem Listener(n ew CheckBoxListener());
}
class CheckBoxListener implements ItemListener {
public void itemStateChanged(ItemEvent e) {
if (englandButton.isSelected( ) &&
!franceButton.isSelected() &&
!russiaButton.isSelected() &&
!unitedStatesButton.isSele cted())
txt.setText(WarInfo.info[0 ]);
else if (!englandButton.isSelected () &&
franceButton.isSelected() &&
!russiaButton.isSelected() &&
!unitedStatesButton.isSele cted())
txt.setText(WarInfo.info[1 ]);
else if (!englandButton.isSelected () &&
!franceButton.isSelected() &&
russiaButton.isSelected() &&
!unitedStatesButton.isSele cted())
txt.setText(WarInfo.info[2 ]);
else if (!englandButton.isSelected () &&
!franceButton.isSelected() &&
!russiaButton.isSelected() &&
unitedStatesButton.isSelec ted())
txt.setText(WarInfo.info[3 ]);
else if (englandButton.isSelected( ) &&
franceButton.isSelected() &&
russiaButton.isSelected() &&
unitedStatesButton.isSelec ted())
txt.setText(WarInfo.info[4 ]);
else if (!englandButton.isSelected () &&
franceButton.isSelected() &&
!russiaButton.isSelected() &&
unitedStatesButton.isSelec ted())
txt.setText(WarInfo.info[5 ]);
else if (!englandButton.isSelected () &&
!franceButton.isSelected() &&
russiaButton.isSelected() &&
unitedStatesButton.isSelec ted())
txt.setText(WarInfo.info[6 ]);
else if (englandButton.isSelected( ) &&
franceButton.isSelected() &&
!russiaButton.isSelected() &&
!unitedStatesButton.isSele cted())
txt.setText(WarInfo.info[7 ]);
else if (englandButton.isSelected( ) &&
!franceButton.isSelected() &&
!russiaButton.isSelected() &&
unitedStatesButton.isSelec ted())
txt.setText(WarInfo.info[8 ]);
else if (englandButton.isSelected( ) &&
franceButton.isSelected() &&
russiaButton.isSelected() &&
!unitedStatesButton.isSele cted())
txt.setText(WarInfo.info[9 ]);
else txt.setText("Please choose a country.");
}
}
}
Also, since the statements are really long, so is my CheckBoxListener class. Is there a way to make this a seprate class in order to make my applet class shorter and easier to read? Here's my code. By the way, any other pionters would be much obliged.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class WarApplet extends JApplet {
JCheckBox englandButton = new JCheckBox("England");
JCheckBox franceButton = new JCheckBox("France");
JCheckBox russiaButton = new JCheckBox("Russia");
JCheckBox unitedStatesButton = new JCheckBox("United States");
JTextArea txt = new JTextArea("Please choose a country.",10, 10);
public void init() {
englandButton.setSelected(
franceButton.setSelected(f
russiaButton.setSelected(f
unitedStatesButton.setSele
Container cp = getContentPane();
JPanel checkPanel = new JPanel();
checkPanel.setLayout(new GridLayout(0,1));
checkPanel.add(englandButt
checkPanel.add(franceButto
checkPanel.add(russiaButto
checkPanel.add(unitedState
cp.add(checkPanel, BorderLayout.WEST);
txt.setLineWrap(true);
txt.setWrapStyleWord(true)
txt.setEditable(false);
cp.add(txt, BorderLayout.CENTER);
englandButton.addItemListe
franceButton.addItemListen
russiaButton.addItemListen
unitedStatesButton.addItem
}
class CheckBoxListener implements ItemListener {
public void itemStateChanged(ItemEvent
if (englandButton.isSelected(
!franceButton.isSelected()
!russiaButton.isSelected()
!unitedStatesButton.isSele
txt.setText(WarInfo.info[0
else if (!englandButton.isSelected
franceButton.isSelected() &&
!russiaButton.isSelected()
!unitedStatesButton.isSele
txt.setText(WarInfo.info[1
else if (!englandButton.isSelected
!franceButton.isSelected()
russiaButton.isSelected() &&
!unitedStatesButton.isSele
txt.setText(WarInfo.info[2
else if (!englandButton.isSelected
!franceButton.isSelected()
!russiaButton.isSelected()
unitedStatesButton.isSelec
txt.setText(WarInfo.info[3
else if (englandButton.isSelected(
franceButton.isSelected() &&
russiaButton.isSelected() &&
unitedStatesButton.isSelec
txt.setText(WarInfo.info[4
else if (!englandButton.isSelected
franceButton.isSelected() &&
!russiaButton.isSelected()
unitedStatesButton.isSelec
txt.setText(WarInfo.info[5
else if (!englandButton.isSelected
!franceButton.isSelected()
russiaButton.isSelected() &&
unitedStatesButton.isSelec
txt.setText(WarInfo.info[6
else if (englandButton.isSelected(
franceButton.isSelected() &&
!russiaButton.isSelected()
!unitedStatesButton.isSele
txt.setText(WarInfo.info[7
else if (englandButton.isSelected(
!franceButton.isSelected()
!russiaButton.isSelected()
unitedStatesButton.isSelec
txt.setText(WarInfo.info[8
else if (englandButton.isSelected(
franceButton.isSelected() &&
russiaButton.isSelected() &&
!unitedStatesButton.isSele
txt.setText(WarInfo.info[9
else txt.setText("Please choose a country.");
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Yes, you'll need to pass reference to the relevant objects or provide methods to perform required functionality.
The same way ANY classes would interact.
ASKER
ok, I now put the classes in their own appropriate files, and both of them compile perfectly. But when the applet loads, it acts as if the if statements arent there. No matter what i check, there is no response. I passed reference by making a WarApplet object "a" in the CheckBoxListener class, like this:
WarApplet a = new WarApplet();
...
...
..
if (a.england.isSelected())
..
you get the point
WarApplet a = new WarApplet();
...
...
..
if (a.england.isSelected())
..
you get the point
A more elegant way to do this would be to take a bit masking approach. You'll find if you create an array of checkboxes, this may help in connection with this way of doing things.
final static int FRANCE = 1 << 0;
final static int ENGLAND = 1 << 1;
...
final static int FRANCE_ENGLAND = ENGLAND | FRANCE;
final static int FRANCE = 1 << 0;
final static int ENGLAND = 1 << 1;
...
final static int FRANCE_ENGLAND = ENGLAND | FRANCE;
You need to pass a reference of WarApplet to you listener class, not create a new one.
ASKER
explain how please with an example
public CheckBoxListener(WarApplet applet)
{
a = applet;
}
{
a = applet;
}
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Answered by: objects
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Venci75
EE Cleanup Volunteer
I will leave a recommendation in the Cleanup topic area that this question is:
Answered by: objects
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Venci75
EE Cleanup Volunteer
per recommendation
SpideyMod
Community Support Moderator @Experts Exchange
SpideyMod
Community Support Moderator @Experts Exchange
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class WarApplet extends JApplet {
JCheckBox englandButton = new JCheckBox("England");
JCheckBox franceButton = new JCheckBox("France");
JCheckBox russiaButton = new JCheckBox("Russia");
JCheckBox unitedStatesButton = new JCheckBox("United States");
JTextArea txt = new JTextArea("Please choose a country.",10, 10);
public void init() {
englandButton.setSelected(
franceButton.setSelected(f
russiaButton.setSelected(f
unitedStatesButton.setSele
Container cp = getContentPane();
JPanel checkPanel = new JPanel();
checkPanel.setLayout(new GridLayout(0,1));
checkPanel.add(englandButt
checkPanel.add(franceButto
checkPanel.add(russiaButto
checkPanel.add(unitedState
cp.add(checkPanel, BorderLayout.WEST);
txt.setLineWrap(true);
txt.setWrapStyleWord(true)
txt.setEditable(false);
cp.add(txt, BorderLayout.CENTER);
englandButton.addItemListe
franceButton.addItemListen
russiaButton.addItemListen
unitedStatesButton.addItem
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CheckBoxListener implements ItemListener {
public void itemStateChanged(ItemEvent
if (englandButton.isSelected(
!franceButton.isSelected()
!russiaButton.isSelected()
!unitedStatesButton.isSele
txt.setText(WarInfo.info[0
else if (!englandButton.isSelected
franceButton.isSelected() &&
!russiaButton.isSelected()
!unitedStatesButton.isSele
txt.setText(WarInfo.info[1
else if (!englandButton.isSelected
!franceButton.isSelected()
russiaButton.isSelected() &&
!unitedStatesButton.isSele
txt.setText(WarInfo.info[2
else if (!englandButton.isSelected
!franceButton.isSelected()
!russiaButton.isSelected()
unitedStatesButton.isSelec
txt.setText(WarInfo.info[3
else if (englandButton.isSelected(
franceButton.isSelected() &&
russiaButton.isSelected() &&
unitedStatesButton.isSelec
txt.setText(WarInfo.info[4
else if (!englandButton.isSelected
franceButton.isSelected() &&
!russiaButton.isSelected()
unitedStatesButton.isSelec
txt.setText(WarInfo.info[5
else if (!englandButton.isSelected
!franceButton.isSelected()
russiaButton.isSelected() &&
unitedStatesButton.isSelec
txt.setText(WarInfo.info[6
else if (englandButton.isSelected(
franceButton.isSelected() &&
!russiaButton.isSelected()
!unitedStatesButton.isSele
txt.setText(WarInfo.info[7
else if (englandButton.isSelected(
!franceButton.isSelected()
!russiaButton.isSelected()
unitedStatesButton.isSelec
txt.setText(WarInfo.info[8
else if (englandButton.isSelected(
franceButton.isSelected() &&
russiaButton.isSelected() &&
!unitedStatesButton.isSele
txt.setText(WarInfo.info[9
else txt.setText("Please choose a country.");
}
}