java复选框添加选中事件_Java的JTable中添加JCheckBox,点击事件处理问题,求高手指点迷津...

这篇博客展示了如何在Java Swing中创建一个包含JTable的程序,其中表格的某一列显示JCheckBox。作者遇到的问题是处理JCheckBox的选中事件,他们希望能够捕捉到用户点击时的状态变化。博客通过一个名为`MyTableModel`的自定义抽象表格模型展示数据,并实现了`isCellEditable`和`setValueAt`方法来控制编辑行为。尽管提供了基本的表格设置,但并未详细说明具体的事件监听和处理机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

展开全部

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 ();

}

});

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值