永发信息网

怎么在JTable中添加RadioButton

答案:3  悬赏:40  手机版
解决时间 2021-03-21 02:39
  • 提问者网友:我是我
  • 2021-03-20 14:49
怎么在一个JTable的单元格里添加一组RadioButton
急求高手帮忙
要用实例代码说明,容易理解
二楼的朋友,我运行起来好像有异常啊
最佳答案
  • 五星知识达人网友:雪起风沙痕
  • 2021-03-20 15:29
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.*;

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.*;

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;
}
}
全部回答
  • 1楼网友:怙棘
  • 2021-03-20 16:57
很简单 你把JRadioButton的代码手工复制,然后粘贴到相应的单元格代码内即可 理解一下...
  • 2楼网友:夜风逐马
  • 2021-03-20 15:50
用数组加一个循环加进去 。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