Solved

Help on an applet

Posted on 2002-04-05
11
161 Views
Last Modified: 2013-11-23
   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(false);
      russiaButton.setSelected(false);
      unitedStatesButton.setSelected(false);
      
      Container cp = getContentPane();

      JPanel checkPanel = new JPanel();
      checkPanel.setLayout(new GridLayout(0,1));
      checkPanel.add(englandButton);
      checkPanel.add(franceButton);
      checkPanel.add(russiaButton);      
      checkPanel.add(unitedStatesButton);
      cp.add(checkPanel, BorderLayout.WEST);

      txt.setLineWrap(true);
      txt.setWrapStyleWord(true);
      txt.setEditable(false);
      cp.add(txt, BorderLayout.CENTER);
      
      englandButton.addItemListener(new CheckBoxListener());
      franceButton.addItemListener(new CheckBoxListener());
      russiaButton.addItemListener(new CheckBoxListener());
      unitedStatesButton.addItemListener(new CheckBoxListener());
    }

    class CheckBoxListener implements ItemListener {
      public void itemStateChanged(ItemEvent e) {
          if (englandButton.isSelected() &&
            !franceButton.isSelected() &&
            !russiaButton.isSelected() &&
            !unitedStatesButton.isSelected())
            txt.setText(WarInfo.info[0]);
          else if (!englandButton.isSelected() &&
                 franceButton.isSelected() &&
                 !russiaButton.isSelected() &&
                 !unitedStatesButton.isSelected())
            txt.setText(WarInfo.info[1]);
          else if (!englandButton.isSelected() &&
                 !franceButton.isSelected() &&
                 russiaButton.isSelected() &&
                 !unitedStatesButton.isSelected())
            txt.setText(WarInfo.info[2]);
          else if (!englandButton.isSelected() &&
                 !franceButton.isSelected() &&
                 !russiaButton.isSelected() &&
                 unitedStatesButton.isSelected())
            txt.setText(WarInfo.info[3]);
          else if (englandButton.isSelected() &&
                 franceButton.isSelected() &&
                 russiaButton.isSelected() &&
                 unitedStatesButton.isSelected())
            txt.setText(WarInfo.info[4]);
          else if (!englandButton.isSelected() &&
                 franceButton.isSelected() &&
                 !russiaButton.isSelected() &&
                 unitedStatesButton.isSelected())
            txt.setText(WarInfo.info[5]);
          else if (!englandButton.isSelected() &&
                 !franceButton.isSelected() &&
                 russiaButton.isSelected() &&
                 unitedStatesButton.isSelected())
            txt.setText(WarInfo.info[6]);
          else if (englandButton.isSelected() &&
                 franceButton.isSelected() &&
                 !russiaButton.isSelected() &&
                 !unitedStatesButton.isSelected())
            txt.setText(WarInfo.info[7]);
          else if (englandButton.isSelected() &&
                 !franceButton.isSelected() &&
                 !russiaButton.isSelected() &&
                 unitedStatesButton.isSelected())
            txt.setText(WarInfo.info[8]);
          else if (englandButton.isSelected() &&
                 franceButton.isSelected() &&
                 russiaButton.isSelected() &&
                 !unitedStatesButton.isSelected())
            txt.setText(WarInfo.info[9]);
          else txt.setText("Please choose a country.");
      }
    }
   
}

   
0
Comment
Question by:alrav79
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 6922445
>  Is there a way to make this a seprate class

It actually already is a seperate class :-) It's what's called an inner class.
You are free to make it a non-inner class, just move it into it's own .java file CheckBoxListener.java.

It's not entirely clear what you're trying to do, so I can't really comment on improvements to your logic.

A minor side note, you don't actually appear to need seperate instance of your listener for each cb, one would suffice.
0
 

Expert Comment

