日期:2014-05-20 浏览次数:21037 次
import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JFrame; import javax.swing.JTable; import swing.ShowDialog; public class Mytable { /** * @param args */ public static void main(String[] args) { CellEditable mytable = new CellEditable(); JFrame f = new JFrame("QQ"); JTable t = new JTable(mytable); t.addMouseListener(new MouseListener(){ public void mouseClicked(MouseEvent arg0) { // TODO Auto-generated method stub } public void mouseEntered(MouseEvent arg0) { // TODO Auto-generated method stub } public void mouseExited(MouseEvent arg0) { // TODO Auto-generated method stub } public void mousePressed(MouseEvent arg0) { JTable table = (JTable)arg0.getSource(); int row =table.getSelectedRow(); int column = table.getSelectedColumn(); String name = table.getValueAt(row,column).toString(); if (arg0.getClickCount()==2) { ShowDialog d = new ShowDialog(null,name); } } public void mouseReleased(MouseEvent arg0) { // TODO Auto-generated method stub }}); t.setCellSelectionEnabled(true); t.setDragEnabled(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(t); f.setSize(100, 200); f.setLocation(200,200); f.setVisible(true); } } import java.util.Vector; import javax.swing.table.DefaultTableModel; public class CellEditable extends DefaultTableModel { public boolean isCellEditable(int arg0, int arg1) { return false; } public int getColumnCount() { return 1; } public int getRowCount() { return 5; } public Object getValueAt(int row, int column) { Object[] rowData = { "警察", "小偷", "魔怔", "公务员", "商人" }; return rowData[row]; } } import java.awt.Frame; import java.awt.HeadlessException; import javax.swing.JDialog; import javax.swing.JTextArea; public class ShowDialog extends JDialog { private JTextArea text = null; public ShowDialog(Frame arg0, String arg1) throws HeadlessException { super(arg0, arg1); text = new JTextArea(); text.setBounds(40, 40, 130, 60); add(text); setLayout(null); setLocation(500, 500); setSize(200, 200); setVisible(true); } }