Im having a problem with this program why i am not able to set present total price of product if gonna seect the product form combo and the remove button cant get the selected index,retrieve the selected item and remove the selected item from the list using its index or deduct the item's cost from the grand total. pls help me guys


btw sorry fo my englis im too bad =(

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

 class Billing extends JFrame {
		JFrame frame = new JFrame();
		JPanel panel_input = new JPanel();
		JPanel panel_output = new JPanel();
		JPanel panel_buttons = new JPanel();
		JPanel panel_upper = new JPanel();
		JPanel panel_main = new JPanel();
		JPanel panel_lower = new JPanel();


		JButton but1 = new JButton("Clear");
		JButton but2 = new JButton("Remove");
		JButton but3 = new JButton("Add");
		JLabel labelTx = new JLabel("Total");
		JLabel label1 = new JLabel(" Product");
		JLabel label2 = new JLabel(" Price");
		JLabel label3 = new JLabel(" ");
		JLabel emp   =  new JLabel (" Quantity");
		JLabel label4 = new JLabel(" text");
		JLabel emp2 = new JLabel(" ");
		String[]options = {"Fresh Milk","Coffee","Tea","Ihi Juice","Float"};
		int [] pricedata ={35,25,15,100,25};
		JComboBox combo = new JComboBox(options);

		JTextField textf1 = new JTextField(15);
		JTextField textf2 = new JTextField(15);
		DefaultListModel listModel = new DefaultListModel();
		JList jl1 = new JList(listModel);
		DefaultListModel listModel2 = new DefaultListModel();
		JList jl2 = new JList(listModel2);
		JPanel panel_list = new JPanel();



	Billing(){

		frame.setResizable(false);
		frame.setVisible(true);
    	frame.setSize(450,500);
    	frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);




    	panel_input.setLayout(new GridLayout(0,2));

    	add(panel_upper);
    	panel_input.add(label1);
    	panel_input.add(combo);
		panel_input.add(label2);
		panel_input.add(label3);
		panel_input.add(emp);
		panel_input.add(textf1);
		panel_input.setSize(250,150);
		panel_input.add(panel_buttons);

		panel_buttons.setLayout(new GridLayout(0,3));
		panel_buttons.setSize(500,200);
		panel_buttons.add(but1);
		panel_buttons.add(but2);
		panel_buttons.add(but3);
		but2.setEnabled(false);



		add(panel_lower);
		panel_list.setLayout(new GridLayout(0,2));
		panel_list.add(new JScrollPane(jl1));
		panel_list.add(new JScrollPane(jl2));
		panel_output.setLayout(new GridLayout(0,2));
		panel_output.add(labelTx);
		panel_output.add(textf2);

		frame.setLayout(new BorderLayout());
		frame.add(panel_input, BorderLayout.NORTH);
		frame.add(panel_list,BorderLayout.CENTER);
		frame.add(panel_output,BorderLayout.SOUTH);
		but1.addActionListener(new ButtonListener());
		but2.addActionListener(new ButtonListener());
		but3.addActionListener(new ButtonListener());
		combo.addActionListener(new ButtonListener());

textf1.requestFocus();




    }

    public class ListListener implements ListSelectionListener{
    	public void valueChanged(ListSelectionEvent ev){
    		if(listModel.size()==0){
    			but2.setEnabled(false);
    			}
    		else{
    			but2.setEnabled(true);
    			}
    		}
    	}
    public class ButtonListener implements ActionListener{

    	public void actionPerformed(ActionEvent e){

			if(e.getSource()==combo){
				int $$ = combo.getSelectedIndex();
				label3.setText(""+pricedata[$$]);
					textf1.requestFocus();
					}
			if(e.getSource()==but3){
				int xy = Integer.parseInt(textf1.getText());
				int yx = Integer.parseInt(label3.getText());
				int result = xy*yx;


				listModel.addElement(combo.getSelectedItem()+" "+xy+"x"+yx);
					if(result>1){
					int shit = result + result;
					textf2.setText(" "+shit);
					}
				listModel2.addElement(result);
				but2.setEnabled(true);
				textf1.requestFocus();
				textf2.setText(""+result);




			}

			if(e.getSource()==but2){
			int yy = jl1.getSelectedIndex();
			listModel.getElementAt(yy);
			double zx = Integer.parseInt(textf1.getText());
			zx = zx-yy;
			but3.setEnabled(false);
			combo.requestFocus();
			but2.setEnabled(false);



				if(listModel.size()==0){
					textf2.setText(" "+0);
				}
				else{
					textf2.setText(" "+zx);
					textf1.setText(" ");
					}
					}


			if(e.getSource()==but1){
				listModel.clear();
				listModel2.clear();

				textf2.setText(" "+0.0);

				combo.requestFocus();

					}







    	}

    	}









 public static void main(String[]args){
    	Billing $ = new Billing();
}
}

