Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    Izuba is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default How do i display a list of strings on one JOptionPane.showMessageDialog?

    I just want one JOptionPane.showMessageDialog to display all the user's selections and in case there is no selection but the user presses the button, i want a message of this kind to be displayed:"please make a selection"
    could you please help me do so?here follows my source code:

    Java Code:
    //file: DriveThrough.java
    import java.awt.*;
    import java.awt.event.*;
     
    import javax.swing.*;
     
    public class DriveThrough {
      public static void main(String[] args) {
        JFrame f = new JFrame("Lister v1.0");
        f.setSize(300, 150);
        f.setLocation(200, 200);
        f.addWindowListener(new WindowAdapter(  ) {
          public void windowClosing(WindowEvent we) { System.exit(0); }
        });
     
        JPanel entreePanel = new JPanel(  );
        final ButtonGroup entreeGroup = new ButtonGroup(  );
        JRadioButton radioButton;
        entreePanel.add(radioButton = new JRadioButton("Beef"));
        radioButton.setActionCommand("Beef");
        entreeGroup.add(radioButton);
        entreePanel.add(radioButton = new JRadioButton("Chicken"));
        radioButton.setActionCommand("Chicken");
        entreeGroup.add(radioButton);
        entreePanel.add(radioButton = new JRadioButton("Veggie"));
        radioButton.setActionCommand("Veggie");
        entreeGroup.add(radioButton);
        
        final JPanel condimentsPanel = new JPanel(  );
        condimentsPanel.add(new JCheckBox("Ketchup"));
        condimentsPanel.add(new JCheckBox("Mustard"));
        condimentsPanel.add(new JCheckBox("Pickles"));
        
        JPanel orderPanel = new JPanel(  );
        JButton orderButton = new JButton("Place Order");
        orderPanel.add(orderButton);
        
        Container content = f.getContentPane(  );
        content.setLayout(new GridLayout(3, 1));
        content.add(entreePanel);
        content.add(condimentsPanel);
        content.add(orderPanel);
        
        orderButton.addActionListener(new ActionListener(  ) {
          public void actionPerformed(ActionEvent ae) {
            String entree = 
              entreeGroup.getSelection().getActionCommand(  );
            JOptionPane.showMessageDialog,(null,entree + " sandwich");
            Component[] components = condimentsPanel.getComponents(  );
            for (int i = 0; i < components.length; i++) {
              JCheckBox cb = (JCheckBox)components[i];
              if (cb.isSelected(  ))
                JOptionPane.showMessageDialog(null,"With " + cb.getText(  ));
            }
          }
        });
        
        f.setVisible(true);
      }
    }
    Last edited by Fubarable; 10-09-2010 at 01:41 PM. Reason: Moderator Edit: Code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Izuba, please do not send private messages regarding questions that should remain in the forum.

    Has anyone else received private messages from Izuba?

    Note that I will delete my question here and its replies in a little bit.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    To solve your problem, it may help to make your JRadioButtons and JCheckBoxes fields of the class (in this situation, static fields), and then you can more easily query them from within your ActionListener's actionPerformed method. I would create a String in actionPerformed and then build it depending on the responses found in the check boxes and radio buttons.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,656
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Fubarable View Post
    Izuba, please do not send private messages regarding questions that should remain in the forum.

    Has anyone else received private messages from Izuba?
    Yup, a private message as well as a few visitor messages; I threw them away.

    kind regards,

    Jos

  5. #5
    Izuba is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default

    I am so sorry about that Sirs.I won't repeat that in the future.
    @Fubarable: I am not sure I got your point very well.could you please be a little bit clearer??thank you for your understanding
    @JoASH: i am sorry.could you still contribute please??

  6. #6
    Izuba is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default

    @Fubarable:could you please illustrate the point you have just brought out so as i could get it properly???

  7. #7
    Izuba is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default

    @Fubarable:could you please illustrate the point you have just brought out so as i could get it properly???

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,656
    Blog Entries
    7
    Rep Power
    21

    Default

    The 'message' part in the JOptionPane doesn't have to be a String (altough most of the time it is); it can be another (J)Component; that other (J)Component can contain everything you want, also a list of Strings in, say, a JList or whatever.

    kind regards,

    Jos

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Actually, ignore my suggestion above as you're getting the results from your checkboxes and radiobuttons. Instead create a String say, resultString, at the beginning of your actionPerfomed, and instead of displaying a bunch of JOptionPanes, concatenate your String with your results:
    Java Code:
                   if (cb.isSelected()) {
                      //JOptionPane.showMessageDialog(null, "With " + cb.getText());
                      resultString += "With " + cb.getText() + "\n";
                   }
    Then display the resultString at the end (as Jos recommends as well).

  10. #10
    Izuba is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default

    i have tried to edit the previous code a bit by removing the JRadioButtons and by putting a line between the design and the event code.I can't still have the effect that i want.I tried to display the items according to the selection but, even if i select two items, only the 1st item is display...here is the edited source code:

    //file: DriveThrough.java
    import java.awt.*;
    import java.awt.event.*;

    import javax.swing.*;

    public class Drive extends JFrame
    {

    final JPanel condimentsPanel = new JPanel();
    JPanel orderPanel = new JPanel( );
    JButton orderButton = new JButton("Place Order");
    JCheckBox checkBox1 = new JCheckBox("Ketchup");
    JCheckBox checkBox2 = new JCheckBox("Mustard");
    JCheckBox checkBox3 = new JCheckBox("Pickles");


    public static void main(String args[])
    {
    Drive f = new Drive();
    f.setLocation(200,200);
    f.setSize(300,150);

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);

    }

    public Drive()
    {
    super();
    Container content = getContentPane( );

    condimentsPanel.add(checkBox1);
    condimentsPanel.add(checkBox2);
    condimentsPanel.add(checkBox3);


    orderPanel.add(orderButton);


    content.setLayout(new GridLayout(2, 1));
    content.add(condimentsPanel);
    content.add(orderPanel);
    ButtonHandler handler = new ButtonHandler();
    orderButton.addActionListener(handler);

    }
    private class ButtonHandler implements ActionListener
    {
    Component[] components = condimentsPanel.getComponents( );
    JCheckBox cb1 = (JCheckBox)components[0];
    JCheckBox cb2 = (JCheckBox)components[1];
    JCheckBox cb3 = (JCheckBox)components[2];

    public void actionPerformed(ActionEvent event)
    {

    if (cb1.isSelected() )
    JOptionPane.showMessageDialog(null,cb1.getText());
    else
    if(cb2.isSelected())
    JOptionPane.showMessageDialog(null,cb2.getText());
    else
    if(cb3.isSelected())
    JOptionPane.showMessageDialog(null,cb3.getText());
    else
    if( cb1.isSelected() & cb2.isSelected())
    JOptionPane.showMessageDialog(null,cb1.getText() + cb2.getText());
    else
    if(cb1.isSelected() & cb3.isSelected())
    JOptionPane.showMessageDialog(null,cb1.getText() + cb3.getText());
    else
    if(cb2.isSelected() & cb3.isSelected())
    JOptionPane.showMessageDialog(null,cb2.getText() + cb3.getText());
    else if(cb1.isSelected())
    if(cb2.isSelected())
    if(cb3.isSelected())
    JOptionPane.showMessageDialog(null,cb1.getText() + cb2.getText()

    +cb3.getText());
    else
    JOptionPane.showMessageDialog(null,"Please make a selection");
    }
    }

    }

  11. #11
    Izuba is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default

    I have edited a bit the previous code by removing the JRadioButtons and putting a line between the design code and event code.when I am selectind two items,only one is displayed,the same case when i am selecting all of them(only the 1st item is selected)yet i do want all selected items to be displayed.
    here follows the new code:

    //file: DriveThrough.java
    import java.awt.*;
    import java.awt.event.*;

    import javax.swing.*;

    public class Drive extends JFrame
    {

    final JPanel condimentsPanel = new JPanel();
    JPanel orderPanel = new JPanel( );
    JButton orderButton = new JButton("Place Order");
    JCheckBox checkBox1 = new JCheckBox("Ketchup");
    JCheckBox checkBox2 = new JCheckBox("Mustard");
    JCheckBox checkBox3 = new JCheckBox("Pickles");


    public static void main(String args[])
    {
    Drive f = new Drive();
    f.setLocation(200,200);
    f.setSize(300,150);

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);

    }

    public Drive()
    {
    super();
    Container content = getContentPane( );

    condimentsPanel.add(checkBox1);
    condimentsPanel.add(checkBox2);
    condimentsPanel.add(checkBox3);


    orderPanel.add(orderButton);


    content.setLayout(new GridLayout(2, 1));
    content.add(condimentsPanel);
    content.add(orderPanel);
    ButtonHandler handler = new ButtonHandler();
    orderButton.addActionListener(handler);

    }
    private class ButtonHandler implements ActionListener
    {
    Component[] components = condimentsPanel.getComponents( );
    JCheckBox cb1 = (JCheckBox)components[0];
    JCheckBox cb2 = (JCheckBox)components[1];
    JCheckBox cb3 = (JCheckBox)components[2];

    public void actionPerformed(ActionEvent event)
    {

    if (cb1.isSelected() )
    JOptionPane.showMessageDialog(null,cb1.getText());
    else
    if(cb2.isSelected())
    JOptionPane.showMessageDialog(null,cb2.getText());
    else
    if(cb3.isSelected())
    JOptionPane.showMessageDialog(null,cb3.getText());
    else
    if( cb1.isSelected() & cb2.isSelected())
    JOptionPane.showMessageDialog(null,cb1.getText() + cb2.getText());
    else
    if(cb1.isSelected() & cb3.isSelected())
    JOptionPane.showMessageDialog(null,cb1.getText() + cb3.getText());
    else
    if(cb2.isSelected() & cb3.isSelected())
    JOptionPane.showMessageDialog(null,cb2.getText() + cb3.getText());
    else if(cb1.isSelected())
    if(cb2.isSelected())
    if(cb3.isSelected())
    JOptionPane.showMessageDialog(null,cb1.getText() + cb2.getText()

    +cb3.getText());
    else
    JOptionPane.showMessageDialog(null,"Please make a selection");
    }
    }

    }

  12. #12
    Izuba is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default

    @ Jos : I am so sorry but i am not sure i've got you properly:could you please illustrate?
    @Faburable: I have just simplified my code,included JCheckboxes, and your suggestion as well.but the result seems to remain the same,here follows the edited source code,kindly correct my mistakes:

    //file: DriveThrough.java
    import java.awt.*;
    import java.awt.event.*;

    import javax.swing.*;

    public class Drive extends JFrame
    {

    final JPanel condimentsPanel = new JPanel();
    JPanel orderPanel = new JPanel( );
    JButton orderButton = new JButton("Place Order");
    JCheckBox checkBox1 = new JCheckBox("Ketchup");
    JCheckBox checkBox2 = new JCheckBox("Mustard");
    JCheckBox checkBox3 = new JCheckBox("Pickles");


    public static void main(String args[])
    {
    Drive f = new Drive();
    f.setLocation(200,200);
    f.setSize(300,150);

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);

    }

    public Drive()
    {
    super();
    Container content = getContentPane( );

    condimentsPanel.add(checkBox1);
    condimentsPanel.add(checkBox2);
    condimentsPanel.add(checkBox3);


    orderPanel.add(orderButton);


    content.setLayout(new GridLayout(2, 1));
    content.add(condimentsPanel);
    content.add(orderPanel);
    ButtonHandler handler = new ButtonHandler();
    orderButton.addActionListener(handler);

    }
    private class ButtonHandler implements ActionListener
    {
    Component[] components = condimentsPanel.getComponents( );
    JCheckBox cb = (JCheckBox)components[i];

    public void actionPerformed(ActionEvent event)
    {
    String resultString = new String();
    if (cb1.isSelected() )
    resultString +="with"+cb.getText() +"\n";
    }

    }

  13. #13
    Izuba is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default

    @ Jos : I am so sorry but i am not sure i've got you properly:could you please illustrate?
    @Faburable: I have just simplified my code,included JCheckboxes, and your suggestion as well.but the result seems to remain the same,here follows the edited source code,kindly correct my mistakes:

    //file: DriveThrough.java
    import java.awt.*;
    import java.awt.event.*;

    import javax.swing.*;

    public class Drive extends JFrame
    {

    final JPanel condimentsPanel = new JPanel();
    JPanel orderPanel = new JPanel( );
    JButton orderButton = new JButton("Place Order");
    JCheckBox checkBox1 = new JCheckBox("Ketchup");
    JCheckBox checkBox2 = new JCheckBox("Mustard");
    JCheckBox checkBox3 = new JCheckBox("Pickles");


    public static void main(String args[])
    {
    Drive f = new Drive();
    f.setLocation(200,200);
    f.setSize(300,150);

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);

    }

    public Drive()
    {
    super();
    Container content = getContentPane( );

    condimentsPanel.add(checkBox1);
    condimentsPanel.add(checkBox2);
    condimentsPanel.add(checkBox3);


    orderPanel.add(orderButton);


    content.setLayout(new GridLayout(2, 1));
    content.add(condimentsPanel);
    content.add(orderPanel);
    ButtonHandler handler = new ButtonHandler();
    orderButton.addActionListener(handler);

    }
    private class ButtonHandler implements ActionListener
    {
    Component[] components = condimentsPanel.getComponents( );
    JCheckBox cb = (JCheckBox)components[i];

    public void actionPerformed(ActionEvent event)
    {
    String resultString = new String();
    if (cb1.isSelected() )
    resultString +="with"+cb.getText() +"\n";
    }

    }

  14. #14
    Izuba is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default

    yep,thank u so much Fubarable and JosAh! i got it.now, i wanted to apply the same principle on a much bigger scale but i am not getting the point right, colud you please correct me? my code is too long,if you can also help me reduce it too.the background is an image.so u can replace it by any other image.
    best regards


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    //BreakFast.java
    //represents breakFast ratm process

    public class Breakfast8 extends JFrame implements ItemListener
    {

    private int basePrice = 0;
    private int item1 = 2000;
    private int item2 = 3000;
    private int item3 = 4000;
    private int item4 = 5000;
    private int item5 = 7000;
    private int item6 = 7800;
    private int item7 = 8900;
    private int item8 = 10000;
    private int item9 = 12000;
    private int item10 = 15000;
    private int item11 = 2000;
    private int item12 = 3000;
    private int item13 = 3500;
    private int item14 = 5000;
    private int totalPrice = basePrice;

    //JCheckBoxes

    JCheckBox checkBox1 = new JCheckBox( "African Tea Ushx"+item1,false);
    JCheckBox checkBox2 = new JCheckBox("Black Tea Ushx"+item2,false);
    JCheckBox checkBox3 = new JCheckBox("English Coffee Ushx"+item3,false);
    JCheckBox checkBox4 = new JCheckBox("Black Coffee Ushx"+item4,false);
    JCheckBox checkBox5 = new JCheckBox("Hot Chocolate Ushx"+item5,false);
    JCheckBox checkBox6 = new JCheckBox("Sausage Ushx"+item6,false);
    JCheckBox checkBox7 = new JCheckBox("Chicken Ushx"+item7,false);
    JCheckBox checkBox8 = new JCheckBox("vegetable Ushx"+item8,false);
    JCheckBox checkBox9 = new JCheckBox("Egg Mayo Ushx"+item9,false);
    JCheckBox checkBox10 = new JCheckBox("Bread Ushx"+item10,false);
    JCheckBox checkBox11 = new JCheckBox("Chapati Ushx"+item11,false);
    JCheckBox checkBox12 = new JCheckBox("Doughnuts Ushx"+item12,false);
    JCheckBox checkBox13 = new JCheckBox("Cake Slices Ushx"+item13,false);
    JCheckBox checkBox14 = new JCheckBox("Queen Cakes Ushx"+item14,false);
    JTextField text1 = new JTextField(10);


    //JImageIcon and JLabels

    ImageIcon img= new ImageIcon("cat.jpg");
    JLabel label1 = new JLabel (img);
    JLabel label = new JLabel ("HOT DRINKS");
    JLabel label2 = new JLabel("BREAKFAST ZONE");
    JLabel lab1 = new JLabel("SANDWICHES");
    JLabel lab2 = new JLabel("BITES");
    JLabel lab4 = new JLabel("Total money is:");

    //JButtons

    private JButton done = new JButton("Done");
    private JButton cancel = new JButton("Cancel");

    //BreakFast class Constructor

    public Breakfast8()
    {
    super ("Breakfast");
    getContentPane().setLayout(new BorderLayout());//set the frame Layout
    label1.setLayout(null);
    add(label1);


    //SETTING FONT onto JLabels

    label.setFont(new Font("Serif",Font.BOLD,28));
    label2.setFont(new Font("Serif",Font.BOLD,28));
    lab1.setFont(new Font("Serif",Font.BOLD,16));
    lab2.setFont(new Font("Serif",Font.BOLD,16));


    // SETTING Background Color onto JCheckBoxes

    checkBox1.setBackground(Color.LIGHT_GRAY);
    checkBox2.setBackground(Color.LIGHT_GRAY);
    checkBox3.setBackground(Color.LIGHT_GRAY);
    checkBox4.setBackground(Color.LIGHT_GRAY);
    checkBox5.setBackground(Color.LIGHT_GRAY);
    checkBox6.setBackground(Color.LIGHT_GRAY);
    checkBox7.setBackground(Color.LIGHT_GRAY);
    checkBox8.setBackground(Color.LIGHT_GRAY);
    checkBox9.setBackground(Color.LIGHT_GRAY);
    checkBox10.setBackground(Color.LIGHT_GRAY);
    checkBox11.setBackground(Color.LIGHT_GRAY);
    checkBox12.setBackground(Color.LIGHT_GRAY);
    checkBox13.setBackground(Color.LIGHT_GRAY);
    checkBox14.setBackground(Color.LIGHT_GRAY);

    //SETTING BOUNDS TO GUI COMPONENTS
    //To JLabels

    label.setBounds(10,3,200,40);
    label2.setBounds(440,5,500,20);
    lab1.setBounds(10,162,200,20);
    lab2.setBounds(10,277,200,20);
    lab4.setBounds(10,420,200,20);

    //To JCheckBoxes

    checkBox1.setBounds(10,48,200,20);
    checkBox2.setBounds(10,70,200,20);
    checkBox3.setBounds(10,92,200,20);
    checkBox4.setBounds(10,114,200,20);
    checkBox5.setBounds(10,136,200,20);
    checkBox6.setBounds(10,190,200,20);
    checkBox7.setBounds(10,212,200,20);
    checkBox8.setBounds(10,234,200,20);
    checkBox9.setBounds(10,256,200,20);
    checkBox10.setBounds(10,298,200,20);
    checkBox11.setBounds(10,320,200,20);
    checkBox12.setBounds(10,342,200,20);
    checkBox13.setBounds(10,364,200,20);
    checkBox14.setBounds(10,386,200,20);

    //To JTextField text1

    text1.setBounds(100,420,120,20);

    // To JButtons

    done.setBounds(230,420,120,20);
    cancel.setBounds(360,420,120,20);

    // ADDING GUI COMPONENTS ONTO JLABEL label1

    // JLabels

    label1.add(label2);
    label1. add(label);
    label1.add(lab1);
    label1.add(lab2);
    label1.add(lab4);

    //JCheckBoxes

    label1.add(checkBox1);
    label1.add(checkBox2);
    label1.add(checkBox3);
    label1.add(checkBox4);
    label1.add(checkBox5);

    label1.add(checkBox6);
    label1.add(checkBox7);
    label1.add(checkBox8);
    label1.add(checkBox9);

    label1.add(checkBox10);
    label1.add(checkBox11);
    label1.add(checkBox12);
    label1.add(checkBox13);
    label1.add(checkBox14);
    label1.add(text1);

    label1.add(done);
    label1.add(cancel);

    text1.setText("Ush"+totalPrice);
    //System.out.printf("%s",this);


    //REGISTER EVENT LISTENERS FOR CHECKBOXES

    checkBox1.addItemListener(this);
    // checkBox2.addItemListener(this);
    checkBox3.addItemListener(this);
    checkBox4.addItemListener(this);
    checkBox5.addItemListener(this);
    checkBox6.addItemListener(this);
    checkBox7.addItemListener(this);
    checkBox8.addItemListener(this);
    checkBox9.addItemListener(this);
    checkBox10.addItemListener(this);
    checkBox11.addItemListener(this);
    checkBox12.addItemListener(this);
    checkBox13.addItemListener(this);
    checkBox14.addItemListener(this);
    ButtonHandler handler = new ButtonHandler();
    done.addActionListener(handler );
    } //end class constructor

    //RESPOND TO CHECKBOX EVENTS

    public void itemStateChanged(ItemEvent event)
    {
    Object source = event.getSource();
    int select = event.getStateChange();

    //PROCESS CHECKBOXES EVENTS

    if(source == checkBox1)
    if(select == ItemEvent.SELECTED)
    totalPrice += item1;
    else
    totalPrice -= item1;


    else if(source == checkBox2)
    {
    if(select == ItemEvent.SELECTED)
    totalPrice += item2;
    else
    totalPrice -= item2;
    }
    else if(source == checkBox3)
    {
    if(select == ItemEvent.SELECTED)
    totalPrice +=item3;
    else
    totalPrice -=item3;
    }

    else if(source == checkBox4)
    {
    if(select == ItemEvent.SELECTED)
    totalPrice += item4;
    else
    totalPrice -= item4;
    }
    else if(source == checkBox5)
    {
    if(select == ItemEvent.SELECTED)
    totalPrice += item5;
    else
    totalPrice -= item5;
    }
    else if(source == checkBox6)
    {
    if(select == ItemEvent.SELECTED)
    totalPrice += item6;
    else
    totalPrice -= item6;
    }
    else if(source == checkBox7)
    {
    if(select == ItemEvent.SELECTED)
    totalPrice += item7;
    else
    totalPrice -= item7;
    }

    else if(source == checkBox8)
    {
    if(select == ItemEvent.SELECTED)
    totalPrice += item8;
    else
    totalPrice -= item8;
    }
    else if(source == checkBox9)
    {
    if(select == ItemEvent.SELECTED)
    totalPrice += item9;
    else
    totalPrice -= item9;
    }
    else if(source == checkBox10)
    {
    if(select == ItemEvent.SELECTED)
    totalPrice += item10;
    else
    totalPrice -= item10;
    }
    else if(source == checkBox11)
    {
    if(select == ItemEvent.SELECTED)
    totalPrice += item11;
    else
    totalPrice -= item11;
    }
    else if(source == checkBox12)
    {
    if(select == ItemEvent.SELECTED)
    totalPrice += item12;
    else
    totalPrice -= item12;
    }
    else if(source == checkBox13)
    {
    if(select == ItemEvent.SELECTED)
    totalPrice += item13;
    else
    totalPrice -= item13;
    }
    else// if(source == checkBox14)

    if(select == ItemEvent.SELECTED)
    totalPrice += item14;
    else
    totalPrice -= item14;
    text1.setText("Ushx" +totalPrice);
    }
    private class ButtonHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent e){
    String entree = new String();
    Component[] components = label1.getComponents();
    for(int i = 0 ; i < components.length ; i++){
    JCheckBox cb = (JCheckBox)components[i];
    if(cb.isSelected())
    entree += cb.getText() +"\n";
    }
    JOptionPane.showMessageDialog(null,entree);
    }
    }

    public static void main(String args[])
    {
    Breakfast8 fast = new Breakfast8();
    fast.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    fast.setSize(800,500);

    fast.setVisible(true);
    } //end of main method
    } //end of class Breakfast

  15. #15
    Izuba is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default

    yep,thank u so much Fubarable and JosAh! i got it.now, i wanted to apply the same principle on a much bigger scale but i am not getting the point right, colud you please correct me? my code is too long,if you can also help me reduce it too.the background is an image.so u can replace it by any other image.
    best regards


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    //BreakFast.java
    //represents breakFast ratm process

    public class Breakfast8 extends JFrame implements ItemListener
    {

    private int basePrice = 0;
    private int item1 = 2000;
    private int item2 = 3000;
    private int item3 = 4000;
    private int item4 = 5000;
    private int item5 = 7000;
    private int item6 = 7800;
    private int item7 = 8900;
    private int item8 = 10000;
    private int item9 = 12000;
    private int item10 = 15000;
    private int item11 = 2000;
    private int item12 = 3000;
    private int item13 = 3500;
    private int item14 = 5000;
    private int totalPrice = basePrice;

    //JCheckBoxes

    JCheckBox checkBox1 = new JCheckBox( "African Tea Ushx"+item1,false);
    JCheckBox checkBox2 = new JCheckBox("Black Tea Ushx"+item2,false);
    JCheckBox checkBox3 = new JCheckBox("English Coffee Ushx"+item3,false);
    JCheckBox checkBox4 = new JCheckBox("Black Coffee Ushx"+item4,false);
    JCheckBox checkBox5 = new JCheckBox("Hot Chocolate Ushx"+item5,false);
    JCheckBox checkBox6 = new JCheckBox("Sausage Ushx"+item6,false);
    JCheckBox checkBox7 = new JCheckBox("Chicken Ushx"+item7,false);
    JCheckBox checkBox8 = new JCheckBox("vegetable Ushx"+item8,false);
    JCheckBox checkBox9 = new JCheckBox("Egg Mayo Ushx"+item9,false);
    JCheckBox checkBox10 = new JCheckBox("Bread Ushx"+item10,false);
    JCheckBox checkBox11 = new JCheckBox("Chapati Ushx"+item11,false);
    JCheckBox checkBox12 = new JCheckBox("Doughnuts Ushx"+item12,false);
    JCheckBox checkBox13 = new JCheckBox("Cake Slices Ushx"+item13,false);
    JCheckBox checkBox14 = new JCheckBox("Queen Cakes Ushx"+item14,false);
    JTextField text1 = new JTextField(10);


    //JImageIcon and JLabels

    ImageIcon img= new ImageIcon("cat.jpg");
    JLabel label1 = new JLabel (img);
    JLabel label = new JLabel ("HOT DRINKS");
    JLabel label2 = new JLabel("BREAKFAST ZONE");
    JLabel lab1 = new JLabel("SANDWICHES");
    JLabel lab2 = new JLabel("BITES");
    JLabel lab4 = new JLabel("Total money is:");

    //JButtons

    private JButton done = new JButton("Done");
    private JButton cancel = new JButton("Cancel");

    //BreakFast class Constructor

    public Breakfast8()
    {
    super ("Breakfast");
    getContentPane().setLayout(new BorderLayout());//set the frame Layout
    label1.setLayout(null);
    add(label1);


    //SETTING FONT onto JLabels

    label.setFont(new Font("Serif",Font.BOLD,28));
    label2.setFont(new Font("Serif",Font.BOLD,28));
    lab1.setFont(new Font("Serif",Font.BOLD,16));
    lab2.setFont(new Font("Serif",Font.BOLD,16));


    // SETTING Background Color onto JCheckBoxes

    checkBox1.setBackground(Color.LIGHT_GRAY);
    checkBox2.setBackground(Color.LIGHT_GRAY);
    checkBox3.setBackground(Color.LIGHT_GRAY);
    checkBox4.setBackground(Color.LIGHT_GRAY);
    checkBox5.setBackground(Color.LIGHT_GRAY);
    checkBox6.setBackground(Color.LIGHT_GRAY);
    checkBox7.setBackground(Color.LIGHT_GRAY);
    checkBox8.setBackground(Color.LIGHT_GRAY);
    checkBox9.setBackground(Color.LIGHT_GRAY);
    checkBox10.setBackground(Color.LIGHT_GRAY);
    checkBox11.setBackground(Color.LIGHT_GRAY);
    checkBox12.setBackground(Color.LIGHT_GRAY);
    checkBox13.setBackground(Color.LIGHT_GRAY);
    checkBox14.setBackground(Color.LIGHT_GRAY);

    //SETTING BOUNDS TO GUI COMPONENTS
    //To JLabels

    label.setBounds(10,3,200,40);
    label2.setBounds(440,5,500,20);
    lab1.setBounds(10,162,200,20);
    lab2.setBounds(10,277,200,20);
    lab4.setBounds(10,420,200,20);

    //To JCheckBoxes

    checkBox1.setBounds(10,48,200,20);
    checkBox2.setBounds(10,70,200,20);
    checkBox3.setBounds(10,92,200,20);
    checkBox4.setBounds(10,114,200,20);
    checkBox5.setBounds(10,136,200,20);
    checkBox6.setBounds(10,190,200,20);
    checkBox7.setBounds(10,212,200,20);
    checkBox8.setBounds(10,234,200,20);
    checkBox9.setBounds(10,256,200,20);
    checkBox10.setBounds(10,298,200,20);
    checkBox11.setBounds(10,320,200,20);
    checkBox12.setBounds(10,342,200,20);
    checkBox13.setBounds(10,364,200,20);
    checkBox14.setBounds(10,386,200,20);

    //To JTextField text1

    text1.setBounds(100,420,120,20);

    // To JButtons

    done.setBounds(230,420,120,20);
    cancel.setBounds(360,420,120,20);

    // ADDING GUI COMPONENTS ONTO JLABEL label1

    // JLabels

    label1.add(label2);
    label1. add(label);
    label1.add(lab1);
    label1.add(lab2);
    label1.add(lab4);

    //JCheckBoxes

    label1.add(checkBox1);
    label1.add(checkBox2);
    label1.add(checkBox3);
    label1.add(checkBox4);
    label1.add(checkBox5);

    label1.add(checkBox6);
    label1.add(checkBox7);
    label1.add(checkBox8);
    label1.add(checkBox9);

    label1.add(checkBox10);
    label1.add(checkBox11);
    label1.add(checkBox12);
    label1.add(checkBox13);
    label1.add(checkBox14);
    label1.add(text1);

    label1.add(done);
    label1.add(cancel);

    text1.setText("Ush"+totalPrice);
    //System.out.printf("%s",this);


    //REGISTER EVENT LISTENERS FOR CHECKBOXES

    checkBox1.addItemListener(this);
    // checkBox2.addItemListener(this);
    checkBox3.addItemListener(this);
    checkBox4.addItemListener(this);
    checkBox5.addItemListener(this);
    checkBox6.addItemListener(this);
    checkBox7.addItemListener(this);
    checkBox8.addItemListener(this);
    checkBox9.addItemListener(this);
    checkBox10.addItemListener(this);
    checkBox11.addItemListener(this);
    checkBox12.addItemListener(this);
    checkBox13.addItemListener(this);
    checkBox14.addItemListener(this);
    ButtonHandler handler = new ButtonHandler();
    done.addActionListener(handler );
    } //end class constructor

    //RESPOND TO CHECKBOX EVENTS

    public void itemStateChanged(ItemEvent event)
    {
    Object source = event.getSource();
    int select = event.getStateChange();

    //PROCESS CHECKBOXES EVENTS

    if(source == checkBox1)
    if(select == ItemEvent.SELECTED)
    totalPrice += item1;
    else
    totalPrice -= item1;


    else if(source == checkBox2)
    {
    if(select == ItemEvent.SELECTED)
    totalPrice += item2;
    else
    totalPrice -= item2;
    }
    else if(source == checkBox3)
    {
    if(select == ItemEvent.SELECTED)
    totalPrice +=item3;
    else
    totalPrice -=item3;
    }

    else if(source == checkBox4)
    {
    if(select == ItemEvent.SELECTED)
    totalPrice += item4;
    else
    totalPrice -= item4;
    }
    else if(source == checkBox5)
    {
    if(select == ItemEvent.SELECTED)
    totalPrice += item5;
    else
    totalPrice -= item5;
    }
    else if(source == checkBox6)
    {
    if(select == ItemEvent.SELECTED)
    totalPrice += item6;
    else
    totalPrice -= item6;
    }
    else if(source == checkBox7)
    {
    if(select == ItemEvent.SELECTED)
    totalPrice += item7;
    else
    totalPrice -= item7;
    }

    else if(source == checkBox8)
    {
    if(select == ItemEvent.SELECTED)
    totalPrice += item8;
    else
    totalPrice -= item8;
    }
    else if(source == checkBox9)
    {
    if(select == ItemEvent.SELECTED)
    totalPrice += item9;
    else
    totalPrice -= item9;
    }
    else if(source == checkBox10)
    {
    if(select == ItemEvent.SELECTED)
    totalPrice += item10;
    else
    totalPrice -= item10;
    }
    else if(source == checkBox11)
    {
    if(select == ItemEvent.SELECTED)
    totalPrice += item11;
    else
    totalPrice -= item11;
    }
    else if(source == checkBox12)
    {
    if(select == ItemEvent.SELECTED)
    totalPrice += item12;
    else
    totalPrice -= item12;
    }
    else if(source == checkBox13)
    {
    if(select == ItemEvent.SELECTED)
    totalPrice += item13;
    else
    totalPrice -= item13;
    }
    else// if(source == checkBox14)

    if(select == ItemEvent.SELECTED)
    totalPrice += item14;
    else
    totalPrice -= item14;
    text1.setText("Ushx" +totalPrice);
    }
    private class ButtonHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent e){
    String entree = new String();
    Component[] components = label1.getComponents();
    for(int i = 0 ; i < components.length ; i++){
    JCheckBox cb = (JCheckBox)components[i];
    if(cb.isSelected())
    entree += cb.getText() +"\n";
    }
    JOptionPane.showMessageDialog(null,entree);
    }
    }

    public static void main(String args[])
    {
    Breakfast8 fast = new Breakfast8();
    fast.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    fast.setSize(800,500);

    fast.setVisible(true);
    } //end of main method
    } //end of class Breakfast

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,656
    Blog Entries
    7
    Rep Power
    21

    Default

    @Izuba: please put your code in those [code] ... [/code] tags just like you did in your first message; your latest messages are unreadable.

    kind regards,

    Jos

  17. #17
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    Quote Originally Posted by JosAH View Post
    The 'message' part in the JOptionPane doesn't have to be a String (altough most of the time it is); it can be another (J)Component; that other (J)Component can contain everything you want, also a list of Strings in, say, a JList or whatever.
    Not too well known: The message argument can also be a String[] or even a multidimensional String array -- even a ragged array.

    db

  18. #18
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    Quote Originally Posted by JosAH View Post
    @Izuba: please put your code in those [code] ... [/code] tags just like you did in your first message
    Fubarable had added the code tags. Some people are slow learners.

    db

  19. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,656
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Not too well known: The message argument can also be a String[] or even a multidimensional String array -- even a ragged array.

    db
    It can even be an Object[] array that mixes all type of elements; it applies recursively. Granted the API documentation mentions this fact in a single sentence but indeed it is quite a powerful feature.

    kind regards,

    Jos

  20. #20
    Izuba is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default

    Hello JosAh and Fubarable!!

    Thank you so much for your precious help.
    I am implementing a program where the customer can buy things either with cash or with credit card. i have now reached the Credit card part and it's giving tough time.What I want is this:
    1)the credit card form to be connected to the database(sql.access,...) for authentication(credit card authentication)
    after the credit card has been authenticated:
    1) I want the user to be told the amount of money in the credit card and whether it's enough or not according to what the user has selected for buying)
    3)If the money in the credit card is enough,I want the program to deduct the total price of things bought by the customer from the money available in the credit card and displays the balance.I have never dealt with database in java before. Could you please help? Here is my source code(for the credit card form).
    Best Regards.
    Izuba

    Java Code:
    //**********************Credit Card class
    //********************Represents credit card form
    
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class CreditCard  extends JFrame
    {
       private JPanel panel;
       private JLabel label1,label2,label3,label4,label5,label6;
       private JLabel cardCode;
       private JTextField text1,text2;
       private JPasswordField password;
       private JComboBox combo1,combo2;
       private String names[ ] = {"Visa","MasterCard","Discover"};
       private String months [] = {"01","02","03","04","05","06","07","08","09","10","11","12"};
      private JButton submitButton;
       public CreditCard()
      {
       super("Customer credit card info");
       getContentPane().setLayout(new BorderLayout());
       // contentPane = getContentPane();
       setLayout(null);
      //contentPane.add(panel);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setUndecorated(true);
       
       //aFrame.setVisible(true);
    
       //create GUI components
       
       panel=new JPanel();
       panel.setBounds(10,5,200,20);
       panel.setSize(600,30);
       panel.setLayout(null);
       panel.setBackground(Color.LIGHT_GRAY);
       add(panel);
       label1 = new JLabel("CREDIT CARD CORNER ");
       label1.setBounds(5,5,200,20);
       panel.add(label1);
       
      label2 = new JLabel("Credit card");
      label2.setBounds(10,40,200,20);
      add(label2);
    
       combo1 = new JComboBox(names);
       combo1.setBounds(110,40,90,20);
       add(combo1);
      
       label3 = new JLabel("Expiration Date");
       label3.setBounds(10,70,90,20);
       add(label3);
    
       combo2 = new JComboBox(months);
      combo2.setBounds(110,70,90,20);
      add(combo2);
    
      label4 = new JLabel("Year");
      label4.setBounds(220,70,90,20);
      add(label4);
      text1 = new JTextField(4); 
      text1.setBounds(250,70,50,20);
      add(text1);
      
      label5 = new JLabel("Card Number");
      label5.setBounds(10,100,200,20);
      add(label5);
      text2 = new JTextField(12);
      text2.setBounds(110,100,90,20);
      add(text2);    
    
    
     label6 = new JLabel("Security Code");
     label6.setBounds(10,125,90,20);
     add(label6);
    
     password = new JPasswordField();
     password.setBounds(110,125,90,20);
     add(password);
       
     submitButton = new JButton("Submit");
     submitButton.setBounds(180,150,90,20);
     add(submitButton);   
       
       
      }
    
      public static void main(String args [])
      {
            
            CreditCard credit = new CreditCard();
            
            credit.setSize(350,200);
            credit.setLocation(650,370);
            credit.setVisible(true);
            
      }  
    }
    Last edited by Izuba; 10-15-2010 at 09:52 AM. Reason: putting code in code tags.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 5
    Last Post: 07-29-2009, 06:38 PM
  2. help with JOptionPane.showMessageDialog()
    By gmn1 in forum New To Java
    Replies: 8
    Last Post: 03-30-2009, 09:31 AM
  3. Replies: 1
    Last Post: 10-29-2008, 07:50 AM
  4. About JOptionPane.showMessageDialog
    By jhetfield18 in forum AWT / Swing
    Replies: 2
    Last Post: 11-02-2007, 10:45 PM
  5. About JOptionPane.showMessageDialog
    By jhetfield18 in forum Advanced Java
    Replies: 0
    Last Post: 11-02-2007, 10:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •