怎么在JTable中添加RadioButton

怎么在一个JTable的单元格里添加一组RadioButton急求高手帮忙要用实例代码说明,容易理解二楼的朋友,我运行起来好像有异常啊... 怎么在一个JTable的单元格里添加一组RadioButton
急求高手帮忙
要用实例代码说明,容易理解
二楼的朋友,我运行起来好像有异常啊
展开
 我来答
176807694
2008-03-19 · TA获得超过158个赞
知道答主
回答量:85
采纳率:100%
帮助的人:25.8万
展开全部
jTable1.getColumn("Con1").setCellRenderer(new ButtonRenderer());
jTable1.getColumn("Con1").setCellEditor(new ButtonEditor(new JCheckBox()));

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
/** * @version 1.0 11/09/98 */
public class ButtonEditor extends DefaultCellEditor {
protected JButton button;
private String label;
private boolean isPushed;
private int rowcount;

public ButtonEditor(JCheckBox checkBox) {
super(checkBox);
button = new JButton();
button.setOpaque(true);

button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fireEditingStopped();}});
}

public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
rowcount = row;
if (isSelected) {
button.setForeground(table.getSelectionForeground());
button.setBackground(new Color(182, 208, 200));
//button.setBackground(table.getSelectionBackground());
} else{
button.setForeground(table.getForeground());
//button.setBackground(new Color(184, 208, 200));
//button.setBackground(new Color(212, 208, 200));
button.setBackground(table.getBackground());
}
label = (value ==null) ? "" : value.toString();
button.setText( label );
isPushed = true;
return button;
}

public Object getCellEditorValue() {
if (isPushed) {
//
//JOptionPane.showMessageDialog(button ,label + ": tOuch!");
// System.out.println(label + ": Ouch!");
}
isPushed = false;
return new String( label ) ;
}

public boolean stopCellEditing() {
isPushed = false;
return super.stopCellEditing();
}

protected void fireEditingStopped() {
super.fireEditingStopped();
}
}

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
/** * @version 1.0 11/09/98 */
public class ButtonRenderer extends JButton implements TableCellRenderer {
public ButtonRenderer() {
setOpaque(true);
}

public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(new Color(184, 208, 200));
setBackground(table.getSelectionBackground());
} else{
setForeground(table.getForeground());
setBackground(new Color(217, 236, 210));
//setBackground(UIManager.getColor("Button.background"));
}
setText( (value ==null) ? "" : value.toString() );
this.setToolTipText("Perform forecasting for this period only!");
return this;
}
}
badbaby2007
2008-03-19
知道答主
回答量:42
采纳率:0%
帮助的人:25.9万
展开全部
用数组加一个循环加进去
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友7d47d1791
2008-03-21 · TA获得超过654个赞
知道小有建树答主
回答量:420
采纳率:0%
帮助的人:0
展开全部
很简单
你把JRadioButton的代码手工复制,然后粘贴到相应的单元格代码内即可
理解一下...
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式