I'm looking for a way to bind the row height (or cellSize) to the amount of rows in my TableView.
The height of the row would be equal to the height of the table divided by the amount of rows.
Is there any way to do this?
I fixed it by adding the following method in my class that extends TableView:
private void fixCellSize(){
int amountOfRows = getItems().size();
double headerHeight = lookup(".column-header-background").getBoundsInLocal().getHeight();
setFixedCellSize((getHeight()-headerHeight)/amountOfRows);
}
In my constructor I added:
Platform.runLater(() -> fixCellSize());
Related
I have a TableView in my code for which I am saving the index and width of each TableColumn when the application shuts down (values are saved to a Properties file). When I start the application again, I want to reset the index and width of each TableColumns back to what is was when the application was shut down.
// If all columns are known, reorder the columns and set the width
if (allKnown) {
ObservableList<TableColumn<T, ?>> columns = FXCollections.observableArrayList();
// Column order and width
TableColumn<T, ?> column;
for (int i = 0; i < colIndex.length; i++) {
column = getOriginalColumns().get(colIndex[i]);
column.setPrefWidth(colWidths[i]);
columns.add(column);
}
getTableView().getColumns().setAll(columns);
}
getTableView().setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
Setting the index works correctly, the column is set in the correct location of the TableView. However, this does not work for the column's prefWidth (all columns are resizable). I have looked at different fora and the culprit seems to be the CONSTRAINED_RESIZE_POLICY. I want to keep that option, since the main screen of my application can be resized. If I use unconstrained, the width works, but then I loose the ability to auto-scale the TableView when the application's main window resizes.
Has anyone experienced the same and found a solution? Your help is greatly appreciated.
The only solution I have found is to initially set the minWidth instead of the prefWidth. then a few pulses after showing the UI, fix the minWidth so column resizing works normally again.
In a QTableView I need the last (rightmost) column to be empty, expandable but not movable. The goal is that the table not to suddenly end (for I use alternate color for rows) or to ugly expand to the right. In QHeaderView there is a setFirstSectionMovable(bool); I need something similar for the last section, letting the rest of them movable. (in other words: Fill the rest of the table with an empty not movable column). Any clue how to acheive this?
I did override mousePressEvent() in a subclass of QHeaderView to skip the last section but it still can be moved by moving other column in its place and I don't know how to prevent this.
AMOQ:
Be HeaderView a subclass of QHeaderView and have
setSectionsMovable(true);
setStretchLastSection(true);
Treat the sectionMoved signal:
connect(this, &QHeaderView::sectionMoved, [this](int, int, int newVisual) {
if(newVisual == count()-1) { moveSection(newVisual, newVisual-1); }
});
Override mousePressEvent:
void HeaderView::mousePressEvent(QMouseEvent* event) {
int logicalIdx = logicalIndexAt(event->pos());
if(logicalIdx != count()-1) { QHeaderView::mousePressEvent(event); }
}
Is it possible to programmatically create a Grid that has 3 rows and 2 columns, but the last row only has 1 column instead of 2?
public class MyGrid : Grid
{
public void DefineRowsAndColumns()
{
// I know you can add RowDefinitions and ColumnDefinitions here, but how to make them uneven?
}
}
I'm not trying to get someone to do my homework here...I just want to know how I can get a grid to have rows with different number of columns.
you can use the ColumnSpan property to make content span multiple columns
var label = new Label { Text = "Row 1" };
myGrid.Children.Add(label,0,0);
Grid.SetColumnSpan(label,2);
the Label will span 2 columns, effectively making that row contain only a single column
HI,
In flex, I have a datagrid with 22 columns. I initially display all the columns. The width of each column right is uniform.
Now when i change the visiblity of a few columns, the width of each column varies. How do i maintain a uniform column width for each column whether or not there are any invisible columns?...
Also how do i get the count of number of visible columns. the ColumnCount property returns total number of columns and not the number of visible ones.
The columns are an array, so you can append code to whatever function makes them invisible and loop through that array setting each ones width. You will just need to keep tabs on your Datagrids width, and the number of visible columns, which you can also do with a loop. Here's some untested code that should put you close to your goal:
function makeAColInvisible():void{
//code you use to set col invisible
var visColCount:number = 0;
for each (var item:DataGridColumn in myDataGrid.columns){
if(item.visible == true){
visColCount++;
}
}
for each (var item2:DataGridColumn in myDataGrid.columns){
item2.width = myDataGrid.width / visColCount;
}
}
In my flex app I store the widths and visiblility of columns in an xml file. When the app loads it reads from the xml file and sets he columns values as applicable:
for(i = 0; i < columnsOrder.length; i++){
newOrder[i] = myDG.columns[Number(columnsOrder[i]) - 1];
newOrder[i].visible = (Number(columnsVisiblity[i]) == 1);
newOrder[i].width = Number(columnsWidth[i]);
}
myDG.columns = newOrder;
myDG.invalidateList();
The problem appears to be setting the visibility (it sets the visible field correctly but messes up the width)... I've tried setting it after setting the width (outside of the loop) and before the loop as well. It resizes the columns properly if I don't do anything with the visibility.
Any ideas?
Add an import statement at the top of your class file:
import mx.core.mx_internal;
Then remove using the mx_internal namespace, remove the owner of the column, change the width and then reasign the parent:
public static function resizeColumn(col:DataGridColumn, size:int):void
{
var owner:* = col.mx_internal::owner
col.mx_internal::owner = null;
col.width = size;
col.mx_internal::owner = owner;
}
This ought to do the trick (well, it did for us after a couple of days of swearing)
Is you horizontalScrollPolicy set to false on the datagrid?
"If the DataGrid's horizontalScrollPolicy property is false, all visible columns must fit in the displayable area, and the DataGrid will not always honor the width of the columns if the total width of the columns is too small or too large for the displayable area."
http://livedocs.adobe.com/flex/3/langref/mx/controls/dataGridClasses/DataGridColumn.html#width
I was able to get it to work by calling the above loop in a function twice... the first time it add the visible columns, the second time it sets the correct width. Not the best solution but I cannot spend any more time on it.