来自combobox.selectedItem的setValueAt(...)jtable抛出.ArrayIndexOutOfBoundsException:-1

时间:2016-02-19 16:12:04

标签: java jtable jcombobox

我有一个面板,其中包含标签和文本字段,以完成订单的infromations和一个空的jtable,所以当我点击添加按钮时,将在jtable中添加一个空行,其中包含一个comboBox,当我选择一个文章引用时使用DefaultTableModel中的setvalueAt方法,第二列和第三列中的项目将使用setvalueAt方法加载文章名称和价格。 我的问题是当我在comboBox中选择一个引用时,它会在setValueAt行抛出ArrayIndexOutOfBoundsException:-1,所以任何人都可以在这里帮帮我!??

这是我的代码: package vues.panel;

import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Date;
import java.util.Calendar;
import java.util.List;
import java.util.Vector;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SpinnerDateModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import model.ArticleTableModel;
import model.beans.Article;
import model.beans.Commande;
import model.persistence.PersistentArticle;

public class CommandePanel extends JPanel {
        private static final long serialVersionUID = 1L;
    private JLabel lblIdCmd, lblDate, lblClient, lblTotal;
    private JTextField tfIdCmd, tfClient, tfTotal;
    private JSpinner spnDateCmd;
    private JButton btAdd, btDelete, btSave;
    private JScrollPane scp;
    private JTable tab;
    private TableColumn refColumn;
    private JComboBox<Integer> comboBox;
    private ArticleCommandeTableModel cmdArtModel;
    public CommandePanel(){
        setLayout(null);
        initComponents();
        remplirComboBox();
        setActions();
        setVisible(true);
    }


    public void initComponents(){
        lblIdCmd = new JLabel("Id Commande : ");
        tfIdCmd = new JTextField(12);
        lblDate = new JLabel("Date Commande : ");
        spnDateCmd = new JSpinner(new SpinnerDateModel());
        spnDateCmd.setValue(Calendar.getInstance().getTime());
        spnDateCmd.setEditor(new JSpinner.DateEditor(spnDateCmd,"dd/MM/yyyy hh:mm"));
        lblClient =  new JLabel("Client : ");
        tfClient = new JTextField(12);

        btAdd = new JButton("Ajouter Article");
        btDelete = new JButton("Supprimer Article");

        tab=new JTable();
        tab.setModel(cmdArtModel= new ArticleCommandeTableModel()); 
        tab.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        refColumn = tab.getColumnModel().getColumn(0);
        scp =new JScrollPane(tab);
        tab = new JTable();
        comboBox = new JComboBox<>();
        lblTotal = new JLabel("Total : ");
        lblTotal.setFont(new Font("Verdana", Font.BOLD, 14));
        tfTotal = new JTextField(12);

        btSave = new JButton("Sauvegarder Commande");

        lblIdCmd.setBounds(50, 15, 300, 25);
        tfIdCmd.setBounds(220, 20, 146, 22);
        lblDate.setBounds(50, 45, 300, 25);
        spnDateCmd.setBounds(220, 50, 146, 22);
        lblClient.setBounds(50, 75, 300, 25);
        tfClient.setBounds(220, 80, 146, 22);

        btAdd.setBounds(30, 145, 150, 25);
        btDelete.setBounds(240, 145, 150, 25);

        scp.setBounds(20, 200, 550, 300);

        lblTotal.setBounds(375, 515, 300, 25);
        tfTotal.setBounds(440, 515, 130, 25);

        btSave.setBounds(175, 560, 200, 25);

        add(lblIdCmd);
        add(tfIdCmd);
        add(lblDate);
        add(spnDateCmd);
        add(lblClient);
        add(tfClient);

        add(btAdd);
        add(btDelete);

        add(scp);

        add(lblTotal);
        add(tfTotal);

        add(btSave);
    }

    public void setActions(){
        btAdd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                chargerTab();
                cmdArtModel.setRowCount(ArticleCommandeTableModel.rowCount++);
            }
        });

        comboBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(comboBox.getSelectedItem()!=null && comboBox.getSelectedIndex()>=0){
                int ref = Integer.valueOf(comboBox.getSelectedItem().toString());
                tab.setValueAt(ref, tab.getSelectedRow(), tab.getSelectedColumn());
                }

            }
        });

    }

    public Commande getCommandeFromView(){
        Commande c = new Commande();
        c.setIdComande(Integer.valueOf(tfIdCmd.getText()));
        c.setDateCmd(Date.valueOf(spnDateCmd.getValue().toString()));
        c.setClient(tfClient.getText());
        return c;
    }

    public void chargerTab(){
            refColumn.setCellEditor(new DefaultCellEditor(comboBox));
            //ArticleCommandeTableModel.count=cmdArtModel.getRowCount()+1;
            cmdArtModel.addRow(new Vector<>());
            DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
            renderer.setToolTipText("Click for combo box");
            refColumn.setCellRenderer(renderer);

    }

    public void remplirComboBox(){
        List<Article> l;
        try {
            l = PersistentArticle.getArticles();
            for(int i= 0;i<l.size();i++){
                comboBox.addItem(l.get(i).getRef());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void remplirLibPu(int ref){
        //tab.setValueAt(arg0, arg1, arg2)
    }
}

class ArticleCommandeTableModel extends DefaultTableModel{

    private static final long serialVersionUID = 1L;
    String[] title ={"Ref Article", "Libelle", "PU", "Qté", "Total"};
    Object[][] data;
    static int rowCount =0;

    public ArticleCommandeTableModel(){ }
    public int getColumnCount(){
        return 5;
    }

//  public int getRowCount(){
//      if(data==null) return 0;
//      return data.length;
//  }

    public String getColumnName(int col){
        return title[col];
    }


    public boolean isCellEditable(int row, int col){
        if(col==0||col==3)return true;
        return false;
    }

    @Override
    public void setValueAt(Object value, int row, int col) {
        int ref = (Integer)value;
        Article a=ArticleTableModel.getArticleByRef(ref);
        if(col==0){
            super.setValueAt(a.getLibelle(), row, 1);
            super.setValueAt(a.getPu(), row, 2);
        }
        else{
            super.setValueAt(value, row, col);
        }
        fireTableDataChanged();
    }




    @Override
    public Object getValueAt(int row, int column) {
        return super.getValueAt(row, column);
    }

    @Override
    public void addRow(Vector rowData) {
        super.addRow(rowData);
    }
    public Component getTableCellRendererComponent(JTable table,
               Object value, boolean isSelected , boolean hasFocus, int row, int column) {
              if(value instanceof JComboBox){
               return (JComboBox) value;
              }
              else{
                  return null;
              }
    }
}

0 个答案:

没有答案