代码如下:程序中jtable添加不了vector,本人知道是什么问题,就是不知道怎么解决!!!希望知道的告诉一声!谢谢!!
package com.fortuneduck.bc.view;import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;import javax.swing.*;import com.fortuneduck.bc.database.BcDatabase;(数据层)
import com.fortuneduck.bc.database.BcDbBarcode;(数据库中表对象)
import com.fortuneduck.bc.database.BcDbBardno;(数据库中表对象)
//import com.fortuneduck.bc.database.BcDbBarcode;(数据库中表对象)
public class BcMasSearch extends JFrame
{
    private JTextField textCode;
    private JButton bOk;
    private Choice choiceFile;
    private JTable table;
    //private JTextArea areaFile;
    private JList list;
    private Map map;
    String str1 = null;
    String str2 = null;
    Vector vector;
    
    public BcMasSearch()
    {
        super("Bar Code Search");
        this.setSize(500,250);        choiceFile = new Choice();
        choiceFile.add("Please Input Bar Code:");
        choiceFile.add("Please Input Nylon Code:");
        
        bOk = new JButton("OK");
        bOk.setFont(new Font("MS Sans Serif", java.awt.Font.PLAIN, 18));
        textCode = new JTextField(20);
        
        map = new HashMap();
        vector = new Vector();
        
        JPanel pTop = new JPanel();
        pTop.add(choiceFile);
        pTop.add(textCode);
        pTop.add(bOk);
        
        JScrollPane pCenter = new JScrollPane(list);
        table = new JTable();
        table.setPreferredScrollableViewportSize(new Dimension(550,30));
        
        Container container = this.getContentPane();
        container.add(pTop,BorderLayout.NORTH);
        container.add(pCenter,BorderLayout.CENTER);
        
        bOk.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        if(e.getSource()==bOk&&choiceFile.getSelectedIndex()==0)
                        {
                                if(BcDatabase.checkCode(textCode.getText()))
                                {
                                    listCode();
                                    //list.setListData(vector);
                                }
                                else
                                {
                                    JOptionPane.showMessageDialog(null,"Please Input Bar Code");
                                }
                           listCode();     
                        }
                       else if(e.getSource()==bOk&&choiceFile.getSelectedIndex()==1)
                       {
                           if(BcDatabase.checkNylon(textCode.getText()))
                           {
                                 listDno();
                           }
                       }
                    }
                });        
    }
    public void listCode()
    {
        map = BcDatabase.getBarcode("select * from bar_code where code ='"+textCode.getText()+"'");
        
         Iterator item = map.keySet().iterator();
         while(item.hasNext())
         {
             String strKey = (String)item.next();
             BcDbBarcode oBcDbBarcode = new BcDbBarcode();
             oBcDbBarcode = (BcDbBarcode)map.get(strKey);
             
             vector.addElement(oBcDbBarcode.getCODE());
             vector.addElement(oBcDbBarcode.getRUNNO());
             str1 = oBcDbBarcode.getCODE();
             str2 = oBcDbBarcode.getRUNNO();
         }
    }
    public void listDno()
    {
        map = BcDatabase.getBarcode("select * from bar_dno");
        
        Iterator item = map.keySet().iterator();
        while(item.hasNext())
        {
            String strKey = (String)item.next();
            BcDbBardno dno = new  BcDbBardno();
            dno = ( BcDbBardno)map.get(strKey);
            
            vector.addElement(dno);
        }
    }
}

解决方案 »

  1.   

    要向TableModel中加vector,而不是像table中加。
      

  2.   


    上面有一个地方写错了应该是:
      JScrollPane pCenter = new JScrollPane(table);
      table = new JTable(vector,null);
      table.setPreferredScrollableViewportSize(new Dimension(550,30));autowind(一个人住真痛苦) ::能给个具体的代码吗?我试了哈还是搞不定
    谢谢!!!
      

  3.   

    这是我写的一个TABLEMODEL,你使用JTABLE的时候需要
    SETMODEL();然后把相应的数据和TITLE传给它。import java.util.*;
    import javax.swing.table.*;public class EquipTm extends AbstractTableModel{  private Vector vect;
      private String[] title;  public EquipTm(){  }  public EquipTm(Vector vect,String[] title){
          this.vect = vect;
          this.title = title;
      }  public int getColumnCount() {
        return title.length;
      }  public int getRowCount() {
        return vect.size();
      }  public Object getValueAt(int row, int column) {
        if (!vect.isEmpty())
          return ( (Vector) vect.elementAt(row)).elementAt(column);
        else
          return null;
      }  public String getColumnName(int column) {
        return title[column];
      }  public void setValueAt(Object value, int row, int column) {
      }  public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
      }  public boolean isCellEditable(int row, int column) {
        return false;
      }  }