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

public class Burger extends JFrame implements ActionListener
{
    JLabel lblForAll, lblBelow, pic, pic1, pic2, pic3, pic4, pic5, pic6, pic7;
    JRadioButton rBtn, rBtn1, rBtn2, rBtn3;
    JCheckBox cBtn, cBtn1, cBtn2, cBtn3;
    JTextField txt, txt1, txt2, txt3, txt4, txt5;
    JButton btn1, btn2;
    ButtonGroup btnG;
    int ham=25, egg=15, bacon=40, chicken=36, coleslow=8, cheese=10, cucumber=12, tomatoe=14;
    //ImageIcon ham1 = new ImageIcon("1.png"), egg1 = new ImageIcon("2.png"), bacon1 = new ImageIcon("3.png"), chicken1 = new ImageIcon("4.png");

    Burger()
    {
        super("Burger Ordering System");
        setLayout(null);

        add(lblForAll = new JLabel("Select Burger: "));
        lblForAll.setBounds(10,20,100,20);

        add(lblForAll = new JLabel("Add Ons: "));
        lblForAll.setBounds(150,20,100,20);

        add(rBtn = new JRadioButton("Ham Burger"));
        rBtn.setBounds(10,40,100,20);
        add(rBtn1 = new JRadioButton("Egg Burger"));
        rBtn1.setBounds(10,60,100,20);
        add(rBtn2 = new JRadioButton("Bacon Burger"));
        rBtn2.setBounds(10,80,130,20);
        add(rBtn3 = new JRadioButton("Chicken Burger"));
        rBtn3.setBounds(10,100,130,20);

        add(txt = new JTextField());
        txt.setBounds(150,130,100,20);
        txt.setText("0");
        add(txt1 = new JTextField());
        txt1.setBounds(150,155,100,20);
        add(txt2 = new JTextField());
        txt2.setBounds(150,180,100,20);
        add(txt3 = new JTextField());
        txt3.setBounds(150,205,100,20);
        add(txt4 = new JTextField());
        txt4.setBounds(150,230,100,20);
        txt4.setText("0");
        add(txt5 = new JTextField());
        txt5.setBounds(150,255,100,20);

        add(cBtn = new JCheckBox("Coleslaw"));
        cBtn.setBounds(150,40,100,20);
        cBtn.setOpaque(false);
        cBtn.addItemListener(
            new ItemListener()
            {
                public void itemStateChanged(ItemEvent e)
                {
                    String txt1;
                    if(cBtn.isSelected()==true)
                    {
                        txt1=Integer.toString(ham);
                        txt2.setText(txt1);
                        cBtn1.setEnabled(false);
                        cBtn2.setEnabled(false);
                        cBtn3.setEnabled(false);
                    }
                    else
                    {
                        cBtn1.setEnabled(true);
                        cBtn2.setEnabled(true);
                        cBtn3.setEnabled(true);
                        txt2.setText("");
                    }
                }
            }
    );
        add(cBtn1 = new JCheckBox("Cheese"));
        cBtn1.setBounds(150,60,100,20);
        cBtn1.setOpaque(false);
        cBtn1.addItemListener(
            new ItemListener()
            {
                public void itemStateChanged(ItemEvent e)
                {
                    String txt1;
                    if(cBtn1.isSelected()==true)
                    {
                        txt1=Integer.toString(cheese);
                        txt2.setText(txt1);
                        cBtn.setEnabled(false);
                        cBtn2.setEnabled(false);
                        cBtn3.setEnabled(false);
                    }
                    else
                    {
                        cBtn.setEnabled(true);
                        cBtn2.setEnabled(true);
                        cBtn3.setEnabled(true);
                        txt2.setText("");
                    }
                }
            }
    );

        add(cBtn2 = new JCheckBox("Cucumber"));
        cBtn2.setBounds(150,80,100,20);
        cBtn2.setOpaque(false);
        cBtn2.addItemListener(
            new ItemListener()
            {
                public void itemStateChanged(ItemEvent e)
                {
                    String txt1;
                    if(cBtn2.isSelected()==true)
                    {
                        txt1=Integer.toString(cucumber);
                        txt2.setText(txt1);
                        cBtn.setEnabled(false);
                        cBtn1.setEnabled(false);
                        cBtn3.setEnabled(false);
                    }
                    else
                    {
                        cBtn.setEnabled(true);
                        cBtn1.setEnabled(true);
                        cBtn3.setEnabled(true);
                        txt2.setText("");
                    }
                }
            }
    );
        add(cBtn3 = new JCheckBox("Tomatoes"));
        cBtn3.setBounds(150,100,100,20);
        cBtn3.setOpaque(false);
        cBtn3.addItemListener(
            new ItemListener()
            {
                public void itemStateChanged(ItemEvent e)
                {
                    String txt1;
                    if(cBtn3.isSelected()==true)
                    {
                        cBtn.setEnabled(false);
                        cBtn1.setEnabled(false);
                        cBtn2.setEnabled(false);
                        txt1=Integer.toString(tomatoe);
                        txt2.setText(txt1);
                    }
                    else
                    {
                        cBtn.setEnabled(true);
                        cBtn1.setEnabled(true);
                        cBtn2.setEnabled(true);
                        txt2.setText("");
                    }
                }
            }
    );

        add(lblBelow = new JLabel("Enter Quantity: "));
        lblBelow.setBounds(20,130,130,20);
        add(lblBelow = new JLabel("Burger Amount:          P"));
        lblBelow.setBounds(20,155,130,20);
        add(lblBelow = new JLabel("Add-Ons Amount:       P"));
        lblBelow.setBounds(20,180,130,20);
        add(lblBelow = new JLabel("Total Amount:              P"));
        lblBelow.setBounds(20,205,130,20);
        add(lblBelow = new JLabel("Enter Payment:           P"));
        lblBelow.setBounds(20,230,130,20);
        add(lblBelow = new JLabel("Change:                        P"));
        lblBelow.setBounds(20,255,140,20);

        add(btn1 = new JButton("Compute Amount"));
        btn1.setBounds(20,290,140,20);
        btn1.addActionListener(this);
        add(btn2 = new JButton("Process Payment"));
        btn2.setBounds(180,290,140,20);
        btn2.addActionListener(this);

        RadioButtonHandler handler = new RadioButtonHandler();
        rBtn.addItemListener(handler);
        rBtn1.addItemListener(handler);
        rBtn2.addItemListener(handler);
        rBtn3.addItemListener(handler);

        btnG = new ButtonGroup();
        btnG.add(rBtn);
        btnG.add(rBtn1);
        btnG.add(rBtn2);
        btnG.add(rBtn3);

        txt1.setEditable(false); txt2.setEditable(false); txt3.setEditable(false); txt5.setEditable(false);

        setSize(400,500);
        setResizable(false);
        setVisible(true);
    }
    public static void main(String[] args)
    {
        Burger a = new Burger();
        a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        a.setDefaultLookAndFeelDecorated(true);
        a.setLocationRelativeTo(null);
    }
    public void actionPerformed(ActionEvent e)
    {
        int total, qntty, charge, pay1, quantty, payment1;
        String text, qnty, text1, text2, pay, payment;
        Boolean validation = true;
        if(e.getSource()==btn1)
        {
            if(txt.getText().equals("0")||txt.getText().equals(""))
            {
                JOptionPane.showMessageDialog(null, "Enter the quantity of Burger", "Error!", JOptionPane.ERROR_MESSAGE);
                txt.requestFocusInWindow();
                txt.setText("");
            }
            else
            {
                qnty = txt.getText(); text2 = txt2.getText(); quantty = Integer.parseInt(txt1.getText());
                if(quantty<=0)
                    validation = true;
                else
                    validation = false;
                if(validation==true)
                {
                    JOptionPane.showMessageDialog(null, "Please enter a valid value", "Error!", JOptionPane.ERROR_MESSAGE);
                    txt.setText("");
                    txt.requestFocusInWindow();
                }
                else
                {
                    qntty = Integer.parseInt(qnty) * Integer.parseInt(txt1.getText());
                    total = qntty + ( Integer.parseInt(qnty) * Integer.parseInt(text2));
                    text = Integer.toString(total);
                    txt3.setText(text);
                    txt4.requestFocusInWindow();
                }
            }
        }
        else
        {
            int payLength = txt4.getText().length();
            pay = txt4.getText();
            pay1 = Integer.parseInt(pay);
            qnty = txt.getText(); text1 = txt1.getText(); text2 = txt2.getText();
            qntty = Integer.parseInt(qnty) * Integer.parseInt(text1);
            total = qntty + Integer.parseInt(text2);
            Boolean valid = true;
            if(pay1<total)
                valid = true;
            else
                valid = false;
            try
            {
                if(txt4.getText().equals(""))
                {
                    throw new NumberFormatException();
                }
                else
                {
                    if(valid==true)
                    {
                        JOptionPane.showMessageDialog(null, "You have insufficient funds", "Error!", JOptionPane.WARNING_MESSAGE);
                        txt5.setText("");
                        txt4.requestFocusInWindow();
                    }
                    else
                    {
                        payment = txt3.getText(); payment1 = Integer.parseInt(payment);
                        charge = Integer.parseInt(pay) - payment1;
                        text = Integer.toString(charge);
                        if(charge<0)
                            valid = false;
                        else
                            valid = true;
                        if(valid==true)
                            txt5.setText(text);
                        else
                        {
                            JOptionPane.showMessageDialog(null, "You have insufficient funds", "Error!", JOptionPane.WARNING_MESSAGE);
                            txt4.requestFocusInWindow();
                        }
                    }
                }
            }
            catch(NumberFormatException excep)
            {
                JOptionPane.showMessageDialog(null, "Please enter a value", "Error!", JOptionPane.WARNING_MESSAGE);
                txt5.setText("");
                txt4.requestFocusInWindow();
            }
            /*if(payLength==0||payLength<0||txt4.getText().equals(""))
            {
                JOptionPane.showMessageDialog(null, "Please enter a value", "Error!", JOptionPane.WARNING_MESSAGE);
                txt5.setText("");
                txt4.requestFocusInWindow();
            }*/

        }
    }
    class RadioButtonHandler implements ItemListener
    {
        public void itemStateChanged(ItemEvent e)
        {
            String text;
            if(rBtn.isSelected())
            {
                //add(pic = new JLabel(ham1));
                //pic.setBounds(20,310,200,150);
                text=Integer.toString(ham);
                txt1.setText(text);
                txt.setText("1");
                //pic1.setBounds(0,0,0,0); pic2.setBounds(0,0,0,0); pic3.setBounds(0,0,0,0);
            }
            else if(rBtn1.isSelected())
            {
                //add(pic1 = new JLabel(egg1));
                //pic1.setBounds(20,310,200,150);
                text=Integer.toString(egg);
                txt1.setText(text);
                txt.setText("1");
                //pic.setBounds(0,0,0,0); pic2.setBounds(0,0,0,0); pic3.setBounds(0,0,0,0);
            }
            else if(rBtn2.isSelected())
            {
                //add(pic2 = new JLabel(bacon1));
                //pic2.setBounds(20,310,200,150);
                text=Integer.toString(bacon);
                txt1.setText(text);
                txt.setText("1");
                //pic.setBounds(0,0,0,0); pic1.setBounds(0,0,0,0); pic3.setBounds(0,0,0,0);
            }
            else if(rBtn3.isSelected())
            {
                //add(pic3 = new JLabel(chicken1));
                //pic3.setBounds(20,310,200,150);
                text=Integer.toString(chicken);
                txt1.setText(text);
                txt.setText("1");
                //pic.setBounds(0,0,0,0); pic1.setBounds(0,0,0,0); pic2.setBounds(0,0,0,0);
            }
        }
    }
}

