Georgio
Georgio

Reputation: 55

Choose an item from JTable and put them on another

I want to add a CheckBox in JTable and when the user selects a question it will be added on another JTable but the priority is that I want to add the check box knowing that the JTable contains information taken from the database. Thanks for help . I hope that you understand me. I made models I that's what I want to have this is the models that i want to have This is the result of my code The result

 List<Question> questions=new ArrayList<>();
 JButton btnAfficher = new JButton("Afficher toutes les questions");
    btnAfficher.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            categorie=GestionCategorieDelegate.doFindCategorieById(PreparerTest.idCategorie);
            questions=GestionTestDelegate.doPrepareManuallyTest(categorie);
            initDataBindings();

        }
    });
    table_1 = new JTable();
    scrollPane_1.setViewportView(table_1);

    table = new JTable();
    scrollPane.setViewportView(table);
    setLayout(groupLayout);
    initDataBindings();

Upvotes: 1

Views: 38

Answers (1)

Priyamal
Priyamal

Reputation: 2989

CustomTableModel mymodel = new CustomTableModel();
mymodel.addRow(new Object[]{false, "2ndcoldata", "3rdcol data"});
mymodel.addRow(new Object[]{true, "2ndcoldata", "3rdcol data"});
mytable.setModel(mymodel);


public class CustomTableModel extends DefaultTableModel {

    public MyTableModel() {
      super(new String[]{"col1", "col2", "col3"}, 0);

@Override
public Class<?> getColumnClass(int columnIndex) {
  Class clazz = String.class;
  switch (columnIndex) {
    case 0:
      clazz = Boolean.class;
      break;
  }
  return clazz;
}

@Override
public boolean isCellEditable(int row, int column) {
  return column == 0;
}

@Override
public void setValueAt(Object aValue, int row, int column) {
  if (aValue instanceof Boolean && column == 0) {
    Vector rowData = (Vector)getDataVector().get(row);
    rowData.set(0, (boolean)aValue);
    fireTableCellUpdated(row, column);
  }
}}

you can do it with above approach, i have customtableModel class which extends the DefaultTableModel class. and whenever you need to add data to the your table create an instance of CustomTableModel and add your row data to the model once it is completed then set the model to your jtable.

Upvotes: 1

Related Questions