How to center all elements from top to bottom in a GridPane - javafx

GridPane.setConstraints(button1,0,0);
GridPane.setConstraints(button2,0,1);
GridPane.setConstraints(button3,0,2);
gridPane.getChildren().addAll(button1,button2,button3);
I have some buttons in a GridPane as they are shown above, and I want to
center them all in gridPane;
center each button's label within
their button boundary (optional).
Currently the buttons are aligned to the left and they don't look so good. Can someone please tell me what to do? Thanks

You can set the vertical and horizontal alignments of each cell by using other constraint options:
GridPane.setConstraints( b1, 0, 0, 1, 1, HPos.CENTER, VPos.CENTER );
GridPane.setConstraints( b2, 0, 1, 1, 1, HPos.CENTER, VPos.CENTER );
GridPane.setConstraints( b3, 0, 2, 1, 1, HPos.CENTER, VPos.CENTER );
You may also set the gaps between the cells with:
gridPane.setVgap( 10 );
gridPane.setHgap( 10 );
to debug those values visually use:
gridPane.setGridLinesVisible( true );

Related

How to create custom hover tool with value mapping

I am trying to create a custom hover tool using which takes the y-value of the plot and maps the value to different value.
The code I could come up with so far to achieve this functionality is
from bokeh.models import HoverTool
import holoviews as hv
df = pd.DataFrame(
{
"zero": [0, 0, 0, 0, 0, 0, 0],
"one": [1, 1, 1, 1, 1, 1, 1],
"two": [2, 2, 2, 2, 2, 2, 2],
}
)
mapping = {i: c for i, c in enumerate(df.columns)}
def col_mapping(num):
return mapping[int(num)]
hover = HoverTool(tooltips=[("x", "$x"), ("y", "$y")])
img = hv.Image((df.index, np.arange(df.shape[1]), df.T)).opts(tools=[hover])
img
x and y will be float values. So the idea is to map the y coordinates to its corresponding value in the mapping dictionary
Let me know how I can get a new value in the hover tool so that when the value is b/w 0 and 1 it will be
Thanks
Here's how I'd do it:
code = f"return ({json.dumps(mapping)})[Math.floor(special_vars.y)];"
hover = HoverTool(tooltips=[("x", "$x"), ("y", "$y"), ('mapped_y', '$y{0}')],
formatters={'$y': CustomJSHover(code=code)})
If you need a some more complicated code than that of col_mapping, then you'd have to use a ColumnDataSource and just add to it the fully transformed column.

gdi, get width of asian characters

int dx[8];
int fit;
SIZE the_size;
res = GetTextExtentExPointW(dc, L"WWWWWWWW", 8, -1, &fit, &dx[0], &the_size);
This works, dx is filled with numbers 7 14 21 etc. But when I try to do the same for asian characters, like L"薔薇薔薇薔薇薔薇", this function fails. I even created a font for this, it doesn't change anything.
HFONT hFont = CreateFont(14,
0,
0,
0,
FW_DONTCARE,
FALSE, //fdwItalic
FALSE, //fdwUnderline
FALSE, //fdwStrikeOut
SHIFTJIS_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
NONANTIALIASED_QUALITY,
VARIABLE_PITCH,
TEXT("MS PGothic"));
if (hFont == NULL) FUCK();
SelectObject(dc, hFont);
The forth parameter should be the maximum width allowed, not -1. Use a large value instead. Check to make sure GetTextExtentExPointW succeeded.
if(GetTextExtentExPointW(dc, L"薔薇薔薇薔薇薔薇", 8, 1000, &fit, &dx[0], &the_size))
{
...
}
Note that a Unicode code point may require 4 bytes, or 2 wchar_t for each code point.

ControlsFX SpreadsheetView rowspan IndexOutOfBoundsException

