将行为添加到默认的JTable单元格渲染器

时间:2017-09-24 13:14:20

标签: java swing jtable tablecellrenderer

如果我使用以下内容显示JTable:

package jtable.fontsize;
import java.awt.BorderLayout;
import java.awt.Font;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

import rcutil.swing.table.LastColumnChangesWidthJTable;


public class JTableCellPlay extends JFrame
{
  public JTableCellPlay()
  {
    super("setting Table font size");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
  }

  public static void main(String ... arguments) throws InvocationTargetException, InterruptedException
  {
    JTableCellPlay mainScreen = new JTableCellPlay();
    mainScreen.go();
  }

  public void go() throws InvocationTargetException, InterruptedException
  {
    SwingUtilities.invokeAndWait(new Runnable() { public void run() { createScreen(); } } );
    setVisible(true);
  }

  public void createScreen()
  {
    LastColumnChangesWidthJTable aLittleJTable = new LastColumnChangesWidthJTable(new LittleTableModel());
    aLittleJTable.setDefaultRenderer(Integer.class, new IntegerCellRenderer());
    JScrollPane scrollPane = new JScrollPane(aLittleJTable);
    add(scrollPane, BorderLayout.CENTER);
    pack();
  }
}

使用以下渲染器:

package jtable.fontsize;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;


public class IntegerCellRenderer extends JLabel implements TableCellRenderer
{
  @Override
  public Component getTableCellRendererComponent
                      (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
  {
    String resultString = String.format("~%d~", (Integer)value);
    setText(resultString);
    setHorizontalAlignment(JLabel.CENTER);
//    if (isSelected) 
//    { 
//      setOpaque(true);  // evidently necessary for JLabel as a component in a JTable cell
//      setBackground(Color.GRAY);
//    }
    return this;
  }
}

以及以下模型(以便您的代码可以运行,是的,我知道还有其他方法可以执行此部分):

package jtable.fontsize;

import javax.swing.table.AbstractTableModel;

public class LittleTableModel extends AbstractTableModel
{
  public Class<? extends Object> getColumnClass(int column)
  {
    Class<? extends Object> c = null;
    switch (column)
    {
    case 0: c = String.class; break;
    case 1: c = Integer.class; break;
    case 2: c = String.class; break;
    }
    return c;
  }

  String[] columnNames = { "first", "second", "third" };
  Object[][] data = { { "one", 2, "three" }, { "four", 5, "six" } };

  public int getColumnCount() { return 3; }
  public int getRowCount() { return 2; }
  public String getColumnName(int index) { return columnNames[index]; }

  public Object getValueAt(int row, int column)
  {
    Object result = data[row][column];
    return result;
  }
}

然后,当我选择其中一行时,两个String列会获得一个选择突出显示,但“Integer”列则不会。

如果我注释掉设置“整数”渲染器的行,则单击某行将按预期选择整行。

我知道我可以使用渲染器中已注释掉的线来突出显示单元格,添加setOpaque(true)setBackground(Color),甚至可以玩游戏以使选定和未选择的背景颜色正确,但是我怀疑有一些方法可以使用最初用于此类事物的相同渲染器,只需使用渲染器中的代码来完成我需要做的特殊操作。有人可以解释一下它的工作原理吗?

1 个答案:

答案 0 :(得分:2)

为什么不简单地为渲染器提供一个else块来解除阻止中的更改?

class MyString {
    size_t _size = 0;
    char* _str = nullptr;
    int* _pRefCount = nullptr;


        }
        operator char() const {
            return ((const MyString&)_s)[_index];
        }
    }; 

    void detach() {

        if(_pRefCount && --*_pRefCount == 0) {
            delete []_str;
            delete _pRefCount;
        }
    }

    void attach(const MyString& str) {
        _size = str._size;
        _str = str._str;
        _pRefCount = str._pRefCount;
        ++*_pRefCount;

或者另一种选择是让你的渲染器类扩展DefaultTableCellRenderer而不是实现TableCellRenderer,允许你使用默认渲染器的固有突出显示功能:

if (isSelected) {
    setOpaque(true); 
    setBackground(Color.GRAY);
} else {
    setOpaque(false); // allow underlying color to show
    setBackground(null); // no color added
}

请注意,您可以在其构造函数中设置渲染器的某些属性。例如:

@SuppressWarnings("serial")
class IntegerCellRenderer2 extends DefaultTableCellRenderer {
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
            int row, int column) {
        String resultString = String.format("~%d~", (Integer) value);
        setText(resultString);
        setHorizontalAlignment(JLabel.CENTER);
        return super.getTableCellRendererComponent(table, resultString, isSelected, hasFocus, row, column);
    }
}