Error while trying to place Jcheckbox in a Jtable column

Asked

Viewed 252 times

0

I’m trying to add a column to a DefaultTableModel, but seems to be giving some problems. Follows code!

JCheckBox jcheck;
DefaultTableModel modelo = new DefaultTableModel(null, new String[]{"Data", "Hora", "SAP", "BPCS", "Etiqueta", "Min", "Max", "MV", "Min", "Max", "T5", "Min", "Max", "TS2", "Min", "Max", "T90", "Min", "Max", "Densidade", "C-Chart", "Blooming","Aprovar"}) {
    @Override
    public boolean isCellEditable(int row, int col) {
        return true;
    }
};

jcheck = new JCheckBox();
TableColumn coluna_um = jTable1.getColumnModel().getColumn(22);
coluna_um.setCellEditor(new DefaultCellEditor(jcheck));

Someone could give a force?

  • It is not in the tablemodel, it serves to model data, just that. To display combo in the table, you need to create a celleditor and cellrenderer. Provide a [mcve] of your code so we can help you.

  • I won’t eat :(

  • Wait, in the title you say Combobox, but in the code is Checkbox? What component are you trying to add after all?

  • It’s check, I’m going crazy already

1 answer

1


JCheckBox is a component that can be considered "boolean", ie either it is marked, or it is not. As it has only two states, when you define a column with this type in DefaultTableModel, the Nderer itself uses this component as a representation of a Boolean column, and your only job is to define which column should be of this type. See in the example below:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;


public class JTableCheckboxTest extends JFrame{

    public void createAndShowGUI() {

        Object[][] rowData = {null, null, null};
        Object[] columnNames = { "Currency Column ", "Column Two",};

        DefaultTableModel model = new DefaultTableModel(rowData, columnNames){
            @Override
            public Class<?> getColumnClass(int columnIndex) {
                return columnIndex == 0 ? Boolean.class : super.getColumnClass(columnIndex);
            }
        };

        JTable table = new JTable(model);       

        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane, BorderLayout.CENTER);
        setPreferredSize(new Dimension(300, 150));
        pack();
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String args[]) throws IllegalAccessException {
        EventQueue.invokeLater(() -> new JTableCheckboxTest().createAndShowGUI());
    }
}

Note that in the method getColumnClass() i do a check to see if the column index is 0, when it is, the column will be of the boolean type, which will make it a column of Checkboxes:

inserir a descrição da imagem aqui

One point of your code worth noting is that, by default, all columns of a DefaultTableModel are editable, so overwrite the method isCellEditable() to return true is redundant.

  • Our @Articuno, perfect guy..

  • @Lucas16 is simpler than it looks ne :p

  • Lots of haha.. now I just want to see how I’ll go through these columns and save approved for when it’s marked :S

  • Loop through all lines, but restrict only the Boolean column in the getValueAt, so you recover the value of that column.

  • vc commented on the iscelleditable, but what if I want to leave only column 22 to be edited?

  • @Lucas16 supposing that 22 is the correct column index: return col == 22;

  • Good Tips :D

  • i made a loop, but I’m having a lot of doubts.. will you n could help me in some chat, something?

  • you would have an example for combobox at jtable now? vc explained me at checkbox and it worked, but I need a comobbox at jtable now.

  • @Lucas16 https://stackoverflow.com/q/3256086/5524514

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.