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();
}
}