1

我有一个jTable两列。第一列设置为布尔值(用于复选框),第二列具有字符串值。当我使用时Netbeans,复选框被添加到第一列的所有行中。我试图只为那些在第二列中具有值的行添加它。我用代码来尝试,

private class CustomCellRenderer extends DefaultTableCellRenderer {

  /* (non-Javadoc)
   * @see javax.swing.table.DefaultTableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
   */

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

    Component rendererComp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus,row, column);

             for(int i=row;i<jTable1.getRowCount();i++){

                if(jTable1.getValueAt(i, 1)==null){
                   jTable1.setValueAt(true, i, 0);
                    //checkbox.setOpaque(false);


                }
            }

        return this ;
  }

 }

如果我尝试为上述 for 循环中的所有其他复选框设置值“true”,它运行良好。如何将其设置为对其余行不可见。

编辑:

我在这个添加我的代码

   package e2;

    import java.awt.Component;
    import javax.swing.JCheckBox;
    import javax.swing.JTable;
    import javax.swing.table.TableCellRenderer;
    public class JTable_CheckBox extends javax.swing.JFrame {

        /** Creates new form JTable_CheckBox */

        JCheckBox checkbox=new JCheckBox();

        public JTable_CheckBox() {
            initComponents();
               jTable1.setValueAt("John",0,1);
               jTable1.setValueAt("James",1,1);
               jTable1.setValueAt("Janet",2,1);
               jTable1.setValueAt("Tom",3,1);

    jTable1.getColumnModel().getColumn(0).setCellRenderer(new CheckboxCellRenderer());
        }


     public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new JTable_CheckBox().setVisible(true);
                }
            });
        }

        public class CheckboxCellRenderer extends JCheckBox implements TableCellRenderer {

        public CheckboxCellRenderer() {
            setOpaque(false);
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                //here i am trying to set check box invisible,but here i am setting as selected

           for(int i=row;i<table.getRowCount();i++){

                    if(table.getValueAt(i, 1)==null){
                       table.setValueAt(true, i, 0);
                        //checkbox.setOpaque(false);

                    }
                }
            return this;
            }

        }
     // Variables declaration - do not modify
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTable jTable1;
        // End of variables declaration

    }
4

4 回答 4

4

首先,您必须了解渲染器/编辑器机制。为此,请阅读Swing 标签描述中引用的在线教程中的相应章节。

其次,为了获得最佳视觉外观,通过重新使用 JTable 本身安装的默认值来实现自定义渲染器,例如:

public class CompoundRenderer implements TableCellRenderer {

    TableCellRenderer booleanRenderer;
    TableCellRenderer defaultRenderer;

    public CompoundRenderer(TableCellRenderer booleanRenderer, 
        TableCellRenderer defaultRenderer) {
        this.booleanRenderer = booleanRenderer;
        this.defaultRenderer = defaultRenderer;
    }
    @Override
    public Component getTableCellRendererComponent(..., Object value, ....) {
        if (value instanceof Boolean) {
            return booleanRenderer.getTableCellRendererComponent(...);
        }
        return defaultRenderer.getTableCellRendererComponent(...);
    }
}

// usage
JTable myTable ...
TableCellRenderer compound = new CompoundRenderer(
       myTable.getDefaultRenderer(Boolean.class), myTable.getDefaultRenderer(Object.class));
// either register for all booleans
myTable.setDefaultRenderer(Boolean.class, compound);
// or register for a particular column
myTable.getColumnModel().getColumn(someIndex).setCellRenderer(compound); 
于 2012-07-04T14:24:32.483 回答
2

TableCellRenderer 用于表格中,以重用同一组件来绘制表格的各个单元格。表格自动设置适当的上下文、组件边界并getTableCellRendererComponent为需要绘制的表格的每个单元格调用重新粉刷)。

因此,在您的 TableCellRenderer 中,您当然不需要遍历表的单元格来尝试设置值。这没有任何意义。请记住,您正在实现TableCellRender具有您需要遵守的合同的接口。

来自 Javadoc:

返回用于绘制单元格的组件。此方法用于在绘制之前适当地配置渲染器。

因此,您需要做的就是返回一个value正确表示参数的组件。可能,您可以修改组件以指示单元格是否具有焦点,以及它当前是否被选中。不多也不少。

现在,来回答您的问题:如果您只想为具有值的行显示复选框,请实现 TableCellRenderer 返回一个复选框并根据第 1 列(第二列)中的值修改其可见性。

这个例子有效:

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;

public class TestTable {

    public class CheckboxCellRenderer extends JPanel implements TableCellRenderer {

        private JCheckBox checkBox;

        public CheckboxCellRenderer() {
            super(new FlowLayout(FlowLayout.CENTER, 0, 0));
            setOpaque(false);
            checkBox = new JCheckBox();
            checkBox.setOpaque(false);
            checkBox.setHorizontalAlignment(JCheckBox.CENTER);
            add(checkBox);
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            Object valueInColumn1 = table.getValueAt(row, 1);
            checkBox.setVisible(valueInColumn1 != null);
            checkBox.setSelected(value instanceof Boolean && (Boolean) value);
            return this;
        }
    }