here's my whole code and my error is on the try and catch of this part..

try
            {
                if(txt4.getText().equals(""))
                {
                    throw new NumberFormatException();
                }
                else
                {
                    if(valid==true)
                    {
                        JOptionPane.showMessageDialog(null, "You have insufficient funds", "Error!", JOptionPane.WARNING_MESSAGE);
                        txt5.setText("");
                        txt4.requestFocusInWindow();
                    }
                    else
                    {
                        payment = txt3.getText(); payment1 = Integer.parseInt(payment);
                        charge = Integer.parseInt(pay) - payment1;
                        text = Integer.toString(charge);
                        if(charge<0)
                            valid = false;
                        else
                            valid = true;
                        if(valid==true)
                            txt5.setText(text);
                        else
                        {
                            JOptionPane.showMessageDialog(null, "You have insufficient funds", "Error!", JOptionPane.WARNING_MESSAGE);
                            txt4.requestFocusInWindow();
                        }
                    }
                }
            }
            catch(NumberFormatException excep)
            {
                JOptionPane.showMessageDialog(null, "Please enter a value", "Error!", JOptionPane.WARNING_MESSAGE);
                txt5.setText("");
                txt4.requestFocusInWindow();
            }

the numberformatexception is not working.. in that part i need to check if the textfield is empty or not.. i tried various codes like .trim(), .equals(""), .length(), etc.. but it still doesnt work.. i always get thsi too many errors:

