• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1363
  • Last Modified:

concat

The code bellow contains radio buttons as well as checkbox controls. When the components are selected the name of the components are obtained and appended and displayed.

For example:

I have 2 radio buttons name "Standard" and "Deluxe".
When I click on "Standard", the name "Standard" will be displayed in my label control.

Then I have about 5 checkbox control. For example if I select "Internet Connection", the word "Standard Internet connection" will be displayed. And when I select "Cable TV", the word "Standard Internet connection Cable TV" will be displayed on my label control.

The problem I am facing now is to add the word "with"
in for example:

"Standard with Internet connection Cable TV"

or

"Delux with Internet connection Cable TV"


only one "with" word should appear on my string.



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

public class Room extends JFrame implements ItemListener
{
         JRadioButton r1,r2;
         JCheckBox c1,c2,c3,c4;
         JLabel lb1;
         ButtonGroup bg;
        
         public Room()
         {
                   Container cp = getContentPane();
                   setTitle("RoomInfo");
                   setSize(500,200);
                   cp.setLayout(new BorderLayout());
                   
                   Panel p1 = new Panel(new FlowLayout());
                   r1 = new JRadioButton("Standard Room");
                   r2 = new JRadioButton("Deluxe Room");
                   bg = new ButtonGroup();
                   bg.add(r1);
                   bg.add(r2);
                   p1.add(r1);
                   p1.add(r2);
                   
                   r1.addItemListener(this);
                   r2.addItemListener(this);
                   
                   Panel p2 = new Panel(new GridLayout(0,1));
                   c1 = new JCheckBox("Internet Connection");
                   c2 = new JCheckBox("Cable TV");
                   c3 = new JCheckBox("Microwave");
                   c4 = new JCheckBox("Refrigerator");
                   p2.add(c1);
                   p2.add(c2);
                   p2.add(c3);
                   p2.add(c4);
                   
                   c1.setEnabled(false);
                   c2.setEnabled(false);
                   c3.setEnabled(false);
                   c4.setEnabled(false);
                   
                   c1.addItemListener(this);
                   c2.addItemListener(this);
                   c3.addItemListener(this);
                   c4.addItemListener(this);
                   
                   Panel p3 = new Panel(new FlowLayout());
                   lb1 = new JLabel("");
                   p3.add(lb1);
                   
                   cp.add(p1,BorderLayout.NORTH);
                   cp.add(p2,BorderLayout.CENTER);       
                   cp.add(p3,BorderLayout.SOUTH);                             
         }
        
         public static void main(String[] args)
         {
              JFrame j = new Room();
              j.setVisible(true);             
         }
        
         public void itemStateChanged(ItemEvent ie)
         {
                   if(ie.getItemSelectable()==r1)
                   {
                        if(ie.getStateChange()==ItemEvent.SELECTED)
                        {
                               c1.setEnabled(false);
                               c2.setEnabled(false);
                               c3.setEnabled(false);
                               c4.setEnabled(false);
                               lb1.setText(r1.getText());
                        }      
                   }
                   
                   if(ie.getItemSelectable()==r2)
                   {
                          if(ie.getStateChange()==ItemEvent.SELECTED)
                        {
                               c1.setEnabled(true);
                               c2.setEnabled(true);
                               c3.setEnabled(true);
                               c4.setEnabled(true);
                               lb1.setText(r2.getText());
                        }      
                   }
                   
                   if(ie.getItemSelectable()==c1)
                   {
                          if(ie.getStateChange()==ItemEvent.SELECTED)
                        {
                               lb1.setText(lb1.getText().concat(" " + c1.getText()));
                        }
                   }
                   
                   
                   if(ie.getItemSelectable()==c2)
                   {
                          if(ie.getStateChange()==ItemEvent.SELECTED)
                        {
                     lb1.setText(lb1.getText().concat(" " + c2.getText()));
                        }
                   }
                   
                   if(ie.getItemSelectable()==c3)
                   {
                          if(ie.getStateChange()==ItemEvent.SELECTED)
                        {
                     lb1.setText(lb1.getText().concat(" " + c3.getText()));
                        }
                   }
                   
                   
                   if(ie.getItemSelectable()==c4)
                   {
                          if(ie.getStateChange()==ItemEvent.SELECTED)
                        {
                     lb1.setText(lb1.getText().concat(" " + c4.getText()));
                        }
                   }
         }
}


How is it done?.

Your help is kindly appreciated.

Regards

Eugene
0
eugene007
Asked:
eugene007
  • 3
  • 2
1 Solution
 
CEHJCommented:
I wouldn't bother with 'with'. Your options will potentially grow long enough anyway without that. A more pressing need is to remove options when checkboxes are deselected, which you currently haven't implemented
0
 
zzynxSoftware engineerCommented:
Why don't you build up the text again every time a check box is selected.

If "Internet Connection" is selected:

StringBuffer labelText = new StringBuffer();
if (standardRadioButton.isSelected())
   labelText.append("Standard");
else
   labelText.append("Deluxe");

if (internetConnectionCheckBox.isSelected())
  labelText.append(" with Internet Connection");
else...
0
 
zzynxSoftware engineerCommented:
>> remove options when checkboxes are deselected
Not needed if you build up the label's text **every** time a(nother) check box is selected
0
 
zzynxSoftware engineerCommented:
thanks
0
 
CEHJCommented:
Your next question reveals why the suggested approach is not a good one - you've ignored the consideration i mentioned earlier. What you should do is iterate a button group, having added the boxes with captions to them (clue: the captions can be set as the components' names) and collect the text *irrespective* of whether a selection or deselection has occurred
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now