    private JFrame f;
    private JTable table;

    private class MyObject {
        private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

        private String value;

        private boolean selected;

        public MyObject(String value) {
            this.value = value;
        }

        public PropertyChangeSupport getPropertyChangeSupport() {
            return propertyChangeSupport;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
            propertyChangeSupport.firePropertyChange("value", null, value);
        }

        public boolean isSelected() {
            return selected;
        }

        public void setSelected(boolean selected) {
            if (this.selected != selected) {
                this.selected = selected;
                propertyChangeSupport.firePropertyChange("selected", !selected, selected);
            }
        }

    }

    protected void initUI() {
        List<MyObject> objects = new ArrayList<TestTable.MyObject>();
        for (int i = 0; i < 200; i++) {
            MyObject object = new MyObject(i % 3 == 1 ? null : "Row " + (i + 1));
            objects.add(object);
        }
        table = new JTable(new MyTableModel(objects));
        table.setRowHeight(20);
        table.getColumnModel().getColumn(0).setCellRenderer(new CheckboxCellRenderer());
        f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new JScrollPane(table), BorderLayout.CENTER);
        f.pack();
        f.setVisible(true);

    }

    public class MyTableModel extends AbstractTableModel implements PropertyChangeListener {

        private List<MyObject> objects;

        public MyTableModel(List<MyObject> objects) {
            super();
            this.objects = objects;
            for (MyObject object : objects) {
                object.getPropertyChangeSupport().addPropertyChangeListener(this);
            }
        }

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getSource() instanceof MyObject) {
                int index = objects.indexOf(evt.getSource());
                fireTableRowsUpdated(index, index);
            }
        }

        @Override
        public int getColumnCount() {
            return 2;
        }

        @Override
        public int getRowCount() {
            return objects.size();
        }

        public MyObject getValueAt(int row) {
            return objects.get(row);
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            switch (columnIndex) {
            case 0:
                return getValueAt(rowIndex).isSelected();
            case 1:
                return getValueAt(rowIndex).getValue();
            }
            return null;
        }

        @Override
        public void setValueAt(Object value, int rowIndex, int columnIndex) {
            if (columnIndex == 0) {
                getValueAt(rowIndex).setSelected(Boolean.TRUE.equals(value));
            }
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return columnIndex == 0 && getValueAt(rowIndex).getValue() != null;
        }

        @Override
        public Class<?> getColumnClass(int column) {
            switch (column) {
            case 0:
                return Boolean.class;
            case 1:
                return String.class;
            }
            return Object.class;
        }

        @Override
        public String getColumnName(int column) {
            switch (column) {
            case 0:
                return "Selected";
            case 1:
                return "Value";
            }
            return null;
        }

    }

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
            UnsupportedLookAndFeelException {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestTable().initUI();
            }
        });
    }

}

PS:我没有考虑 isSelected/hasFocus,如果你愿意,我们可以稍后再谈。

于 2012-07-04T12:21:23.987 回答
2

俗话说:多条路通罗马。

我刚刚遇到了类似的问题,即我有一个表,其中有一列包含复选框,我想隐藏第一行中的复选框。在评估了其他解决方案后,我想出了一些稍微不同的东西(适用于原始问题):

创建一个特定的 CheckBoxRenderer 类:

public class CheckBoxRenderer extends JCheckBox implements TableCellRenderer {

    CheckBoxRenderer() {
      setHorizontalAlignment(JLabel.CENTER);
    }

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

      Object valueInColumn1 = table.getValueAt(row, 1);
      if (valueInColumn1 == null) {
          JLabel x=new JLabel();
          return x;
      } 
      setSelected((value != null && ((Boolean) value).booleanValue()));
      return this;
    }
}

第 2 步:将渲染器附加到 te 列:

paramTable.getColumnModel().getColumn(3).setCellRenderer(new CheckBoxRenderer());

就是这样。

于 2017-03-15T15:41:23.937 回答
0

我知道这是一个旧线程,但我有类似的需求,发现 sers 的回答很有帮助。我发现我需要添加的一个附加功能是创建一个 CheckBoxEditor。否则,当我单击单元格时,我会看到复选框暂时出现。所以我添加了这样的东西:

public class CheckBoxEditor extends DefaultCellEditor
{
   private JCheckBox checkBox = new JCheckBox();

   CheckBoxEditor()
   {
      super( checkBox );
   }

   @Override
   public Component getTableCellEditorComponent(
                       JTable  table,
                       Object  value,
                       boolean isSelected,
                       int     row,
                       int     column )
   {
      if ( row != 1 )
      {
         return super.getTableCellEditorComponent(
                        table, value, isSelected, row, column );
      }
      else
      {
         return null;
      }
   }
}

然后你会做类似的事情:

paramTable.getColumnModel().getColumn(3).setCellEditor( new CheckBoxEditor() );

无论如何,为我工作。

另一个注意事项是,如果您的单元格有背景颜色,您可以通过添加以下行来扩展 sers 答案:

x.setOpaque( true );
x.setBackground( MYCOLOR );

将 JLabel 设置为不透明是关键,这可以在有关类似主题的其他 Stack Exchange 答案中找到。

于 2017-09-30T16:35:35.610 回答