Creating a table with an initial element selected - java

I want to create a table which displays in each row a set of data, in addition to a radio button for possible selection.
This can be achieved easily by using a simple table model class which extends the DefaultTableModel and a cell renderer and editor class which implements the TableCellRenderer and TableCellEditor interfaces respectively.
What I really couldn't manage to do is to make the table to display initially one of the elements (rows) initially selected. It seems straightforward but it isn't...does anybody have a clue about it?

Depends on your exact requirement.
table.changeSelection(2, 0, false, false);
will select the row and make the cell have focus in the table

Use the setSelectionInterval() method of the table's ListSelectionModel:
table.getSelectionModel().setSelectionInterval(4, 5);

Related

Is a custom TableCellEditor constructor only called once?

I have a JTable. One column in the JTable is assigned an extended TableCellEditor that displays an extended JComboBox.
There is a fixed list of 100 String objects that populates the comboboxes.
The challenge:
Design the JComboBoxes so that any selection is unique relative to other boxes? That is, if "A" is slected from the combobox in the first row, it is automatically removed from the list of each other combobox.
When a new room is added to the table, the combobox it contains should auto-populate to the first available list item.
The problem:
My comboboxes work beautifully. I can select items at will. I even have made some progress in eliminating already used items from the lists. But I can't figure out how to correctly auto-populate.
I am very confused because it appears that my combobox constructor is only called once when the table is created, not once for each row.
Is this the case? Is the constructor for a TableCellEditor only called once ever? If so, how do I modify the behavior of each combobox as it come into existence?
Thanks for your help!
If you would like specific code, please let me know. I don't know if you want me to paste in the whole classes.
When a new room is added to the table, the combobox it contains should auto-populate to the first available list item.
When you add a new row of data to the TableModel you must add the values of all columns in the row. This should not be a function of the editor. The editor allows you to change values in the cell.
I was able to get around my problem by creating an abstract superclass for my combobox that can be accessed from the tablemodel extension when it sets up its data.

Related to checkbox in jtable

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.

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.

Make header text bold when its column is selected in JTable

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.

Inserting radio buttons in a DefaultTableModel

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.

Categories

Resources