如何渲染JTable中的复选框? [英] How to render a checkbox in a JTable?

查看:167
本文介绍了如何渲染JTable中的复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码来渲染一个JTable并改变行的颜色,但它不会在第6列显示一个复选框,只有string(true,false)。



您可以提供解决方案来解决此问题吗?



提前感谢。

 code> public class JLabelRenderer extends JLabel implements TableCellRenderer 
{
private MyJTable myTable;
/ **
*创建一个自定义JLabel单元格渲染器
* @param你的JTable实现,它保存Hashtable来查询
*行和绘制的颜色。
* /
public JLabelRenderer(MyJTable t)
{
this.myTable = t;
}

/ **
*返回用于绘制单元格的组件。这个方法是
*,用于在绘制之前适当地配置渲染器。
* see TableCellRenderer.getTableCellRendererComponent(...);更多关于方法的注释
* /
public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int column)
{
setOpaque ); // JLabel在默认情况下不是不透明的

setText(value.toString());
setFont(myTable.getFont());

if(!isSelected)//如果没有选择行,使用自定义颜色
setBackground(myTable.getRowToPaint(row));
else //如果选择行使用默认选择颜色
setBackground(myTable.getSelectionBackground());

//可以使用另一个Hashtable更改前台...
setForeground(myTable.getForeground());

//因为渲染器是一个组件,返回自己
return this;
}

//以下方法覆盖默认的性能原因
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。摆动; 
import javax.swing.table。*;
import java.util.Hashtable;
import java.awt.Color;

public class MyJTable extends JTable
{
Hashtable rowsToPaint = new Hashtable(1);

/ **
*默认构造函数
* /
public MyJTable()
{
super();
}

/ **
*设置TableModel,然后使用自定义单元格渲染器渲染每个列
* @param tm TableModel
* /
public void setModel(TableModel tm)
{
super.setModel(tm);
renderColumns(new JLabelRenderer(this));
}

/ **
*添加一个新条目,指示:
* @param row要绘制的行 - 第一行= 0;
* @param bgColor背景颜色
* /
public void addRowToPaint(int row,Color bgColor)
{
rowsToPaint.put(new Integer(row),bgColor );
this.repaint(); //你需要重新绘制每个你放在哈希表中的表。
}

/ **
*返回用户选择的BG颜色或默认BG颜色。
* @param row要绘制的行
* @return Color BG用户为行选择的颜色
* /
public Color getRowToPaint(int row)
{
Color bgColor =(Color)rowsToPaint.get(new Integer(row));
return(bgColor!= null)?bgColor:getBackground();
}

/ **
*使用指定的单元格渲染器渲染所有列
* @param cellRender TableCellRenderer
* /
public void renderColumns(TableCellRenderer cellRender)
{
for(int i = 0; i {
renderColumn(this.getColumnModel ).getColumn(i),cellRender);
}
}

/ **
*使用指定的单元格呈现器呈现一个TableColumn
* @param col TableColumn
* @param cellRender TableCellRenderer
* /
public void renderColumn(TableColumn col,TableCellRenderer cellRender)
{
try {
col.setCellRenderer(cellRender); $ col.getDirectory();};}}}}}}}}}}}}; .toString());}
}
}


解决方案

你可能知道,JTable将为你提供布尔值作为复选框。我想你的问题是,开箱即用,你不能根据数据中的特定条件每行设置自定义背景颜色。您可以为布尔列创建一个新的 TableCellRenderer



您有几个选项:


  1. 在当前渲染器中测试以确定传递的是否为布尔值,如果是,则配置 JCheckbox 实例被返回。这可能会影响你想要的,但你需要小心,因为你的渲染器经常调用,如果你创建一个 JCheckbox es,它可能会导致很多或者,您可以创建一个新的 TableCellRenderer ,以扩展 JCheckbox


  2. (正如你当前的扩展 JLabel )。你想重构​​你当前的着色逻辑,使它可以在两个渲染器之间共享。 ,您可以将此渲染器与您的列相关联。您可以通过将其设置为表上的默认渲染器来为某个 myTable.setDefaultRenderer(Class,TableCellRenderer)),或将其设置为特定列的渲染器( myTable.getColumnModel()。getColumn(int).setCellRenderer(TableCellRenderer)



This is my code to render a JTable and change the color of rows, but it doesn't show a checkbox in column 6, only string (true,false).

Can you provide a solution to fix this?

Thanks in advance.

    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) {}
}

This is the table:

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<this.getModel().getColumnCount(); 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());}
  }
}

here is my screen

解决方案

As you probably know, JTable will render boolean values as checkboxes for you. I suppose that your problem is that, out of the box, you cannot set custom background color per row based on specific criteria in your data. You can create a new TableCellRenderer for your boolean column.

You have a couple options:

  1. you can put a test in your current renderer to determine whether the value passed in is boolean or not, and if so, configure a JCheckbox instance to be returned. This could effect what you want, but you would need to be careful, as your renderer is called often and if you create one-off JCheckboxes, it could cause a lot of churn.

  2. alternatively, you could create a new TableCellRenderer that extends JCheckbox (just as your current one extends JLabel. You would want to refactor your current coloring logic such that it could be shared between the two renderers. Finally, you would want to associate this renderer with your column. You can either do this by setting it as the default renderer on the table for a certain Class (myTable.setDefaultRenderer(Class, TableCellRenderer)), or setting it as the renderer for a specific column (myTable.getColumnModel().getColumn(int).setCellRenderer(TableCellRenderer))

这篇关于如何渲染JTable中的复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