I'd like to insert a JPopupMenu for each cell of a column of my JTable (to be able to select multiple items).
Any idea how to do that?
For multiple selections within a cell, you'll need both a custom renderer and editor. For the editor component, use a JList with the selection mode set to MULTIPLE_INTERVAL_SELECTION. The renderer should display something appropriate for the content, e.g. a comma-separated list of values. Note that the data for each row must be stored in the TableModel, not in the renderer and editor itself. The details will depend on your data model, but List<String> or EnumSet<String> may be appropriate.
Related
I created a custom table, with a custom model, a custom cell renderer and a custom cell editor. Every cell of this table is a panel which contains another custom table.
The problem is: (with or without the internal table) to interact with the cell, thus to trigger the event listener of the components in that cell(which is a panel), you need to click the table before.
I imagine it wants you to "enter the table" or select the cell before you can actually interact with its components but this is not functional.
Is there a way to avoid this issue?
I am using a table to display data.
I am providing checkbox to each row of a table to perform some operations based on selection. When I did like that, I am able to check multiple rows.
But my requirement is, at any point of time I should check only one checkbox. To be precise, I need the behavior of Buttongroup to all checkboxes in table.
How can I do this?
If you really want to use checkboxes, I assume your TableModel holds a boolean for those checkboxes. It should be trivial to move the logic for the single selection to the TableModel.
If you do not need the checkboxes but just want to operate on the selected rows (see JTable#getSelectedRows), you can adjust the ListSelectionModel which is present on the JTable to only allow for single selection (see ListSelectionModel#SINGLE_SELECTION)
CheckOne is a complete example that simply clears all check boxes in a specific column and sets the new value. This related example uses JRadioButton.
how to add a jcheckbox when the table model initially is not defined.
I have populated the jtable with data from database, but now I need to add a jcheckbox used for selection of desired record.
I can add jcheckbox to jtable if the datatable rows are perdefined with boolean class. But as I am using a database to read from I am bit confused.
any help is welcome.
JTable relies on its TableModel to determine what to display. Although only existing rows can be shown, you can insert an uncommitted row in the model for editing. What and when to commit are entirely up to your application.
As discussed in How to Use Tables: Concepts: Editors and Renderers, the default renderer and editor for a column of type Boolean is a JCheckBox. Here's a simple example that conditions getColumnClass() accordingly. Here's a more complex example that defines a composite type, Value, as well as a custom renderer and editor.
is possible to turn header text bold when I select a cell of its column? How to do it?
Thanks
Leandro
Set custom renderer into table header. If current column is selected column set font to bold.
JTable table = new JTable()
table.getTableHeader().setDefaultRenderer(new MyRenderer());
class MyRenderer implements TableCellRenderer {
//todo implement
}
It is partially possible. What you have to do is write your model is such way that on table's cell selection your model executes fireTableStructureChanges method( assuming you use AbstractTableModel as a base). This will repaint the whole table including column header. All you have to do is keep the state of which column is selected.
In the beginning I said "partially" possible. That is because calling fireTableStructureChanges will revalidate the whole table and you will lose your current column model state - column widths and sequence.
To make your text bold you can use HTML - something like <html><b>your text</b></html>, but it has to change dynamically based on your model's internal state
UPDATE: Also column table header text can be set directly but model change or tableStructureChanged event will make the table to reread from the model.
I have a 2D array of objects which needs to be displayed in a table through a table model class which extends a DefaultTableModel. In addition to the columns needed to accommodate the data included in this array, I would like to add an extra column with radiobuttons, in order to enable the user to make a selection. As table model accepts only arrays of objects or vectors, how should I add radio buttons?
By default, JTable infers how to render and edit entries based on the entry's class, as discussed in the tutorial article Editors and Renderers. In this example, a checkbox allows multiple selections in a column. Substituting a radio button and using a ButtonGroup would accommodate a unique selection.