0

我正在尝试调整 jtable 单元格内的滑块大小。但是在调整列标题的大小时,我确实传入了相关代码(第一列的方法 getTableCellRendererComponent),但没有任何反应(滑块没有调整大小??)。

下面是我的渲染器的代码:

public class NavigationDataModelRenderer extends JLabel implements TableCellRenderer {

    private JSlider slider = null;
public NavigationDataModelRenderer()
{
    super();
}

public NavigationDataModelRenderer(int tolerance) 
{
    super();
    slider = new JSlider(tolerance * -3,tolerance *3);
    slider.setPaintTicks(true);
    slider.setMajorTickSpacing(tolerance);
    slider.setUI(new SpreadSliderUI(slider));
}


@Override
public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column) 
{
    Component comp = null;
    if (table.getModel()!= null && table.getModel() instanceof NavigationDataModel)
    {
        NavigationDataModel model = (NavigationDataModel) table.getModel();
        Object o = table.getModel().getValueAt(row, column);
        this.setText(o.toString());

        if (column == 1)
        {//slider
            if (model.getList() != null && model.getList().get(0)!= null && model.getList().get(row) != null)
            {
                slider.setValue((Integer)o);
            }
            comp = slider;

            slider.setSize(table.getCellRect(row, column, false).getSize());
        }
        else if (column == 2)
        {
            if (((Integer)o) ==  Integer.MAX_VALUE)
            {
                this.setText("Invalid");
            }
        }
        else
        {
            comp = this;
        }
    }
    return comp;
}

}

谢谢你的帮助。

4

2 回答 2

1

我遇到这个问题是因为我为我的滑块使用了 BasicSliderUI。现在在我的渲染器中,每次输入 getTableCellRendererComponent(...) 并调整滑块大小时,我都会为滑块设置一个新的 BasicSliderUI。

于 2012-09-24T13:14:59.267 回答
1
    public class GrowingCellRenderer extends DefaultTableCellRenderer
    {

     @Override
      public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
          int row, int column)
      {
        Component renderer = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

        //renderer.setBackground(Color.red);
        renderer.setSize(getMinimumSize());
        table.setRowHeight(row, renderer.getHeight());
        return renderer;
      }
    }

public static void main(String[] args)
{
  GrowingCellRenderer testInstance = new GrowingCellRenderer();
  final Date dateProjectStart = new Date();  //Start the "project" today
  final JTable jtab = new JTable(TableTools.createTestData(dateProjectStart));
  jtab.setDefaultRenderer(String.class, testInstance);


}
于 2015-11-07T05:54:31.780 回答