日期:2014-05-20  浏览次数:21054 次

使用JTable做表格的时候,为什么会这样啊,为什么值传不过去?
[color=#FF0000][/color]
代码:
package com.jtable;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTextField;
import javax.swing.JList;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ListSelectionModel;
;public class JTableTest extends JFrame {

private JPanel contentPane;
private JTextField textField;
private JTable table;

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

/**
* Create the frame.
*/
public JTableTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setForeground(Color.BLACK);
setContentPane(contentPane);
contentPane.setLayout(null);

textField = new JTextField();
textField.setBounds(136, 221, 177, 21);
contentPane.add(textField);
textField.setColumns(10);

JList list = new JList();
list.setBounds(218, 216, 1, -120);
contentPane.add(list);

table = new JTable();
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent e) {
Object o=table.getValueAt(table.getSelectedRow(), table.getSelectedColumn());
String str = (String)o;
textField.setText(str);  
}
});
 
table.setModel(new DefaultTableModel(
new Object[][] {
{new Integer(1), new Integer(2), new Integer(3)},
{new Integer(4), new Integer(5), new Integer(6)},
{new Integer(7), new Integer(8), new Integer(9)},
{new Integer(10), new Integer(11), new Integer(12)},
{new Integer(13), new Integer(14), new Integer(15)},
{new Integer(16), new Integer(17), new Integer(18)},
},
new String[] {
"\u8BED\u6587", "\u6570\u5B66", "\u82F1\u8BED"
}
));

table.setBounds(76, 48, 263, 93);
contentPane.add(table);
}
}

异常
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at com.jtable.JTableTest$2.mouseClicked(JTableTest.java:63)
at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)