> --------------------Configuration: <Default>--------------------
> Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
>     at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
>     at java.lang.Integer.parseInt(Integer.java:504)
>     at java.lang.Integer.parseInt(Integer.java:527)
>     at Burger.actionPerformed(Burger.java:246)
>     at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
>     at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
>     at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
>     at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
>     at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
>     at java.awt.Component.processMouseEvent(Component.java:6505)
>     at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
>     at java.awt.Component.processEvent(Component.java:6270)
>     at java.awt.Container.processEvent(Container.java:2229)
>     at java.awt.Component.dispatchEventImpl(Component.java:4861)
>     at java.awt.Container.dispatchEventImpl(Container.java:2287)
>     at java.awt.Component.dispatchEvent(Component.java:4687)
>     at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
>     at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
>     at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
>     at java.awt.Container.dispatchEventImpl(Container.java:2273)
>     at java.awt.Window.dispatchEventImpl(Window.java:2719)
>     at java.awt.Component.dispatchEvent(Component.java:4687)
>     at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
>     at java.awt.EventQueue.access$200(EventQueue.java:103)
>     at java.awt.EventQueue$3.run(EventQueue.java:688)
>     at java.awt.EventQueue$3.run(EventQueue.java:686)
>     at java.security.AccessController.doPrivileged(Native Method)
>     at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
>     at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
>     at java.awt.EventQueue$4.run(EventQueue.java:702)
>     at java.awt.EventQueue$4.run(EventQueue.java:700)
>     at java.security.AccessController.doPrivileged(Native Method)
>     at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
>     at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
>     at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
>     at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
>     at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
>     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
>     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
>     at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

can someone help me on making a validation that will work..

  • use JFormattedTextField for number instance (similair code example in Oracles tutorial about how to use JFormattedTextField), or JSpinner

  • there are two ways (JTextComponents returns Document, model for JTextComponents, use getDocument rather than getText)

    1. use DocumentListener (in the case that all changes from JTextComponents will going outside)

    2. use DocumentFilter (in the case that all changes are internall, restrict, change or modify own Document)

  • you can to combine DocumentFilter with DocumentListener,

  • use LayoutManagers instead of setBounds e.g.

Edited 3 Years Ago by mKorbel

but im still a newbie here.. don't know yet those JFormattedTextField,JSpinner,JTextComponents , getDocument ,DocumentListener ,DocumentFilter ,LayoutManagers and how those works..

Any container can always contain any component. That is why Swing and AWT are organized into containers and components.

uhm, this is already solved.. figured that i misplaced the parsing of the string of textfield.. if shud be posted if the textfield is not empty.. thanks for help..

This question has already been answered. Start a new discussion instead.