at first of all put here Use Formatted Text Fields instead of JTestField

set NumberFormat

for example

NumberFormat formTextFieldFormat = NumberFormat.getNumberInstance();
        formTextFieldFormat.setMinimumFractionDigits(2);
        formTextFieldFormat.setMaximumFractionDigits(2);
        formTextFieldFormat.setRoundingMode(RoundingMode.HALF_UP);

. ..
...
...
JFormattedTextField  formTextField = new JFormattedTextField(formTextFieldFormat);

don't forget to set minimal ammout

formTextField.setValue(amount);

nothing complicated as you can see (for example)

import java.awt.*;
import java.awt.event.*;
import java.math.RoundingMode;
import java.text.NumberFormat;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

public class FormatterLimit {

    private JFrame frame = new JFrame();
    private JPanel pnl = new JPanel();
    private JLabel focusLabel = new JLabel(" focusLost Handle ");
    private JFormattedTextField formTextField;
    private JLabel docLabel = new JLabel(" document Handle ");
    private JFormattedTextField formTextField1;
    private NumberFormat formTextFieldFormat;
    private double amount = 10000.00;

    public FormatterLimit() {
        formTextFieldFormat = NumberFormat.getNumberInstance();
        formTextFieldFormat.setMinimumFractionDigits(2);
        formTextFieldFormat.setMaximumFractionDigits(2);
        formTextFieldFormat.setRoundingMode(RoundingMode.HALF_UP);

        focusLabel.setFont(new Font("Serif", Font.BOLD, 14));
        focusLabel.setForeground(Color.blue);
        focusLabel.setPreferredSize(new Dimension(120, 27));
        formTextField = new JFormattedTextField(formTextFieldFormat);
        formTextField.setValue(amount);
        formTextField.setFont(new Font("Serif", Font.BOLD, 22));
        formTextField.setForeground(Color.black);
        formTextField.setBackground(Color.yellow);
        formTextField.setPreferredSize(new Dimension(120, 27));
        formTextField.setHorizontalAlignment(SwingConstants.RIGHT);
        formTextField.addFocusListener(new FocusListener() {

            @Override
            public void focusGained(FocusEvent e) {
                formTextField.requestFocus();
                formTextField.setText(formTextField.getText());
                formTextField.selectAll();
            }

            @Override
            public void focusLost(FocusEvent e) {
                //Runnable doRun = new Runnable() {

                //public void run() {
                double t1a1 = (((Number) formTextField.getValue()).doubleValue());
                if (t1a1 < 1000) {
                    formTextField.setValue(amount);
                }
                //}
                //};
                //SwingUtilities.invokeLater(doRun);

            }
        });

        docLabel.setFont(new Font("Serif", Font.BOLD, 14));
        docLabel.setForeground(Color.blue);
        docLabel.setPreferredSize(new Dimension(120, 27));

        formTextField1 = new JFormattedTextField(formTextFieldFormat);
        formTextField1.setValue(amount);
        formTextField1.setFont(new Font("Serif", Font.BOLD, 22));
        formTextField1.setForeground(Color.black);
        formTextField1.setBackground(Color.yellow);
        formTextField1.setPreferredSize(new Dimension(120, 27));
        formTextField1.setHorizontalAlignment(SwingConstants.RIGHT);
        formTextField1.addFocusListener(new FocusListener() {

            @Override
            public void focusGained(FocusEvent e) {
                formTextField1.requestFocus();
                formTextField1.setText(formTextField1.getText());
                formTextField1.selectAll();
            }

            @Override
            public void focusLost(FocusEvent e) {
            }
        });
        formTextField1.getDocument().addDocumentListener(docListener);

        pnl = new JPanel();
        pnl.setBorder(new EmptyBorder(2, 2, 2, 2));
        pnl.setLayout(new GridLayout(2, 2));
        pnl.add(focusLabel);
        pnl.add(formTextField);
        pnl.add(docLabel);
        pnl.add(formTextField1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(pnl, BorderLayout.CENTER);
        frame.setLocation(200, 200);
        frame.pack();
        frame.setVisible(true);
        //formTextFieldFocus();
    }
    //
    private DocumentListener docListener = new DocumentListener() {

        @Override
        public void changedUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);
        }

