Pessoal to com dificuldade pra conseguir fazer uma Jtable,
que tem na primeira coluna um checkbox para selecionar
todas as linhas , segue meu código.
visualmete ta tudo legal mais quando seleciona, só da pra seleciona uma linha,
já tentei varias alternativa e nada, alguem da um luz aii
/**
* Esta classe representa o Model utilizados na Jtable que tenha Cod e Descr
*/
class ModelTableEntity extends AbstractTableModel {
// private List<PanelEntityCommand> listSelected;
private static final long serialVersionUID = 1L;
private List<PanelEntityCommand> listRows;
private String[] listColumm;
// Construtor principal ModelTableMaterial
public ModelTableEntity(List<PanelEntityCommand> listRows, String[] listColumm) {
if(listRows.size()==0){
PanelEntityCommand PanelEntityCommand = new PanelEntityCommand(new PanelSimpleEntity());
PanelEntityCommand.getEntityData().setDescr("Nenhum registro na base de dados...");
listRows.add(PanelEntityCommand);
}
this.listRows = listRows;
this.listColumm = listColumm;
}
@Override
public int getColumnCount() {
return listColumm.length;
}
@Override
public int getRowCount() {
return listRows.size();
}
public Class<?> getColumnClass(int columnIndex) {
if (columnIndex == 0) {
return Boolean.class;
}
return super.getColumnClass(columnIndex);
}
@Override
public Object getValueAt(int row, int columm) {
switch (columm) {
// case 0:
// return new Boolean(false);
case 1:
return listRows.get(row).getEntityData().getCode();
case 2:
return listRows.get(row).getEntityData().getDescr();
case 3:
return listRows.get(row).getCommandText();
}
return null;
}
//Metodo que define as colunas que será editavel
public boolean isCellEditable(int rowIndex, int colIndex) {
if (colIndex==0){
return true; // Disallow the editing of any cell
} else {
return false; //Disallow the editing of any cell
}
}
// Nomes das colunas
public String getColumnName(int column) {
return listColumm[column];
}
// Seta o objeto que vem Jtable
public void setValueAt(Object obj, int rowIndex, int colummIndex) {
switch(colummIndex){
case 0 : {
checkBox = (Boolean) obj;// minha idéia é pode recuperar esse valor pra min monta uma lista com as linha selecionadas
}
}
}
}// fim da Classe
as outras classes que uso
class MyItemListener implements ItemListener {
public void itemStateChanged(ItemEvent e) {
Object source = e.getSource();
if (source instanceof AbstractButton == false)
return;
boolean checked = e.getStateChange() == ItemEvent.SELECTED;
for (int x = 0; x < getTableMaterial().getRowCount(); x++) {
getTableMaterial().setValueAt(new Boolean(checked), x, 0);
for(int i=1 ;i<=3;i++){
listRowsSelection.add((PanelEntityCommand) getTableMaterial().getValueAt(x, i));
}
}
}
}
/**
* Classe que implementa o checkbox no cabeçalho da tabela
*/
class CheckBoxHeader extends JCheckBox implements TableCellRenderer, MouseListener {
protected CheckBoxHeader rendererComponent;
protected int column;
protected boolean mousePressed = false;
public CheckBoxHeader(ItemListener itemListener) {
rendererComponent = this;
rendererComponent.addItemListener(itemListener);
}
public Component getTableCellRendererComponent(JTable table,Object value, boolean isSelected, boolean hasFocus, int row,int column) {
if (table != null) {
JTableHeader header = table.getTableHeader();
if (header != null) {
rendererComponent.setForeground(header.getForeground());
rendererComponent.setBackground(header.getBackground());
rendererComponent.setFont(header.getFont());
header.addMouseListener(rendererComponent);
}
}
setColumn(column);
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
return rendererComponent;
}
protected void setColumn(int column) {
this.column = column;
}
public int getColumn() {
return column;
}
protected void handleClickEvent(MouseEvent e) {
if (mousePressed) {
mousePressed = false;
JTableHeader header = (JTableHeader) (e.getSource());
JTable tableView = header.getTable();
TableColumnModel columnModel = tableView.getColumnModel();
int viewColumn = columnModel.getColumnIndexAtX(e.getX());
int column = tableView.convertColumnIndexToModel(viewColumn);
if (viewColumn == this.column && e.getClickCount() == 1 && column != -1) {
doClick();
}
}
}
public void mouseClicked(MouseEvent e) {
handleClickEvent(e);
((JTableHeader) e.getSource()).repaint();
}
public void mousePressed(MouseEvent e) {
mousePressed = true;
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
vlw =)