How to either disable or set the last-selected JTable element? - java

After looking through the JTable API I didn't see anything about this but I didn't really know what to look for, anyways take a look at this picture:
See how the "Failed" in the Status column has a blue outline around it, indicating that it was the most recently selected/the mouse was over it when the user stopped dragging. I want to either be able to: 1) disallow the JTable to have a single last-selected-element or 2) be able to set it myself. How do I do that?
If it helps, the reason I want either one of those is that I am refreshing the table which means I need to reapply the selected rows, however when I do so this last-selected-element thing gets lost.
Edit: I would actually really rather disallow the JTable from having a "lead selection index" as it is called, how do I do that?

That cell indicates which cell has focus as indicated by the following methods:
int row = table.getSelectedRow();
int column = table.getSelectedColumn();
I want to either be able to set it myself
You can set the selected cell and selected rows with code like:
table.changeSelection(6, 2, false, false);
ListSelectionModel lsm = table.getSelectionModel();
lsm.setSelectionInterval(3, 6);
or
ListSelectionModel lsm = table.getSelectionModel();
lsm.setSelectionInterval(3, 6);
table.changeSelection(6, 2, false, true);
Edit:
It appears you need to get the row/column from the appropriate selection model:
int row = table.getSelectionModel().getLeadSelectionIndex();
int column = table.getColumnModel().getSelectionModel().getLeadSelectionIndex();

Related

JTable Cell with window focus in JAVA Swing

I have created JTable with TableModel in which I make column Product Id as editable. This jtable is shown when i click one cashMemoBtn and if i type product id in first row and hit enter key then it is adding new empty row to jtable. But my problem is when new Empty row is added then Product Id Cell should have blicking cursor like we see in any editor OR in textfield.
I have attached my expected output screenshot here
In image you can see vertical bar which is indicating current focus but i want it should be automatic for all new rows when i add new rows.
I tried with
jTable.setEditingColumn(1);
jTable.setEditingRow(0);
But is not working as per my requirement. Please help.
Thanks in advance.
After you add the empty row of data to the table you then need to start editing on the desired cell.
The basic code would be:
if (table.editCellAt(row, 1))
{
Component editor = table.getEditorComponent();
editor.requestFocusInWindow();
}

Displaying just one value in JComboBox when one is selected

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.

How can I deselect one row in JTable?

I have JTable with ListSelectionModel.MULTIPLE_INTERVAL_SELECTION:
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
When I use CTRL+mouse click, rows are selected and all are right. But if I want to deselect only one selected row and press CTRL+mouse click to one selected row, all rows are deselected, not only one. Then if I press CTRL+mouse click to some row again, previouse rows are selected again, but without row, that I wanted to deselect.
I want to deselect only one row using CTRL+mouse click. How can I do that?
EDIT:
I had this in my code:
table.getColumnModel().setColumnSelectionAllowed(true);
I found that if I remove this line then all works fine. But can JTable work correctly although 'ColumnSelectionAllowed' is true?
Try method clearSelection on JTable .
it works by calling clearSelection on the ListSelectionModel
If you are looking for deselection one row from many, use the below code:
table.getSelectionModel().removeSelectionInterval(rowFrom, rowTo);
for deselecting one row:
table.getSelectionModel().removeSelectionInterval(row, row);
you can use multiple selection like this
table.getSelectionModel().removeSelectionInterval(1, 3); //1,2,3 starting from 0
table.getSelectionModel().removeSelectionInterval(5, 5); //5
to add it back, use:
table.getSelectionModel().addSelectionInterval(rowFrom, rowTo);
you can use multiple selection like this
table.getSelectionModel().addSelectionInterval(1, 2); //1,2
table.getSelectionModel().addSelectionInterval(4, 4); //4
to set it to specific range, which will clear your old selection first,
table.getSelectionModel().addSelectionInterval(rowFrom, rowTo);
Which it is equal to:
table.clearSelection();
table.getSelectionModel().addSelectionInterval(rowFrom, rowTo);

How to hide a particlar column in DefaultTableModel from displaying it in table?

I am using Java Swingx framework. I have 4 columns in my DefaultTableModel object. I wish to display only 3 of the columns. But, I need all four for computing.
Actual data model
S.No. | ID | GDC ID | Decsription
What I want to display in table
S.No.| GDC ID | Decsription
Is it possible to hide or omit only one column from rendering? Please guide me.
by default minimum size is 10 pixels widht,
you can to remove / add column from JTable view, column presents in the XxxTableModel, you can to hide and show any of column(s)
No need to adjust your model, or to try to make that column very small. JTable has built-in functionality for this: the removeColumn method. As stated in the javadoc of that method
Removes aColumn from this JTable's array of columns. Note: this method does not remove the column of data from the model; it just removes the TableColumn that was responsible for displaying it.
Also note the existence of the following methods:
JTable#convertColumnIndexToModel
JTable#convertColumnIndexToView
Since the column order and column count in the view (the JTable) might be different from the one in the model you need those methods to switch between view and model
You can hide it by setting its width to 0.
_table.getColumn("ID").setPreferredWidth(0);
_table.getColumn("ID").setMinWidth(0);
_table.getColumn("ID").setWidth(0);
_table.getColumn("ID").setMaxWidth(0);
try this to remove a single column:
myTableModel = new DefaultTableModel();
myTableModel.setColumnIdentifiers(new Object[]{"S.No.", "ID", "GDC ID", "Decsription"});
JTable myTable = new JTable(myTableModel);
// remember to save the references
TableColumn myTableColumn0 = guiLoteryNumbersTable.getColumnModel().getColumn(0);
TableColumn myTableColumn1 = guiLoteryNumbersTable.getColumnModel().getColumn(1);
TableColumn myTableColumn2 = guiLoteryNumbersTable.getColumnModel().getColumn(2);
TableColumn myTableColumn3 = guiLoteryNumbersTable.getColumnModel().getColumn(3);
myTable.getColumnModel().removeColumn(myTableColumn1);
Then to show the column again keeping the order:
// 1- remove all the existent columns
myTable.getColumnModel().removeColumn(myTableColumn0);
myTable.getColumnModel().removeColumn(myTableColumn2);
myTable.getColumnModel().removeColumn(myTableColumn3);
// 2- add all the columns again in the right order
myTable.getColumnModel().addColumn(myTableColumn0);
myTable.getColumnModel().addColumn(myTableColumn1);
myTable.getColumnModel().addColumn(myTableColumn2);
myTable.getColumnModel().addColumn(myTableColumn3);
Sorry, but that's the best way I know.
You manipulate the getValueAt , getColumnCount to achieve this.
So for example the getColumnCount you give it as 3
and on getValueAt - to skip if the column index is above the skipped column
JTable will first call getColumnCount and getRowCount and fetch calling getValueAt for each of the cells.
NOTE: Ignore this and see link by trashgod below.

Item always selected in JTable

I have created a JTable where the included items are provided with radio buttons in order to enable users to select any of these once at a time. In order to show always the first item selected when the JTable is initialised (or when the item previously selected has been deleted) what method should I use?
You should see the JTable Javadoc and particularly at :
getCellRect(int row, int column, boolean includeSpacing)
and
scrollRectToVisible(Rectangle aRect)
Something like
table.scrollRectToVisible(table.getCellRect(table.getSelectedRows()[0], 0, true));
should suit you.

Categories

Resources