可以通过实现ItemListener接口,如下
public class JcheckBoxtest extends JPanel implements ItemListener {JCheckBox chin
public JcheckBoxtest() {
super(new BorderLayout())
// 创建复选框按键,并设置快捷键,和选定
chin = new JCheckBox("chin")
// 设置一个panel,将复选框放入同一个panel
JPanel checkPanel = new JPanel(new GridLayout(0, 1))
checkPanel.add(chin)
// 添加复选框的监听事件
chin.addItemListener(this)
}
// 接受处理复选框点击事件
@Override
public void itemStateChanged(ItemEvent e) {
// 获取改变的复选按键
Object source = e.getItemSelectable()
char c = '-'
if (source == chin) {
//..进行处理
}
}
}
1)cellrenderer只是用于界面呈现,所有格子公用一个实例, 且并没有一个真正的组件摆在jtable的格子上,所以加listener或取值都没有意义2)celleditor也只有一个实例jcheckbox cb=(jcheckbox)table.getdefaulteditor(boolean.class)然后你可以cb.addxxxxxxxlistener()取值也一样3)其实取值的话, 建议直接从tablemodel中取package p1import 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 ()
{
super (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 < 2)
{
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 < numRows i++ )
{
System.out.print (" row " + i + ":")
for ( int j = 0 j < numCols 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 ()
}
})
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)