by:cini_gm
ID: 6922475
This way u can break this class. Save the two classes in the same directory and compile it

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(false);
     russiaButton.setSelected(false);
     unitedStatesButton.setSelected(false);
     
     Container cp = getContentPane();

     JPanel checkPanel = new JPanel();
     checkPanel.setLayout(new GridLayout(0,1));
     checkPanel.add(englandButton);
     checkPanel.add(franceButton);
     checkPanel.add(russiaButton);    
     checkPanel.add(unitedStatesButton);
     cp.add(checkPanel, BorderLayout.WEST);

     txt.setLineWrap(true);
     txt.setWrapStyleWord(true);
     txt.setEditable(false);
     cp.add(txt, BorderLayout.CENTER);
     
     englandButton.addItemListener(new CheckBoxListener());
     franceButton.addItemListener(new CheckBoxListener());
     russiaButton.addItemListener(new CheckBoxListener());
     unitedStatesButton.addItemListener(new CheckBoxListener());
   }
}

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class CheckBoxListener implements ItemListener {
     public void itemStateChanged(ItemEvent e) {
         if (englandButton.isSelected() &&
          !franceButton.isSelected() &&
          !russiaButton.isSelected() &&
          !unitedStatesButton.isSelected())
          txt.setText(WarInfo.info[0]);
         else if (!englandButton.isSelected() &&
               franceButton.isSelected() &&
               !russiaButton.isSelected() &&
               !unitedStatesButton.isSelected())
          txt.setText(WarInfo.info[1]);
         else if (!englandButton.isSelected() &&
               !franceButton.isSelected() &&
               russiaButton.isSelected() &&
               !unitedStatesButton.isSelected())
          txt.setText(WarInfo.info[2]);
         else if (!englandButton.isSelected() &&
               !franceButton.isSelected() &&
               !russiaButton.isSelected() &&
               unitedStatesButton.isSelected())
          txt.setText(WarInfo.info[3]);
         else if (englandButton.isSelected() &&
               franceButton.isSelected() &&
               russiaButton.isSelected() &&
               unitedStatesButton.isSelected())
          txt.setText(WarInfo.info[4]);
         else if (!englandButton.isSelected() &&
               franceButton.isSelected() &&
               !russiaButton.isSelected() &&
               unitedStatesButton.isSelected())
          txt.setText(WarInfo.info[5]);
         else if (!englandButton.isSelected() &&
               !franceButton.isSelected() &&
               russiaButton.isSelected() &&
               unitedStatesButton.isSelected())
          txt.setText(WarInfo.info[6]);
         else if (englandButton.isSelected() &&
               franceButton.isSelected() &&
               !russiaButton.isSelected() &&
               !unitedStatesButton.isSelected())
          txt.setText(WarInfo.info[7]);
         else if (englandButton.isSelected() &&
               !franceButton.isSelected() &&
               !russiaButton.isSelected() &&
               unitedStatesButton.isSelected())
          txt.setText(WarInfo.info[8]);
         else if (englandButton.isSelected() &&
               franceButton.isSelected() &&
               russiaButton.isSelected() &&
               !unitedStatesButton.isSelected())
          txt.setText(WarInfo.info[9]);
         else txt.setText("Please choose a country.");
     }
   }
   

0
 
LVL 92

Expert Comment

by:objects
ID: 6922483
Yes, you'll need to pass reference to the relevant objects or provide methods to perform required functionality.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 6922484
The same way ANY classes would interact.
0
 

Author Comment

by:alrav79
ID: 6922597
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 6923064
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;


0
 
LVL 92

Expert Comment

by:objects
ID: 6923374
You need to pass a reference of WarApplet to you listener class, not create a new one.
0
 

Author Comment

by:alrav79
ID: 6924624
explain how please with an example
0
 
LVL 92

Expert Comment

by:objects
ID: 6924636
public CheckBoxListener(WarApplet applet)
{
   a = applet;
}
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7933256
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
0
 

Expert Comment

by:SpideyMod
ID: 7984190
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to see all occupied ports on windows 10 laptop 15 82
ejb example issues 3 47
import as existing maven project 3 34
netbeans can not run self signed app 8 7
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question