java swing中实现列表中加入单选按钮,单选按钮发生变化时能触发事件
Posted 崔耀强
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java swing中实现列表中加入单选按钮,单选按钮发生变化时能触发事件相关的知识,希望对你有一定的参考价值。
java swing中实现列表中加入单选按钮,单选按钮发生变化时能触发事件
package test;
import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
class RadioButtonRenderer implements TableCellRenderer
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column)
if (value == null)
return null;
return (Component) value;
class RadioButtonEditor extends DefaultCellEditor implements ItemListener
/**
*/
private static final long serialVersionUID = 1L;
private JRadioButton button;
public RadioButtonEditor(JCheckBox checkBox)
super(checkBox);
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column)
if (value == null)
return null;
button = (JRadioButton) value;
button.addItemListener(this);
return (Component) value;
public Object getCellEditorValue()
button.removeItemListener(this);
return button;
public void itemStateChanged(ItemEvent e)
super.fireEditingStopped();
System.out.println("111111111");
public class JRadioButtonTableExample extends JFrame
/**
*
*/
private static final long serialVersionUID = 1L;
public JRadioButtonTableExample()
DefaultTableModel dm = new DefaultTableModel();
Object [][]o= new Object[5][1];
for(int i=0;i<5;i++)
o[i][0]="Group 1";
Object [][]o2= new Object[5][2];
for(int i=0;i<5;i++)
o2[i][0]=o[i][0];
o2[i][1]=new JRadioButton();
dm.setDataVector(o2, new Object[]
"String", "JRadioButton" );
JTable table = new JTable(dm)
public void tableChanged(TableModelEvent e)
super.tableChanged(e);
repaint();
;
table.getColumn("JRadioButton").setCellRenderer(
new RadioButtonRenderer());
table.getColumn("JRadioButton").setCellEditor(
new RadioButtonEditor(new JCheckBox()));
JScrollPane scroll = new JScrollPane(table);
getContentPane().add(scroll);
setSize(200, 140);
setVisible(true);
public static void main(String[] args)
JRadioButtonTableExample frame = new JRadioButtonTableExample();
frame.addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent e)
System.exit(0);
);
无意中发现了一个巨牛的人工智能教程,忍不住分享一下给大家。教程不仅是零基础,通俗易懂,而且非常风趣幽默,像看小说一样!觉得太牛了,所以分享给大家。点这里可以跳转到教程。
以上是关于java swing中实现列表中加入单选按钮,单选按钮发生变化时能触发事件的主要内容,如果未能解决你的问题,请参考以下文章