怎么在JTable中添加RadioButton
怎么在一个JTable的单元格里添加一组RadioButton急求高手帮忙要用实例代码说明,容易理解二楼的朋友,我运行起来好像有异常啊...
怎么在一个JTable的单元格里添加一组RadioButton
急求高手帮忙
要用实例代码说明,容易理解
二楼的朋友,我运行起来好像有异常啊 展开
急求高手帮忙
要用实例代码说明,容易理解
二楼的朋友,我运行起来好像有异常啊 展开
3个回答
展开全部
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;
}
}
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;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询