无法在eclipse中编辑jTables中的单元格

时间:2014-02-20 19:13:10

标签: eclipse swing jtable awt

当我编写以下Stock.java文件

package com.zawlatt.ui;
import java.awt.EventQueue;
import javax.swing.*;
import com.zawlatt.model.*;

public class Stock 
{

private JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JTable table;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Stock window = new Stock();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Stock() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 688, 514);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JLabel lblSearchItemBy = new JLabel("Search Item by Item ID");
    lblSearchItemBy.setHorizontalAlignment(SwingConstants.CENTER);
    lblSearchItemBy.setBounds(27, 58, 167, 23);
    frame.getContentPane().add(lblSearchItemBy);

    textField = new JTextField();
    textField.setBounds(212, 58, 114, 23);
    frame.getContentPane().add(textField);
    textField.setColumns(10);

    JLabel label = new JLabel("Search Item by Item ID");
    label.setHorizontalAlignment(SwingConstants.CENTER);
    label.setBounds(358, 58, 167, 23);
    frame.getContentPane().add(label);

    textField_1 = new JTextField();
    textField_1.setColumns(10);
    textField_1.setBounds(543, 58, 114, 23);
    frame.getContentPane().add(textField_1);

    String [] colNames = {"#", "Item", "Description", "Original Qty","Last Qty ", "Original Price", "Sale Price" , "Recieved Date"};
    StockItem [][] stockData = new StockItem[15][8];
    for(int i=0; i<stockData.length;i++)
    {
        for(int j=0 ; j<stockData[i].length;j++)
        {

            stockData[i][j]=new StockItem();
        }
    }
    table = new JTable(stockData,colNames);
    table.setEnabled(true);
    //table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    JScrollPane pane = new JScrollPane(table , ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS , ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    pane.setBounds(35, 93, 622, 274);
    frame.getContentPane().add(pane);
}

}

我接到了例外情况!

Exception in thread "AWT-EventQueue-0" java.lang.ArrayStoreException: java.lang.String
at javax.swing.JTable$1.setValueAt(JTable.java:693)
at javax.swing.JTable.setValueAt(JTable.java:2740)
at javax.swing.JTable.editingStopped(JTable.java:4725)
at javax.swing.AbstractCellEditor.fireEditingStopped(AbstractCellEditor.java:142)
at javax.swing.DefaultCellEditor$EditorDelegate.stopCellEditing(DefaultCellEditor.java:366)
at javax.swing.DefaultCellEditor.stopCellEditing(DefaultCellEditor.java:231)
at javax.swing.JTable$GenericEditor.stopCellEditing(JTable.java:5480)
at javax.swing.plaf.basic.BasicTableUI$Handler.mousePressed(BasicTableUI.java:1007)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:280)
at java.awt.Component.processMouseEvent(Component.java:6285)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6053)
at java.awt.Container.processEvent(Container.java:2045)
at java.awt.Component.dispatchEventImpl(Component.java:4649)
at java.awt.Container.dispatchEventImpl(Container.java:2103)
at java.awt.Component.dispatchEvent(Component.java:4475)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4633)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4294)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4227)
at java.awt.Container.dispatchEventImpl(Container.java:2089)
at java.awt.Window.dispatchEventImpl(Window.java:2587)
at java.awt.Component.dispatchEvent(Component.java:4475)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:675)
at java.awt.EventQueue.access$300(EventQueue.java:96)
at java.awt.EventQueue$2.run(EventQueue.java:634)
at java.awt.EventQueue$2.run(EventQueue.java:632)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:108)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:119)
at java.awt.EventQueue$3.run(EventQueue.java:648)
at java.awt.EventQueue$3.run(EventQueue.java:646)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:108)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:645)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

1 个答案:

答案 0 :(得分:0)

您编写的代码中未正确填充数据模型:

String [] colNames = {"#", "Item", "Description", "Original Qty","Last Qty ", "Original Price", "Sale Price" , "Recieved Date"};
StockItem [][] stockData = new StockItem[15][8];
for(int i=0; i<stockData.length;i++)
{
    for(int j=0 ; j<stockData[i].length;j++)
    {
       fill the correct data----->stockData[i][j]=new StockItem();
    }
}

更改以上内容如下:

for(int i=0; i<stockData.length;i++)
{    
     StockItem item = new StockItem();
     stockData[i][0]= item.getId();
     stockData[i][1]= item.getItem();
     .
     .
     stockData[i][7]= item.getReceiveDate();
}