English 中文(简体)
单项选择单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单 s。
原标题:jtable checkbox single selection java swing
  • 时间:2012-01-12 07:01:31
  •  标签:
  • java
  • swing

I have a jtable. I put checkbox in a column. But the check box are in multi selection mode,ie in my jtable there are 5 checkbox. i can select 5 of 5. I want to select only 1 at a time. How can I change it to single selection?

我的法典如下:

TableColumn colTable2 = jTable2.getColumnModel().getColumn(1);

colTable2.setCellEditor(new DefaultCellEditor(jCheckBox2));
colTable2.setCellRenderer(jTable2.getDefaultRenderer(Boolean.class)); 

提前感谢。

问题回答

在您的表格模型中,当5列价值中有一个被确定为实值时,你也应确定该栏目前真实的伪造表格模型。

您也应考虑使用无线电台作为播音员和编辑,因为它是代表独特选择的最适当组成部分。

另一种办法是用单列替换这5栏,并使用bo子作为电池编辑。

我建议你采取替代做法,

查阅JRadioButton,我想这将是一个更好的选择。

Note , from How to Use Buttons, Check Boxes, and Radio Buttons

检查箱与无线电台相类似,但其选择模式因公约而有所不同。 任何一个组别中的任何检查箱——没有一个,有些或全部——都可以挑选。 另一方面,一组无线电台只能选择一个顿。

Here is a TableModel that introduces a single selection check box column at the end of an existing table. You can install it like this

CheckBoxSelectionTableModel.register(table);

您可在SimpleTable Demo上进行审判。

enter image description here

这只要求现有的表格模式成为javax.swing.table.AbstractTableModel的范例,其中80%将属于这种情况。

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

public class CheckBoxSelectionTableModel implements TableModel, ListSelectionListener {

  protected final AbstractTableModel delegate;
  protected int selectedRow = -1;
  protected final ListSelectionModel selectionModel;

  public CheckBoxSelectionTableModel(AbstractTableModel delegate, ListSelectionModel selectionModel) {
    this.delegate = delegate;
    this.selectionModel = selectionModel;
    selectionModel.addListSelectionListener(this);
  }

  public static void register(JTable table) {
    table.setModel(new CheckBoxSelectionTableModel((AbstractTableModel)table.getModel(), table.getSelectionModel()));
  }

  protected boolean isCheckBoxCloumn(int columnIndex) {
    return columnIndex == getCheckBoxColumnIndex();
  }

  protected int getCheckBoxColumnIndex() {
    return delegate.getColumnCount();
  }

  // --------------------- delegate methods --------------------- \

  public int getRowCount() {
    return delegate.getRowCount();
  }

  public int getColumnCount() {
    return getCheckBoxColumnIndex()+1;
  }

  public String getColumnName(int columnIndex) {
    return isCheckBoxCloumn(columnIndex) ? "" : delegate.getColumnName(columnIndex);
  }


  public Class<?> getColumnClass(int columnIndex) {
    return isCheckBoxCloumn(columnIndex) ? Boolean.class : delegate.getColumnClass(columnIndex);
  }

  public boolean isCellEditable(int rowIndex, int columnIndex) {
    return isCheckBoxCloumn(columnIndex) ? true : delegate.isCellEditable(rowIndex, columnIndex);
  }

  public Object getValueAt(int rowIndex, int columnIndex) {
    return  isCheckBoxCloumn(columnIndex) ?  rowIndex == selectedRow : delegate.getValueAt(rowIndex, columnIndex);
  }    

  public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
    if(isCheckBoxCloumn(columnIndex)) {
      int lastSelected = selectedRow;
      if((Boolean) aValue){
        selectedRow = rowIndex;
      } else {
        selectionModel.clearSelection();
        selectedRow = -1;
      }
      if(lastSelected > -1) {
        delegate.fireTableRowsUpdated(lastSelected, lastSelected);
      }
      delegate.fireTableRowsUpdated(rowIndex, rowIndex);
    } else {
      delegate.setValueAt(aValue, rowIndex, columnIndex);
    }
  }

  public void addTableModelListener(TableModelListener l) {
    delegate.addTableModelListener(l);
  }

  public void removeTableModelListener(TableModelListener l) {
    delegate.removeTableModelListener(l);
  }

  // --------------------- ListSelectionListener methods --------------------- \

  @Override
  public void valueChanged(final ListSelectionEvent e) {
    if(e.getValueIsAdjusting()){
      return;
    }
    int index = selectionModel.getLeadSelectionIndex();
    boolean isSelected = selectionModel.isSelectedIndex(index);
    setValueAt(isSelected ,index , getCheckBoxColumnIndex());     
  }


}

我只是想到这个问题,我来到了这个解决办法......希望它能帮助别人。

private void MiTablaMouseClicked(java.awt.event.MouseEvent evt) {                                         
    for(int i=0; i<MiTabla.getRowCount(); i++){
        if(i==MiTabla.getSelectedRow()){
            MiTabla.setValueAt(true, MiTabla.getSelectedRow(), 0);
        }else{
            MiTabla.setValueAt(false, i, 0);
        }
    }

}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签