Cell position in TableView on MouseEvent - mouseevent

I'm trying to get the position of a cell in a TableView in JavaFX after a mouse event. I get the correct row value, but the column value is null. Any ideas why?
table.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
TablePosition tc = table.getFocusModel().getFocusedCell();
System.err.println(tc.toString()); // I get the correct row, but the column value is null.
}
});
Thanks!

Related

javafx format cells from ArrayList

I am working on a calendar application that shall retrieve date values from an ArrayList, which retrieves the string values from a SQL database. Formatting is achieved through a day cell factory.
Problem is that not only the dates from the ArrayList are formatted, by the cells of the whole month have a green background. Anyone has a hint of what I am doing wrong?
I started programming a year ago with Python and I am working with JavaFx for only a couple of months now, so my experiences with ArrayLists and cell factories are still quite limited.
Any help would be greatly appreciated. Here is what I have so far ....
ObservableList<LocalDate> dates = FXCollections.observableArrayList();
LocalDate date_1 = LocalDate.parse("2017-05-20");
LocalDate date_2 = LocalDate.parse ("2017-05-18");
dates.add(date_1);
dates.add(date_2);
Callback<DatePicker, DateCell> dayCellFactory = new Callback<DatePicker, DateCell>() {
public DateCell call (final DatePicker datePicker ) {
return new DateCell() {
#Override
public void updateItem (LocalDate item , boolean empty) {
super.updateItem(item, empty);
for (LocalDate ldt : dates) {
this.setStyle("-fx-background-color: green");
this.setTextFill(Color.BLACK);
this.setTooltip(new Tooltip("hello"));
}
};
};
};
};
dp.setDayCellFactory(dayCellFactory);
DatePickerSkin datePickerSkin = new DatePickerSkin(dp);
Node popupContent = datePickerSkin.getPopupContent();
calendar_pane_1.getChildren().add(popupContent);
Your cell's updateItem(...) method:
public void updateItem (LocalDate item , boolean empty) {
super.updateItem(item, empty);
for (LocalDate ldt : dates) {
this.setStyle("-fx-background-color: green");
this.setTextFill(Color.BLACK);
this.setTooltip(new Tooltip("hello"));
}
}
sets the style of the cell so that it has a green background, no matter the value of the item that is passed to the method. (It even does it twice, since there are two elements in the dates list.) So clearly, every cell will have a green background.
You didn't state it in the question, but I'm guessing that you only want the items in your list to have the styled background. You need:
public void updateItem (LocalDate item , boolean empty) {
super.updateItem(item, empty);
if (item != null && dates.contains(item)) {
this.setStyle("-fx-background-color: green; -fx-text-fill: black ;");
this.setTooltip(new Tooltip("hello"));
} else {
this.setStyle("");
this.setTooltip(null);
}
}

how to properly detect which mouse buttons are down in JavaFX

I've set a listener to my Pane so that it will detect mouse left and right buttons being down.
But when I hold left mouse button, then press right one, previous action seem to lose it's effect!
My code:
root.setOnMouseDragged(new EventHandler<MouseEvent>(){
#Override
public void handle(MouseEvent t) {
if(t.getButton() == MouseButton.PRIMARY) f1();
if(t.getButton() == MouseButton.SECONDARY) f2();
}
});
while holding LMB I have f1() running, but when I push RMB it seems like new event totally overwrites previous one: only f2() runs.
How can I separate this two events?
getButton() can return only one value at a time. And it's latest pressed button. If you need to detect multiple mouse down being pressed you need to use corresponding functions:
root.setOnMouseDragged(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent t) {
if (t.isPrimaryButtonDown()) {
System.out.println("rockets armed");
}
if (t.isSecondaryButtonDown()) {
System.out.println("autoaim engaged");
}
}
});

adding dropdown to table cell

I am facing some issues in eclipse rcp for setting the dropdown for a particular cell.
My requrement is to set the dropdown in the first row of the table. And that dropdown should be able to remove also.
One more thing that drop should be able to filter the contents in the table. So My question is that
1) Is it possible to add the dropdown only to the particular cell or row?
2) Can that filter act as a filter for the table?
3) How do I remove once I add the dropdown to the table cell?
Yes this is entirely possible. I suggest you start by reading Building and delivering a table editor with SWT/JFace, this tutorial contains everything you need to know.
As a rough outline, you will need to make the first item in your content model different from your data items - it will store the filter values. Then setup editing support on your TableViewerColumns something like (this is just a starter - this code will not work on its own):
tableViewerColumn.setEditingSupport(new EditingSupport(tableViewer)
{
#Override
protected boolean canEdit(Object element) {
if(object instanceof FilterDataObject) // your model object you are using to store the filter selections
{
return true;
}
}
#Override
protected CellEditor getCellEditor(Object element)
{
final ComboBoxCellEditor editor = new ComboBoxCellEditor(table, getPossibleFilterValues(), SWT.READ_ONLY);
((CCombo)editor.getControl()).addModifyListener(new ModifyListener()
{
public void modifyText(ModifyEvent e)
{
IStructuredSelection sel = (IStructuredSelection)m_tableViewer.getSelection();
FilterDataObject filterValue = (FilterDataObject)sel.getFirstElement();
// .. update the filter on your TableViewer
}
});
return editor;
}
#Override
protected Object getValue(Object element)
{
if(object instanceof FilterDataObject)
{
// get the filter value
}
else
{
// get your data model's value for this column
}
}
#Override
protected void setValue(Object element, Object value)
{
if(object instanceof FilterDataObject)
{
// update your FilterDataObject
}
}
});

How to Color TableViewer Rows alternatively

I'm using Viewer Framework in my rcp application, i would like to color viewer rows alternatively,i tried to override getBackground method of ColumnLabelProvider, below is code snippet
col.setLabelProvider(new ColumnLabelProvider(){
----//other methods
#override
public Color getBackground(Object element) {
return gray;//here gray is color object defined somewhere in class
}
});
this colors the columns, but not a row, below is output
how do i achieve this correctly
You can find an example here which uses an IColorProvider. Maybe you could just reuse the getBackground() method in your code, just change the reference to your tableViewer:
public Color getBackground(Object element) {
ArrayList list = (ArrayList) tableViewer.getInput();
int index = list.indexOf(element);
if ((index % 2) == 0) {
return gray;
} else {
return null;
}
}

Changing the style of Individual cells in a datagridview row

Is it possible to give individual cells in a data grid view row different styles such as backcolor, fontcolor etc?
I do not mean giving the whole row a new style, only a specific cell.
certainly:
Me.myDatagridview.Rows(0).Cells(0).Style.ForeColor = Color.Aqua
Something like this?
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string imageName = e.Row.Cells[0].Text.ToString();
e.Row.Cells[1].Attributes.Add(“Style”,
“background-image: url(’images/” + imageName + “‘);”);
}
}