0

I have created my own JTable I wanted it to contain:

JLabel, JColorChooser, JCheckBox in this order in every column, but I have no idea how to add any of these to my JPanel. Furthermore I in the 3rd collumn I added Booleans that are shown as a checkboxes inside the table, when I click them they do not edit(false -> true, true->false). I set my all cellse for editable.

I have JTable:

import javax.swing.JTable;
import javax.swing.table.TableModel;


public class MyJTab extends JTable {
        public MyJTab(TableModel model) {
            super(model);       
        }
}

and the Model:

import java.util.ArrayList;

import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JLabel;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;


public class MyTableModel extends AbstractTableModel {
    private String[] columnNames = {"ksztalt", "kolor", "stan watku"};
    private ArrayList<RowModel> data;

    public MyTableModel() {
        data = new ArrayList<RowModel>();

    }

    public void addRow(RowModel row){
        data.add(row);
    }
    @Override
    public Class<?> getColumnClass(int columnIndex) {

    /*  return data.get(0).getElement(columnIndex).getClass();*/
        if(columnIndex == 0)
            return JLabel.class;
        if(columnIndex == 1)
            return  JColorChooser.class;
        if(columnIndex == 2)
            return Boolean.class;
        return null;
    }

    @Override
    public int getColumnCount() {
         return columnNames.length;
    }

    @Override
    public String getColumnName(int columnIndex) {
        return columnNames[columnIndex];
    }

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

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
          return data.get(rowIndex).getElement(columnIndex);
    }

    @Override
    public void setValueAt(Object val, int row, int col) {      
          data.get(row).setValue(col, val);
          fireTableCellUpdated(row, col);   
    }

     public boolean isCellEditable(int row, int col) {
             return true;        
     }

}

And the row Model:

import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JLabel;


public class RowModel {
Przesuwacz p;
JLabel iconLabel;
JColorChooser colorChooser;
Boolean isMoving;

private final static int ICON_LABEL = 0, COLOR_CHOOSER = 1, CHECK_BOX = 2;
    public RowModel(Przesuwacz p) {
        this.p = p;
        isMoving = new Boolean(true);
        ImageIcon icon =  new ImageIcon(p.getF().getClass().getName().toLowerCase() +".jpg");
        iconLabel = new JLabel();
        iconLabel.setIcon(icon);

    }
    public Object getElement(int columnIndex) {
        if(columnIndex == ICON_LABEL)
            return iconLabel;
        if(columnIndex == COLOR_CHOOSER)
            return colorChooser;
        if(columnIndex == CHECK_BOX)
            return isMoving;
        return null;
    }

    public void setValue(int col, Object val) {

    }

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user2398815
  • 421
  • 1
  • 4
  • 5
  • 1
    Well, to start with, the `setValue` method of you `RowModel` doesn't do anything, so it's not recording any changes, therefore the values never change. There's also no default cell renderer/editor for `JColorChoorser`, so you're going to have to provide both of those implementations your self. To my mind, it seems weird that each row would have a color chooser. Instead, I probably have maintain a `Color` – MadProgrammer May 25 '13 at 10:56
  • Also, don't store a JLabel in the model. Store the Icon you want to display. JTable provides support to display an Icon on a JLabel by default. – camickr May 25 '13 at 15:07

0 Answers0