java复选框添加选中事件_Java的JTable中添加JCheckBox,点击事件处理问题,求高手指点迷津...
展开全部
package p1;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class TableDemo extends JPanel
{
private boolean DEBUG = true;
public TableDemo ()
{
32313133353236313431303231363533e78988e69d8331333335343364super (new GridLayout (1, 0));
JTable table = new JTable (new MyTableModel ());
table.setPreferredScrollableViewportSize (new Dimension (500, 70));
table.setFillsViewportHeight (true);
JScrollPane scrollPane = new JScrollPane (table);
add (scrollPane);
}
class MyTableModel extends AbstractTableModel
{
private String[] columnNames = { "First Name", "Last Name", "Sport", "# of Years", "Vegetarian" };
private Object[][] data = { { "Kathy", "Smith", "Snowboarding", new Integer (5), new Boolean (false) },
{ "John", "Doe", "Rowing", new Integer (3), new Boolean (true) },
{ "Sue", "Black", "Knitting", new Integer (2), new Boolean (false) },
{ "Jane", "White", "Speed reading", new Integer (20), new Boolean (true) },
{ "Joe", "Brown", "Pool", new Integer (10), new Boolean (false) } };
public int getColumnCount ()
{
return columnNames.length;
}
public int getRowCount ()
{
return data.length;
}
public String getColumnName ( int col )
{
return columnNames[col];
}
public Object getValueAt ( int row, int col )
{
return data[row][col];
}
public Class getColumnClass ( int c )
{
return getValueAt (0, c).getClass ();
}
public boolean isCellEditable ( int row, int col )
{
if (col
{
return false;
}
else
{
return true;
}
}
public void setValueAt ( Object value, int row, int col )
{
if (DEBUG)
{
System.out.println ("Setting value at " + row + "," + col + " to " + value + " (an instance of "
+ value.getClass () + ")");
}
data[row][col] = value;
fireTableCellUpdated (row, col);
if (DEBUG)
{
System.out.println ("New value of data:");
printDebugData ();
}
}
private void printDebugData ()
{
int numRows = getRowCount ();
int numCols = getColumnCount ();
for ( int i = 0; i
{
System.out.print (" row " + i + ":");
for ( int j = 0; j
{
System.out.print (" " + data[i][j]);
}
System.out.println ();
}
System.out.println ("--------------------------");
}
}
private static void createAndShowGUI ()
{
JFrame frame = new JFrame ("TableDemo");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
TableDemo newContentPane = new TableDemo ();
newContentPane.setOpaque (true);
frame.setContentPane (newContentPane);
frame.pack ();
frame.setVisible (true);
}
public static void main ( String[] args )
{
javax.swing.SwingUtilities.invokeLater (new Runnable ()
{
public void run ()
{
createAndShowGUI ();
}
});
}
}