        @Override
        public void insertUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);
        }

        @Override
        public void removeUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);
        }

        private void printIt(DocumentEvent documentEvent) {
            DocumentEvent.EventType type = documentEvent.getType();
            double t1a1 = (((Number) formTextField1.getValue()).doubleValue());
            if (t1a1 < 1000) {
                Runnable doRun = new Runnable() {

                    @Override
                    public void run() {
                        formTextField1.setValue(amount);
                    }
                };
                SwingUtilities.invokeLater(doRun);
            }
        }
    };

    public void formTextFieldFocus1() {
        Runnable doRun = new Runnable() {

            @Override
            public void run() {
                formTextField1.requestFocus();
                formTextField1.setText(formTextField1.getText());
                formTextField1.selectAll();
            }
        };
        SwingUtilities.invokeLater(doRun);
    }

    public void formTextFieldFocus() {
        Runnable doRun = new Runnable() {

            @Override
            public void run() {
                formTextField.requestFocus();
                formTextField.setText(formTextField.getText());
                formTextField.selectAll();
                formTextFieldFocus1();
            }
        };
        SwingUtilities.invokeLater(doRun);
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                FormatterLimit fl = new FormatterLimit();
            }
        });
    }
}

or

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.*;

public class CurrencyFormaterTest {

    private JLabel jl;
    private JFormattedTextField jft, jft1, jft2, jft3, jft4;
    private JButton jb = new JButton("but");

    public CurrencyFormaterTest() {
        JFrame frame = new JFrame("Text Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jl = new JLabel("Try with formatted text field   ");
        Locale local1 = new Locale("en", "US");
        int minFra = 0;
        int maxFra = 3;
        jft = setFormat(jft, local1, minFra, maxFra);
        //jft.setColumns(20);
        jft.setMaximumSize(new Dimension(100, 20));
        jft.setMinimumSize(new Dimension(100, 20));
        jft.setPreferredSize(new Dimension(100, 20));
        local1 = new Locale("gb", "GB");
        minFra = 2;
        maxFra = 2;
        jft1 = setFormat(jft1, local1, minFra, maxFra);
        //jft1.setColumns(20);
        jft1.setMaximumSize(new Dimension(100, 20));
        jft1.setMinimumSize(new Dimension(100, 20));
        jft1.setPreferredSize(new Dimension(100, 20));
        local1 = new Locale("de", "DE");
        minFra = 0;
        maxFra = 3;
        jft2 = setFormat(jft2, local1, minFra, maxFra);
        //jft2.setColumns(20);
        jft2.setMaximumSize(new Dimension(100, 20));
        jft2.setMinimumSize(new Dimension(100, 20));
        jft2.setPreferredSize(new Dimension(100, 20));
        local1 = new Locale("jp", "JP");
        minFra = 0;
        maxFra = 0;
        jft3 = setFormat(jft3, local1, minFra, maxFra);
        //jft3.setColumns(20);
        jft3.setMaximumSize(new Dimension(100, 20));
        jft3.setMinimumSize(new Dimension(100, 20));
        jft3.setPreferredSize(new Dimension(100, 20));
        local1 = new Locale("hk", "HK");
        minFra = 0;
        maxFra = 3;
        jft4 = setFormat(jft4, local1, minFra, maxFra);
        //jft4.setColumns(20);
        jft4.setMaximumSize(new Dimension(100, 20));
        jft4.setMinimumSize(new Dimension(100, 20));
        jft4.setPreferredSize(new Dimension(100, 20));
        frame.add(jb);
        //
        frame.add(jl);
        frame.add(jft, BorderLayout.NORTH);
        frame.add(jft1, BorderLayout.SOUTH);
        frame.add(jft2, BorderLayout.WEST);
        frame.add(jft3, BorderLayout.EAST);
        frame.add(jft4, BorderLayout.CENTER);
        jb.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(jb, "nel text Box : " + jft.getText());
            }
        });

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        CurrencyFormaterTest textTest = new CurrencyFormaterTest();
    }

    private JFormattedTextField setFormat(JFormattedTextField jft, Locale local1, int minFra, int maxFra) {
        NumberFormat numberFormat;
        Locale local = local1;
        int setMin = minFra;
        int setMax = maxFra;
        numberFormat = NumberFormat.getCurrencyInstance(local);
        numberFormat.setMinimumFractionDigits(setMin);
        numberFormat.setMaximumFractionDigits(setMax);
        numberFormat.setRoundingMode(RoundingMode.HALF_UP);
        jft = new JFormattedTextField(numberFormat);
        jft.setValue(new Double(342.796));
        return jft;
    }
}

