Auto edit cell in nebula nattable - java

My nattable is a tree table that has custom root objects. I want to edit a cell in the table. The second column is static. But row index is dynamic. Im not able to get the correct row index for nattable edit after I add an entry into the table. The new row is added based on dialog selection.
Code:
nattableprovider.getSelectionProvider().setSelection(new StructuredSelection(obj));
SelectionLayer selLayer = nattableprovider.getBodyLayer().getSelectionLayer();
PositionCoordinate pos = selLayer.getSelectionAnchor();
nattable.doCommand(newEditCellCommand(nattable, nattable.getConfugRegistry(),nattable.getCellByPosition(2,pos.rowPositon)

First, I don't see a question. You should phrase a question if you expect an answer.
Second, you show some code that probably should trigger a selection and then activate the edit mode, but you do not show the NatTable composition. There is typically a correlation.
Third, not sure if you understand the difference of row index vs. row position. Especially because you retrieve the row position from the SelectionLayer and then want to use it on the NatTable layer. Typically there is a transformation in between, at least if you have a column header. But as you don't give an information on your NatTable composition, hard to tell.
Also if you want to edit something that is not visible, the activation of the edit mode is not possible. But again, hard to say without more details. There could be multiple issues. But I suppose each is somehow related to incorrect position handling.

Related

Options menu for one cell in java table

Welcome
In this table, I made the column mentioned cells editable to let the users make the needed value as they want ...
But they told me the values of this column can take just one of the values: 25,50,75,100, so, they asked me I make an options menu (like it is mention in the pic) allow them to choose directly the needed value and working fast.
So, how can I solve it, please!
(note: I'm working with java swing)
It's not the same question, but you can take a look on solution from below question:
How do I create a right click context menu in Java Swing?
you should create such listener, and add it to your Cell / Table.
you can take a look on below question to understand how to add listener to your Cell (it's not obvious :/)
JTable cell listener?

ColumnHideCommand makes the table columns to disappear in NatTable, when all table elements are selected

I have a NatTable component with the following layers:
ViewportLayer
SelectionLayer
RowHideShowLayer
ColumnGroupExpandCollapseLayer
ColumnHideShowLayer
DataLayer
I need to show/hide a specific column, when a checkbox selection changes. In order to that, I use the #doCommand() method provided by the NatTable component:
if(selection) {
nattable.doCommand(new ColumnShowCommand(nattable, COLUMN_INDEX));
} else {
nattable.doCommand(new ColumnHideCommand(nattable, COLUMN_INDEX+1));
}
Everything works just fine, excepting the case when ALL the items in the table are selected, and the ColumnHideCommand is executed. On this specific scenario, the whole table content disappears (Screenshot). If there is no selection in the table, or not all the elements are selected, then everything works just fine.
Please let me know if you have any idea what's going on there or if you experienced this kind of issues before. My experience with NatTables is quite limited, so please let me know if you need any additional information. Thank you!
This is a feature of the SelectionLayer to support multi column hide operations based on the column selection. A ColumnHideCommand get consumed and instead a MultiColumnHideCommand is created and executed based on the fully selected columns. The code in charge is located in SelectionLayer#handleColumnHideCommand(ColumnHideCommand). The method is protected, so if you don't need that feature because you only support column hide/show programmatically and not via UI performed by a user, you can override the method to simply perform a super.doCommand(command); without the check for selections.

how can i programatically select a particular text from JTable when i do search in it?

here are the screenshots of the application
Rows will be displayed in the Table according to the text which is written in the search textfield.
Now i want to mark that particular text as per the shown in the second image with the yellow color
I know how to select a row or a particular cell.
but I don't know how to select a particular text inside the cell of any row in the table.
I am guessing you know how to search in JTable, so I am not pasting code of it here.
You can look over the SwingX library. It has this kind of function as you said predefined it it. You just need to add it to your table. This is where you can find it. Give it a try you will surely like it.
The basic premise would be to use a custom TableCellRenderer that provided the functionality that you require.
The problem is how to implement it.
I would create a TableCellRenderer based on a JTextField, remove it's border and make it transparent. This will allow you to use the text highlighting functionality provided by JTextCompoent to highlight portions of the text, as demonstrated here.
The next problem is then knowing what to highlight. There are a number of possibilities.
You could provide a method in your table model that could return the current text that should be highlighted.
I'd, personally, probably use the JTable#putClientProperty and JTable#getClientProperty methods to seed the search text.
Or, you could actually provide a simple model that directly to the renderer which had a method that returned the current search text. This might actually be more useful as you could link it to field, the method building the filter and the renderers and allow them to simply seed each other

set scroll bar position in JTable/SWT Table

I would like to ask question that is not directly related to any technology but it’s a logic level question. Forgive me if I am wrong.
I am trying to solve this issue in Java side.
We have a list view (may be JTable or SWT Table). We are displaying records from a table in to that list. The table may have millions of records. And the table size is keeps increasing. As the table size is huge and it keeps increasing we are keeping a cache of 3000 records and displaying the cache in the table. So at a single point only 3000 records will be there in the view. At this time, the scroll bar of the view (JTable or SWT Table) shall be set based on the cache only (in this case 3000 records).
And based on the user events like, PgUP, PgDwn, DownKey, UpKey, Mouse Scroll etc we are refreshing the cache so that the view will be refreshed.
But we need to give users an actual feel that the scroll position based on the records in the table. I think it is not possible as the scroll position is set by the OS based on the number records in the display. Can somebody can assist me with an alternate method. If I am not explained my question correctly, I will explain this in details. Please help.
Usually a scrollbar is set at the top of the list, or at the bottom, depending on if the property "AutoScroll" is set to true or false.
I believe there IS a method where you can set the scrollbar at a specific point, but last time I tried using it, it was kinda clunky.
It sounds like you're trying to make the scroll bar do two things at once, and that can be confusing. You could use a separate control to the do the paging. A row of buttons in a JToolBar looks nice, but a JComboBox will take up less room. There's some good answers about this here.

Is there anyway I can highlight a row in JTable?

I am currently building a database using JTable and DefaultTableModel. In my program I
have a function which allows users to search the database. I have the search part build but I don't
know how to highlight a row or a cell in the JTable. Can someone please help me?
Thank you
Are you sure you want to highlight as opposed to filter out the extraneous results? If you highlight, you'll have to scroll through the whole list to find all of the matching results, whereas if you filter the display, it's a lot easier to find what you're looking for.
If you go the filtering route I'd look into GlazedLists, a truly great Java library for doing things like dynamic filtering, sorting, etc. of JTables.
If you still want to go the highlighting route, then there's two main ways I see of accomplishing this. The first is to use the ListSelectionModel of the JTable and ensure that all of the matching rows are in the selected set; this will cause them to be visually distinguished with a minimum of coding. On the other hand, as soon as the user drags in the table and selects something else, the visual effect is lost.
The second way to accomplish this would be to use a custom TableCellRenderer that changes how a row is rendered if the row matches your selection criteria. An easy way to do that would be to change the background color.
The Swing tutorial on How to Use Tables has a section on filtering so you can just dislay the data that meet the search criteria. If you want to see all the data, then you just remove the filter.
If you really want to do separate highlighting then I would take a look at the Table Row Rendering approach.
I can tell you how I am doing it.
I implemented my search to work as the search in a document, i.e. finding single result at a time. I am storing the current index of the selected row or starting from the first one if no row was previously selected. Then I have my model implement my interface with functionality to search for next or previous match, example below shows use of find next match method which returns an index of the row in a table where the matching string was found, then I change the selection to it else I clear the selection to let the user know there is no match.
int index = serchableTableModel.findNextMatchIndex(serchedText, currentIndex);
if(index != -1)
table.changeSelection(index, 0, false, false);
else
table.clearSelection();
I hope this sorts your problem.
NOTE: I was not aware of the glazed lists before, they really seem promising. They would have saved me implementing sorting of tables, searching myself.

Categories

Resources