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
I have a JTable whose every cell contains a JList. What I want to do is to make that table editable so that every item on the JList can be edited either using a JTextfield or Choosing an item from another List whenever the user right clicks on that list item in a particular table cell. I also want 2 of my table column need to be set uneditable. Here is a picture of my JTable. I want the every cell and every JList item on that cell to be editable leaving the 'Batch' and 'Break' column to be uneditable.
P.S. I don't want any spoon fed code. I just want an idea how it can be done. And I'll be really grateful if you can reference me some link on the web where I can read to learn how this type of problems can be solved. Thank you..!
enter image description here
I'm developing a small game that has a highscore list displayed by a javafx TableView. I've created a subclass HighscoreTableView extends TableView which is a TableView node that automatically creates the TableColumns I need and fills them with data on construction.
I want that table to be sorted by a default column on initialisation. I've added the following code lines:
duration.setSortType(TableColumn.SortType.DESCENDING);
this.getSortOrder().add(duration);
duration.setSortable(true);
this.sort();
where duration is the TableColumn that should define the sorting. Of course, it's added to the TableView. But when I create a new instance of that HighscoreTableView, it remains unsorted by default, until the user clicks on one of the column headers. This is unexpected, since this question, this question and this question say it should work that way. Any ideas?
Further information for reproduction:
The HighscoreTableView class is used by a HighscoreStage extends Stage class, which contains a TabPane with four Tabs. Each Tab contains a HighscoreTableView with different data taken from a static Data object. The data model is a class HighscoreEntry, an ObservableList of them gets added to the HighscoreTableViews. My full code is available here.
You are calling sort() before any data is loaded. Instead you should call it each time after adding/changing data to the table view.
Also you can use SortedList with appropriate comparator to wrap your original backing list. Than all changes will be automatically propagated to view.
Hello :) I need help with changing a JComboBox in a JTable. I'm new to GUI-Programming and Swing and couldn't solve this problem: I have to change the behavior of a JComboBox.
You can see the ComboBox in the picture below. If "Ja" is selected there should just be "Nein" as an option and the other way around. It would also be cool if "Nein" is set per default. The code was written from one student from last semester and I have difficulties to adjust the combobox like I have to.
That's the code snippet where the ComboBox gets initialized.
optionsInteger = new JComboBox<String>();
optionsInteger.addItem("Ja");
optionsInteger.addItem("Nein");
optionsInteger.setSelectedItem(optionsInteger.getItemAt(0));
optionsInteger.setSelectedIndex(1);
optionsInteger.setName("optionsInteger");
The ComboBox gets inserted to a JTable in this method:
public void repaintXTable(DefaultTableModel model,JTable table, int xAmount, JScrollPane scrollPane,
JComboBox<String> optionsInteger) {
model.setRowCount(xAmount);
th = table.getTableHeader();
tcm = th.getColumnModel();
tcs = tcm.getColumns();
tcs.nextElement().setHeaderValue("");
tcs.nextElement().setHeaderValue("Lower");
tcs.nextElement().setHeaderValue("Upper");
tc = tcs.nextElement();
tc.setHeaderValue("Integer");
tc.setCellEditor(new DefaultCellEditor(optionsInteger));
for(int i=0;i<xAmount;i++)
{
model.setValueAt("X"+(i+1), i, 0);
}
}
Thank you very much for your help.
In your code, this line
optionsInteger.setSelectedItem(optionsInteger.getItemAt(0));
sets the default selection to the zeroth element (Ja). This line
optionsInteger.setSelectedIndex(1);
sets the default selection to the first element (Nein).
Either set the selected item or the selected index. There's no need to do both.
A JComboBox does not remove the selected element by default. If the selected element is removed, how would the selected element display in your JTable?
If you really want to do this, you'll have to create your own version of a JComboBox.
. If "Ja" is selected there should just be "Nein" as an option and the other way around.
So then you need two separate ComboBoxModels, one model containing "Nein" and the other containing "Ja". Then when you start to edit the cell you check the current value and use the model containing the other value.
Check out How to add unique JComboBoxes to a column in a JTable (Java). This example shows how you can dynamically change the model at the time the cell is about to be edited.
It would also be cool if "Nein" is set per default.
This has nothing to do with the editor. You just add "Nein" to the TableModel when you add the data to the model.
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.
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.