at first of all put here Use Formatted Text Fields instead of JTestField

set NumberFormat

for example

NumberFormat formTextFieldFormat = NumberFormat.getNumberInstance();
        formTextFieldFormat.setMinimumFractionDigits(2);
        formTextFieldFormat.setMaximumFractionDigits(2);
        formTextFieldFormat.setRoundingMode(RoundingMode.HALF_UP);

. ..
...
...
JFormattedTextField  formTextField = new JFormattedTextField(formTextFieldFormat);

don't forget to set minimal ammout

formTextField.setValue(amount);

nothing complicated as you can see (for example)

import java.awt.*;
import java.awt.event.*;
import java.math.RoundingMode;
import java.text.NumberFormat;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

public class FormatterLimit {

    private JFrame frame = new JFrame();
    private JPanel pnl = new JPanel();
    private JLabel focusLabel = new JLabel(" focusLost Handle ");
    private JFormattedTextField formTextField;
    private JLabel docLabel = new JLabel(" document Handle ");
    private JFormattedTextField formTextField1;
    private NumberFormat formTextFieldFormat;
    private double amount = 10000.00;

    public FormatterLimit() {
        formTextFieldFormat = NumberFormat.getNumberInstance();
        formTextFieldFormat.setMinimumFractionDigits(2);
        formTextFieldFormat.setMaximumFractionDigits(2);
        formTextFieldFormat.setRoundingMode(RoundingMode.HALF_UP);

        focusLabel.setFont(new Font("Serif", Font.BOLD, 14));
        focusLabel.setForeground(Color.blue);
        focusLabel.setPreferredSize(new Dimension(120, 27));
        formTextField = new JFormattedTextField(formTextFieldFormat);
        formTextField.setValue(amount);
        formTextField.setFont(new Font("Serif", Font.BOLD, 22));
        formTextField.setForeground(Color.black);
        formTextField.setBackground(Color.yellow);
        formTextField.setPreferredSize(new Dimension(120, 27));
        formTextField.setHorizontalAlignment(SwingConstants.RIGHT);
        formTextField.addFocusListener(new FocusListener() {

            @Override
            public void focusGained(FocusEvent e) {
                formTextField.requestFocus();
                formTextField.setText(formTextField.getText());
                formTextField.selectAll();
            }

            @Override
            public void focusLost(FocusEvent e) {
                //Runnable doRun = new Runnable() {

                //public void run() {
                double t1a1 = (((Number) formTextField.getValue()).doubleValue());
                if (t1a1 < 1000) {
                    formTextField.setValue(amount);
                }
                //}
                //};
                //SwingUtilities.invokeLater(doRun);

            }
        });

        docLabel.setFont(new Font("Serif", Font.BOLD, 14));
        docLabel.setForeground(Color.blue);
        docLabel.setPreferredSize(new Dimension(120, 27));

        formTextField1 = new JFormattedTextField(formTextFieldFormat);
        formTextField1.setValue(amount);
        formTextField1.setFont(new Font("Serif", Font.BOLD, 22));
        formTextField1.setForeground(Color.black);
        formTextField1.setBackground(Color.yellow);
        formTextField1.setPreferredSize(new Dimension(120, 27));
        formTextField1.setHorizontalAlignment(SwingConstants.RIGHT);
        formTextField1.addFocusListener(new FocusListener() {

            @Override
            public void focusGained(FocusEvent e) {
                formTextField1.requestFocus();
                formTextField1.setText(formTextField1.getText());
                formTextField1.selectAll();
            }

            @Override
            public void focusLost(FocusEvent e) {
            }
        });
        formTextField1.getDocument().addDocumentListener(docListener);

        pnl = new JPanel();
        pnl.setBorder(new EmptyBorder(2, 2, 2, 2));
        pnl.setLayout(new GridLayout(2, 2));
        pnl.add(focusLabel);
        pnl.add(formTextField);
        pnl.add(docLabel);
        pnl.add(formTextField1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(pnl, BorderLayout.CENTER);
        frame.setLocation(200, 200);
        frame.pack();
        frame.setVisible(true);
        //formTextFieldFocus();
    }
    //
    private DocumentListener docListener = new DocumentListener() {

        @Override
        public void changedUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);
        }

