java jtable 复选框_java – 如何在JTable中呈现一个复选框?

这是我渲染JTable并更改行颜色的代码,但它没有在第6列中显示复选框,只显示字符串(true,false).

你能提供解决方案吗?

提前致谢.

public class JLabelRenderer extends JLabel implements TableCellRenderer

{

private MyJTable myTable;

/**

* Creates a Custom JLabel Cell Renderer

* @param t your JTable implmentation that holds the Hashtable to inquire for

* rows and colors to paint.

*/

public JLabelRenderer(MyJTable t)

{

this.myTable = t;

}

/**

* Returns the component used for drawing the cell. This method is

* used to configure the renderer appropriately before drawing.

* see TableCellRenderer.getTableCellRendererComponent(...); for more comments on the method

*/

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)

{

setOpaque(true); //JLabel isn't opaque by default

setText(value.toString());

setFont(myTable.getFont());

if(!isSelected)//if the row is not selected then use the custom color

setBackground(myTable.getRowToPaint(row));

else //if the row is selected use the default selection color

setBackground(myTable.getSelectionBackground());

//Foreground could be changed using another Hashtable...

setForeground(myTable.getForeground());

// Since the renderer is a component, return itself

return this;

}

// The following methods override the defaults for performance reasons

public void validate() {}

public void revalidate() {}

protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {}

public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {}

}

这是表:

import javax.swing.JTable;

import javax.swing.table.*;

import java.util.Hashtable;

import java.awt.Color;

public class MyJTable extends JTable

{

Hashtable rowsToPaint = new Hashtable(1);

/**

* Default Constructor

*/

public MyJTable()

{

super();

}

/**

* Set the TableModel and then render each column with a custom cell renderer

* @param tm TableModel

*/

public void setModel(TableModel tm)

{

super.setModel(tm);

renderColumns(new JLabelRenderer(this));

}

/**

* Add a new entry indicating:

* @param row the row to paint - the first row = 0;

* @param bgColor background color

*/

public void addRowToPaint(int row, Color bgColor)

{

rowsToPaint.put(new Integer(row), bgColor);

this.repaint();// you need to repaint the table for each you put in the hashtable.

}

/**

* Returns the user selected BG Color or default BG Color.

* @param row the row to paint

* @return Color BG Color selected by the user for the row

*/

public Color getRowToPaint(int row)

{

Color bgColor = (Color)rowsToPaint.get(new Integer(row));

return (bgColor != null)?bgColor:getBackground();

}

/**

* Render all columns with the specified cell renderer

* @param cellRender TableCellRenderer

*/

public void renderColumns(TableCellRenderer cellRender)

{

for(int i=0; i

{

renderColumn(this.getColumnModel().getColumn(i), cellRender);

}

}

/**

* Render a TableColumn with the sepecified Cell Renderer

* @param col TableColumn

* @param cellRender TableCellRenderer

*/

public void renderColumn(TableColumn col, TableCellRenderer cellRender)

{

try{

col.setCellRenderer(cellRender);

}catch(Exception e){System.err.println("Error rendering column: [HeaderValue]: "+col.getHeaderValue().toString()+" [Identifier]: "+col.getIdentifier().toString());}

}

}

这是我的屏幕

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值