eugene007
asked on
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.NOR TH);
cp.add(p2,BorderLayout.CEN TER);
cp.add(p3,BorderLayout.SOU TH);
}
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()==It emEvent.SE LECTED)
{
c1.setEnabled(false);
c2.setEnabled(false);
c3.setEnabled(false);
c4.setEnabled(false);
lb1.setText(r1.getText());
}
}
if(ie.getItemSelectable()= =r2)
{
if(ie.getStateChange()==It emEvent.SE LECTED)
{
c1.setEnabled(true);
c2.setEnabled(true);
c3.setEnabled(true);
c4.setEnabled(true);
lb1.setText(r2.getText());
}
}
if(ie.getItemSelectable()= =c1)
{
if(ie.getStateChange()==It emEvent.SE LECTED)
{
lb1.setText(lb1.getText(). concat(" " + c1.getText()));
}
}
if(ie.getItemSelectable()= =c2)
{
if(ie.getStateChange()==It emEvent.SE LECTED)
{
lb1.setText(lb1.getText(). concat(" " + c2.getText()));
}
}
if(ie.getItemSelectable()= =c3)
{
if(ie.getStateChange()==It emEvent.SE LECTED)
{
lb1.setText(lb1.getText(). concat(" " + c3.getText()));
}
}
if(ie.getItemSelectable()= =c4)
{
if(ie.getStateChange()==It emEvent.SE LECTED)
{
lb1.setText(lb1.getText(). concat(" " + c4.getText()));
}
}
}
}
How is it done?.
Your help is kindly appreciated.
Regards
Eugene
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.NOR
cp.add(p2,BorderLayout.CEN
cp.add(p3,BorderLayout.SOU
}
public static void main(String[] args)
{
JFrame j = new Room();
j.setVisible(true);
}
public void itemStateChanged(ItemEvent
{
if(ie.getItemSelectable()=
{
if(ie.getStateChange()==It
{
c1.setEnabled(false);
c2.setEnabled(false);
c3.setEnabled(false);
c4.setEnabled(false);
lb1.setText(r1.getText());
}
}
if(ie.getItemSelectable()=
{
if(ie.getStateChange()==It
{
c1.setEnabled(true);
c2.setEnabled(true);
c3.setEnabled(true);
c4.setEnabled(true);
lb1.setText(r2.getText());
}
}
if(ie.getItemSelectable()=
{
if(ie.getStateChange()==It
{
lb1.setText(lb1.getText().
}
}
if(ie.getItemSelectable()=
{
if(ie.getStateChange()==It
{
lb1.setText(lb1.getText().
}
}
if(ie.getItemSelectable()=
{
if(ie.getStateChange()==It
{
lb1.setText(lb1.getText().
}
}
if(ie.getItemSelectable()=
{
if(ie.getStateChange()==It
{
lb1.setText(lb1.getText().
}
}
}
}
How is it done?.
Your help is kindly appreciated.
Regards
Eugene
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> remove options when checkboxes are deselected
Not needed if you build up the label's text **every** time a(nother) check box is selected
Not needed if you build up the label's text **every** time a(nother) check box is selected
thanks
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