        @Override
        public void insertUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);
        }

        @Override
        public void removeUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);
        }

        private void printIt(DocumentEvent documentEvent) {
            DocumentEvent.EventType type = documentEvent.getType();
            double t1a1 = (((Number) formTextField1.getValue()).doubleValue());
            if (t1a1 < 1000) {
                Runnable doRun = new Runnable() {

                    @Override
                    public void run() {
                        formTextField1.setValue(amount);
                    }
                };
                SwingUtilities.invokeLater(doRun);
            }
        }
    };

    public void formTextFieldFocus1() {
        Runnable doRun = new Runnable() {

            @Override
            public void run() {
                formTextField1.requestFocus();
                formTextField1.setText(formTextField1.getText());
                formTextField1.selectAll();
            }
        };
        SwingUtilities.invokeLater(doRun);
    }

    public void formTextFieldFocus() {
        Runnable doRun = new Runnable() {

            @Override
            public void run() {
                formTextField.requestFocus();
                formTextField.setText(formTextField.getText());
                formTextField.selectAll();
                formTextFieldFocus1();
            }
        };
        SwingUtilities.invokeLater(doRun);
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                FormatterLimit fl = new FormatterLimit();
            }
        });
    }
}

or

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.*;

public class CurrencyFormaterTest {

    private JLabel jl;
    private JFormattedTextField jft, jft1, jft2, jft3, jft4;
    private JButton jb = new JButton("but");

    public CurrencyFormaterTest() {
        JFrame frame = new JFrame("Text Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jl = new JLabel("Try with formatted text field   ");
        Locale local1 = new Locale("en", "US");
        int minFra = 0;
        int maxFra = 3;
        jft = setFormat(jft, local1, minFra, maxFra);
        //jft.setColumns(20);
        jft.setMaximumSize(new Dimension(100, 20));
        jft.setMinimumSize(new Dimension(100, 20));
        jft.setPreferredSize(new Dimension(100, 20));
        local1 = new Locale("gb", "GB");
        minFra = 2;
        maxFra = 2;
        jft1 = setFormat(jft1, local1, minFra, maxFra);
        //jft1.setColumns(20);
        jft1.setMaximumSize(new Dimension(100, 20));
        jft1.setMinimumSize(new Dimension(100, 20));
        jft1.setPreferredSize(new Dimension(100, 20));
        local1 = new Locale("de", "DE");
        minFra = 0;
        maxFra = 3;
        jft2 = setFormat(jft2, local1, minFra, maxFra);
        //jft2.setColumns(20);
        jft2.setMaximumSize(new Dimension(100, 20));
        jft2.setMinimumSize(new Dimension(100, 20));
        jft2.setPreferredSize(new Dimension(100, 20));
        local1 = new Locale("jp", "JP");
        minFra = 0;
        maxFra = 0;
        jft3 = setFormat(jft3, local1, minFra, maxFra);
        //jft3.setColumns(20);
        jft3.setMaximumSize(new Dimension(100, 20));
        jft3.setMinimumSize(new Dimension(100, 20));
        jft3.setPreferredSize(new Dimension(100, 20));
        local1 = new Locale("hk", "HK");
        minFra = 0;
        maxFra = 3;
        jft4 = setFormat(jft4, local1, minFra, maxFra);
        //jft4.setColumns(20);
        jft4.setMaximumSize(new Dimension(100, 20));
        jft4.setMinimumSize(new Dimension(100, 20));
        jft4.setPreferredSize(new Dimension(100, 20));
        frame.add(jb);
        //
        frame.add(jl);
        frame.add(jft, BorderLayout.NORTH);
        frame.add(jft1, BorderLayout.SOUTH);
        frame.add(jft2, BorderLayout.WEST);
        frame.add(jft3, BorderLayout.EAST);
        frame.add(jft4, BorderLayout.CENTER);
        jb.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(jb, "nel text Box : " + jft.getText());
            }
        });

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        CurrencyFormaterTest textTest = new CurrencyFormaterTest();
    }

    private JFormattedTextField setFormat(JFormattedTextField jft, Locale local1, int minFra, int maxFra) {
        NumberFormat numberFormat;
        Locale local = local1;
        int setMin = minFra;
        int setMax = maxFra;
        numberFormat = NumberFormat.getCurrencyInstance(local);
        numberFormat.setMinimumFractionDigits(setMin);
        numberFormat.setMaximumFractionDigits(setMax);
        numberFormat.setRoundingMode(RoundingMode.HALF_UP);
        jft = new JFormattedTextField(numberFormat);
        jft.setValue(new Double(342.796));
        return jft;
    }
}

thanks sir, it helps me a lot
=)

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