I am new to JavaFX and ControlsFX.
I am trying to create a very basic SpreadsheetView using the ControlsFX library. Following is the function to populate and create the SpreadsheetView:
private fun spreadSheetFunc() : SpreadsheetView {
val rowCount = 15
val columnCount = 10
val grid = GridBase(rowCount, columnCount)
val rows = FXCollections.observableArrayList<ObservableList<SpreadsheetCell>>()
var list = FXCollections.observableArrayList<SpreadsheetCell>()
list.add(SpreadsheetCellType.STRING.createCell(0, 0, 1, 1, "row0-col0"))
list.add(SpreadsheetCellType.STRING.createCell(0, 1, 2, 1, "row0-col1"))
list.add(SpreadsheetCellType.STRING.createCell(0, 2, 1, 1, "row0-col2"))
rows.add(list)
list = FXCollections.observableArrayList()
list.add(SpreadsheetCellType.STRING.createCell(1, 0, 1, 1, "row1-col0"))
//commenting row1-col1 as row0-col1 has a rowspan of 2
//list.add(SpreadsheetCellType.STRING.createCell(1, 1, 1, 1, "row1-col1"))
list.add(SpreadsheetCellType.STRING.createCell(1, 2, 1, 1, "row1-col2"))
rows.add(list)
list = FXCollections.observableArrayList()
list.add(SpreadsheetCellType.STRING.createCell(2, 0, 1, 1, "row2-col0"))
list.add(SpreadsheetCellType.STRING.createCell(2, 1, 1, 1, "row2-col1"))
list.add(SpreadsheetCellType.STRING.createCell(2, 2, 1, 1, "row2-col2"))
rows.add(list)
list = FXCollections.observableArrayList()
list.add(SpreadsheetCellType.STRING.createCell(3, 0, 1, 1, "row3-col0"))
list.add(SpreadsheetCellType.STRING.createCell(3, 1, 1, 1, "row3-col1"))
list.add(SpreadsheetCellType.STRING.createCell(3, 2, 1, 1, "row3-col2"))
rows.add(list)
grid.setRows(rows)
return SpreadsheetView(grid)
}
On running it, I get the following error:
java.lang.IndexOutOfBoundsException: Index: 2, Size: 2 at
java.util.ArrayList.rangeCheck(ArrayList.java:653)
I know its happening because I am not adding any value for rowIndex=1 colIndex=1 (see the commented out line) ... but that is what I want.
The row0-col1 has a rowspan of 2 which should mean that even if my row1-col1 is absent, there shouldn't be any problem.
Why doesn't ControlsFX automatically take care of this?
If I uncomment that line, I get the following output:
Edit 1:
Also, I found another issue, when a colspan/rowspan occupies the whole column/row in the SpreadsheetView and then when one presses arrow key to navigate to cells you get an error:
The above situation arises when you press the right arrow key (Even though their isn't a cell on the right)
Let me apologize because it is not well documented how span must be made in the SpreadsheetView. I will update the documentation.
If you want to span, you have to put the same cell in every cell inside the span.
So either you build your own cell, and then in every place. In your case, you would add the same cell in row 0 column 1 and in row 1 column 1.
Or you could keep your code, and simply call the method spanRow on the Grid. This method will automatically take your cell and place it accordingly.
Regarding the second issue, please submit it to our issue tracker so we can fix it : https://bitbucket.org/controlsfx/controlsfx/issues?status=new&status=open
If you have other issue regarding the SpreadsheetView, consider posting in our Google group where we will get notifications : http://groups.controlsfx.org

QGraphicsRectItem::pos() function returns something that is not expected?

I have this simple piece of code in a class constructor which is inherited from QGraphicsScene :
setSceneRect(0,0,800,800);
addRect(sceneRect());
QGraphicsRectItem*r1 = addRect(200, 0, 5, 5);
qDebug()<<r1->pos();
Here I add a rect at pos(200,0) but qDebug() prints QPointF(0, 0)! I'm totally confused.
Even scenePos returns (0,0)! Is it true?!
The rect is at position (200, 0) inside the item's coordinate system, but the item itself is at (0,0) in its parent's coordinate system.
The QGraphicsScene::addRect() documentation explains this:
Note that the item's geometry is provided in item coordinates, and its position is initialized to (0, 0). For example, if a QRect(50, 50, 100, 100) is added, its top-left corner will be at (50, 50) relative to the origin in the items coordinate system.
Alternatively, you can add a rectangle at 0, 0, 5, 5) and move the item afterwards, to get the result you expected:
QGraphicsRectItem* r1 = addRect(0, 0, 5, 5);
r1->setPos(200, 0);

persp3d - main title that doesn't rotate? [duplicate]

This question already has answers here:
Put a fixed title in an interactive 3D plot using rgl package, R
(4 answers)
Closed 9 years ago.
Editing to provide more code and a runnable example.
If I open a 3D plot using the following code:
library(rgl)
GainTargets = seq(from=1, to=12, by=1)
PredBarTargets = seq(from=3, to=16, by=1)
data=structure(c(1176.18, 1379.34, 1280.67, 1149.02, 1034.01, 1028.74,
944.06, 807.54, 715.39, 691.08, 679.14, 644.02, 642.98, 577.61,
1004.22, 1238.13, 1216.16, 1104.92, 1032.46, 1038.11, 919.98,
855.47, 706.54, 668.51, 665.67, 689.57, 686, 623.82, 729.61,
1127.32, 1137.55, 1084.25, 955.15, 1005.86, 955.6, 881.66, 839.93,
767.45, 731.49, 727.19, 696.45, 634.52, 377.81, 917.08, 1087.57,
1036.17, 920.88, 993.75, 964.35, 848.94, 874.95, 780.58, 725.08,
695.12, 709.32, 663.93, 227, 787.53, 949.31, 972.59, 950.3, 946.22,
876.33, 881.49, 827.44, 767.66, 755.26, 731.71, 744.37, 692.86,
32.65, 404.91, 514.82, 652.8, 697.77, 859.85, 808.12, 794.15,
746.61, 719.72, 709.59, 662.36, 695.06, 687.76, 0, 262.09, 347.17,
442.35, 453.76, 684.06, 638.13, 664.69, 721.68, 688.32, 653.87,
665.21, 680.52, 685.13, 0, 38.77, 264.4, 432.8, 408.33, 457.38,
474.23, 453.76, 570.74, 591.96, 593.7, 636.75, 585.43, 577.72,
0, 0, 92.65, 255.08, 388.66, 432.73, 418.19, 425.02, 436.3, 452.17,
489.17, 499.27, 500.93, 495.26, 0, 0, 0, 90.28, 309.44, 324.99,
357.05, 376.11, 412.2, 337.4, 338.54, 370.11, 389.95, 501.99,
0, 0, 0, 0, 106.55, 190.84, 216.41, 322.11, 337.05, 313.99, 325.03,
356.98, 381.47, 373.77, 0, 0, 0, 0, 0, 198.84, 120.76, 204.33,
146.22, 289.93, 284.82, 337.22, 347.41, 337.29), .Dim = c(14L,
12L))
Points<-as.matrix(data)
open3d()
persp3d(x=PredBarTargets, y=GainTargets, z=Points,
col="green3", main="Total Points")
then I do get a title on the plot, but unfortunately the title moves around with mouse actions.
Does anyone know how to get the title text "Total Points" to be fixed at the top of the window such that it doesn't move? Also, if there's a way to associate the axis text with the axis that that has the numbers on it then that might be more readable.
Thanks
This cannot be done in rgl at the moment. You can only place text in 3d coordinates and have it rotate with the plot. Why not contact the maintainer of rgl and put this on a feature request list.
Note this Q is a duplicate of another question on SO.

Resources